Backbuffer clearing and volatile images

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 -


paintComponent(Graphics g) {

   // fill my bufferedimage with black
   // Draw some stuff directly on the g context
   // pass the graphics context of my bufferedImage to a routine
   // do a drawImage call on g with my bufferedImage

}

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!

yes, what your code is doing is creating a backbuffer.

When performing simple draw operations that can be hardware accelerated, your will get best performance when your backbuffer is stored in vram. (i.e. a VolatileImage)

BufferedImages have their primary surface in main memory, so any blit operation onto them will be performed in software. (and so cannot be hardware accelerated)

*I say primary surface, as BufferedImages constructed correctly can be cached in vram.
However, this is only a 1 way optimisation (images reads)
Modifications to a BufferedImage (image writes) happen to the master copy of the image in main memory, causing any cached version in vram to be discarded.

So if I switch to using a volatile image, I should see a performance jump.

What about clearing the image each time vs getting a new volatile image?

Thanks so much!

Regards,
Aaron R>

[quote]So if I switch to using a volatile image, I should see a performance jump.

What about clearing the image each time vs getting a new volatile image?

Thanks so much!

Regards,
Aaron R>
[/quote]
It depends what operations you are performing on the VolatileImage, but yes, in general you will see a huge performance gain. (in the order of 100times faster)

Clearing an Image is always going to be faster than creating a new Image, and discarding the old 1. (regardless of the type of Image it is)

Swell! Thanks for the info.

I’ve redone the stuff from before. I haven’t seen a real performance jump though and I’m a tad confused. Someone answered this question at javaranch with some code. They made a quick app where you can switch between a volatile and buffered image draw routine.

http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=2&t=009405

Any thoughts on why I’m not seeing a jump in performance?