aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Drawing to a backbuffer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Drawing to a backbuffer" Watch "Drawing to a backbuffer" New topic
Author

Drawing to a backbuffer

Aaron Roberts
Ranch Hand

Joined: Sep 10, 2002
Posts: 174
I've written a simple app which shows some graphics on the screen and does some very basic sprite movement. I don't know/understand the best way to redraw each frame though.

What I'm doing now is approximately this -



This works, but it seems like a very 'expensive way' to do the drawing each frame. Is there some way to clear out the bufferedImage without doing a fill? Would it be better to create a new image each time - seems like I'd be moving lots of memory around that way.

I read the article at -

http://weblogs.java.net/pub/wlg/435

and wondered if using a volatile image would be a better way. I'm not using full screen mode. I'm drawing on a JPanel, by overriding its paintComponent method.

Thanks!
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Aaron Roberts
Ranch Hand

Joined: Sep 10, 2002
Posts: 174
Hey thanks alot for the example!

I didn't have the T6 image in the path you had listed, so I grabbed one from the j2sdk under the applets examples. It looks like a molecule of some sort.

The only difference on my machine was that using a volatile image, the background gets drawn. IE I get a grey box around the pic as it bounces around the screen. The buffered doesn't do this.

It also seems to stutter when I drop the delay down all the way to 10. Its not a jerkiness per se. You can see the pic sliding across at a constant speed and then it hiccups ever so slightly. Would this be the garbage collector maybe?

Regards,
Aaron R>
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
The gray box around your volatile image suggests trouble in this line:

I used the T6 in the tumbling duke image sequence from the sun example index. It has a white background and was being rendered on a white background in the app so I could have had some invisible artifacts. You could try the no–argument repaint to see if it works any better. I may have messed up the clipping rectangle. Please let me know how you do with it.

Did you read Chet's second article? It has some more code and information. I found that re–initializing the volatile image when validate returns VolatileImage.IMAGE_RESTORED (as shown in the articles and api) causes some tail–chasing: the renewed image triggers the IMAGE_RESTORED state.

I was pleased to find that the volatile image never needed to be re–initialized, even when I did some of the things on Chet's list of lost–data causes like launching the windows task manager. And I also could not distinguish any difference in appearance or performance between the volatile and buffered images.

I also observed the uneven motion of the image at high speeds near 10. I think this is caused by Swing adding separate calls to repaint together as they pile up in the event dispatch thread queue.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Drawing to a backbuffer
 
Similar Threads
How do I render and draw buffered Images in Parallel? - JFreeChart Question
Canvas HELP!!!
bufferedImage and Graphics2D
Confused about painting in JPanels
Graphics problem , how to approach ?