Fast background drawing

Hi

I am searching for the fastest way to draw a component’s background in AWT/Swing. Currently I use a JPanel and override the paintComponent() method. In this method I update a TYPE_3BYTE_BGR BufferedImage and simply draw it on the panel’s Graphics2D.

This seems odd to me, since the panel’s background is cleared with the background color each time it is redrawn. It would be better, if it was cleared with my BufferedImage directly. This would mean at least one huge pixel operation less.

How can I do this? Should I use a JLabel with my BufferedImage as an icon? Should I use a heavyweight component?

Thanks in advance,
Marvin

component.setOpaque(false); ?

Yes, I have already done that. But I still have to draw my image over the cleared background.

Marvin

Ah… like that :-X

Well, IIRC java.awt.Canvas provides that. You just must be sure that repaint() does nothing, and you grab the graphics of a canvas using canvas.getGraphics() and draw your stuff on top of what is already there.

It can turn into a big mess when you are using an ancient OS (like XP) that doesn’t have a pixelbuffer per window, and works with dirty rectangles.

Thanks. I will try that.

And ofcourse active rendering… you flip the buffers, and they don’t get cleared after the flip.

you should just have to overide “update (Graphics g )” method of your components (the top one so it wont repaint background) and on this update method fill your buffered image and draw it to the Graphics g?

something like :

public void update (Graphics g)
{
 bufferedGraphics.drawSomething();
 then for each  childrens components  call 
 child[i].update(bufferedGraphics);
 g.drawImage(buffereGraphicsImage,0,0,null);
}