Copying Images Offsceen

Hi.

I am using a javax.swing.JPanel to display data in diagrams and graphs.

The routine is this:

EVENT: Size changed -> Children recreate BufferedImages and render their contents. -> Panel calls draw method -> Children draw their images on the Graphics of the BufferedImage used to prebuffer contents in the Panel -> Panel paints the prebuffered Image on the Screen.

On paint(Graphics g) only the prebufferedImage is drawn with g.drawImage(…)

This works fine till 640x480. BUT:

Animation stuff: I start a Thread moving a node from left to right. This causes the Panel to update its contents, what means that the render() method calls the children to draw their Images to the Buffer and then the Buffer is displayed by repaint(). And this takes simply too long and eats CPU like hell.

Can anyone help me to get some Performance tuning. I am using BufferedImages all over. VolatileImage is too slow ! ???

Isn’t JPanel dubble-buffered anyway?

DoubleBuffered or not, it’s going to call your CustomWidget#paint() method more than you’d like.

When the user resizes the window, you pretty much have to redraw everything. But if the size hasn’t changed, you can skip the step of having the children redraw to their bufferedImages, but rather just copy the bufferedImage your drew last time.

But I think that’s what you are doing already? Beyond that you can start caching the background and all the children that haven’t changed, so that you only have to draw that image and the one child that’s changing.

Turning off RenderingHints and avoiding recalculating anything not based on size could help as well.