Results 1 to 12 of 12
- 01-26-2012, 12:33 PM #1
Tile game! Help me increase the performance of Graphics2D.
So i decided to start programming in java again after countless hours of Erlang coding. So basically im making a simple game thats going to have tiles. My tile map is going to move and my main character is going to be focused in the center of the screen. Each tile is 40x40 pixels.
My strategy is like this ... I paint a larger picture (80 pixels each side) and when the picture has moved one tile (40 pixels) in some direction I move the picture back and update the objects. This works but you can see the updates (screen is a bit blurry when you hold down the action keys). Im going to have one thread to move the map and update the screen and another to change the mainchars movement.
I'v tried to figure out how double buffering works but I don't know if I implemented it correctly.
This is the code for the graphics and iv removed all unnecessary things like listeners and logic for moving the screen.
Java Code:@Override public void paintComponent(Graphics g){ super.paintComponent(g); if (getSize().width <= 0 || getSize().height <= 0) return; Graphics2D g2 = createOffscreenImage(g); paintOnPicture(g2); g2.dispose(); if (offscreen != null && isShowing()) { g.drawImage(offscreen, posInImageX, posInImageY, this); } } // Creates offscreen image for double buffering private Graphics2D createOffscreenImage(Graphics g){ Graphics2D g2 = null; if (offscreen == null || offscreen.getWidth() != imageSizeToBuffer.width || offscreen.getHeight() != imageSizeToBuffer.height) { offscreen = (BufferedImage) createImage(imageSizeToBuffer.width, imageSizeToBuffer.height); } if (offscreen != null) { g2 = offscreen.createGraphics(); } g2.clearRect(0, 0, imageSizeToBuffer.width, imageSizeToBuffer.height); return g2; } // This method is used to do all the painting. Currently is goes through an array and paints the pictures (tiles) that each object has. private void paintOnPicture(Graphics2D g2d){ int x=0,y=0; for ( int i = BlockLoctionY; i < (BlockLoctionY + numOfBlocksY);i++){ for(int ii = BlockLoctionX;ii< (BlockLoctionX + numOfBlocksX);ii++){ Map.currentMap[i][ii].paintMe(g2d,y*40,x*40); // goes to each object and paints them x++; } x = 0; y++; } } }
So any ideas how to improve the performance of the graphics? I was thinking to draw a sub image in the screen in staid of moving a bigger picture. But it kind of failed with the drawImage method though its stuck in one side resulting in the image being zoomed and dragged out when i tried to change the position. Thanks for your help guys ! I havent programmed java in ages
- 01-26-2012, 02:58 PM #2
Re: Tile game! Help me increase the performance of Graphics2D.
I don't think that's really double buffering. In fact, you're making more work for yourself by drawing to an image, and then immediately drawing that image to the screen. That approach might be valid, but I don't think it counts as double buffering. Are you sure you even need double buffering?
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 01-26-2012, 03:21 PM #3
Re: Tile game! Help me increase the performance of Graphics2D.
If i was to write the image directly to the screen I would see the rendering (for loop adding images) so basically in my code i have the "offscreen" object witch is my BufferedImage to make the drawings on ...and then i create a graphics object (g2) to write stuff to this image in the createOffscreenImage() and then i write stuff on the image in paintOnPucture() and then disposes the g2 graphics object (for memory) and draw the image to the screen. I think this is pretty much the definition of double buffering Double Buffering and Page Flipping (The Java™ Tutorials > Bonus > Full-Screen Exclusive Mode API). My problem is that the image still becomes a bit blurry ...
I'm starting to think that the "if statement" that changes the object and center the image might have something to do with the blur... ex the objects should change and the image be centered if the image has moved 40 pixels in any direction but maybe it moves more than 40 pixels and thereby causing the blur. Or maybe i'm drawing to big picture (size of JPanel + 180 pixels in each direction). Thanks
- 01-26-2012, 03:25 PM #4
Re: Tile game! Help me increase the performance of Graphics2D.
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 01-26-2012, 03:31 PM #5
Re: Tile game! Help me increase the performance of Graphics2D.
Im sorry i was wrong you don't see the rendering... but now i don't know how to move the background anymore . =( how can i display half objects and so on.
- 01-26-2012, 03:47 PM #6
Re: Tile game! Help me increase the performance of Graphics2D.
I'm not really sure what you mean by half objects. Just drawing off the side of the screen?
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 01-26-2012, 04:27 PM #7
Re: Tile game! Help me increase the performance of Graphics2D.
Exactly. Swing components are double buffered by default.
That said, your code loses all possible benefits of drawing to an offscreen buffer by doing that drawing in a call originating from paintComponent(...). The correct approach is to do the offscreen drawing whenever a state change requires a GUI update, and calling repaint() when done.
The paintComponent(...) override would then only draw the BufferedImage.
Moving this discussion to Java 2D
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 01-26-2012, 04:41 PM #8
Re: Tile game! Help me increase the performance of Graphics2D.
So your saying that i should just draw directly to the screen using drawImage() or when a state change occur use double buffer (before) and then just call repaint() and paint the buffered image? Its really confusing for me, can you explain it so that a idiot would understand it ? =)
- 01-26-2012, 04:51 PM #9
Re: Tile game! Help me increase the performance of Graphics2D.
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 01-27-2012, 12:52 AM #10
Re: Tile game! Help me increase the performance of Graphics2D.
well I now draw the images without any buffering and its the same (if not worse flickering on the screen when i move the background).
Java Code:@Override public void paintComponent(Graphics g){ super.paintComponent(g); Graphics2D g2d = ((Graphics2D) g); if (getSize().width <= 0 || getSize().height <= 0) return; paintOnPicture(g2d); } private void paintOnPicture(Graphics2D g2d){ paintX = movementX; paintY = movementY; for ( int i = 0; i < numOfBlocksY;i++){ for(int ii = 0;ii< numOfBlocksX;ii++){ Map.currentMap[i][ii].paintMe(g2d,paintY,paintX); // Draws an 40x40 image paintX += 40; } paintX = movementX; paintY += 40; } }
Any idea what i'm doing wrong? It still feels like i should double buffer guys ! =P
- 01-27-2012, 05:17 AM #11
Re: Tile game! Help me increase the performance of Graphics2D.
What does the paintMe(g2d,paintY,paintX) method look like?
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 01-27-2012, 03:50 PM #12
Similar Threads
-
Help tile TimeScrambler game
By DrCooksAlot in forum New To JavaReplies: 8Last Post: 01-25-2012, 03:12 PM -
How to increase performance?
By sandeep43 in forum New To JavaReplies: 7Last Post: 08-08-2011, 06:38 PM -
Simple Tile Game Question
By sgthale in forum Java AppletsReplies: 9Last Post: 06-17-2011, 11:20 AM -
Need help in Graphics2D
By Yakkut in forum New To JavaReplies: 1Last Post: 03-30-2011, 08:05 PM -
Concept of Tile map via database
By GrInd3r in forum New To JavaReplies: 0Last Post: 12-08-2010, 05:18 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks