drawing with double buffering to a JFrame

I want to create a simple JFrame and draw stuff to it viaGraphic object using double buffering.

If I create a loop and draw constantly using JFrame.getGraphic().xxx, am i doingdouble buffering. ( 'cause i read somewhere that swing components automaticly use double buffering ). And is this the righ approuch ( or whatever it's spelled ) ?

[361 byte] By [Sasa_Ivanovic] at [2007-11-26 12:16:21]
# 1

I want to create a simple JFrame and draw stuff to it

Don't do that.

If you want to do custom rendering then use JPanel or JComponent, not JFrame.

If I create a loop and draw constantly using JFrame.getGraphic().xxx, am i doing double buffering.

No.

You're also painting outside the standard paint cycle, which is a Bad Idea.

So, essentially, everything your suggesting is the wrong way to go about things.

In its most simple form, double buffering works something like this, though there are a whole load of subtleties to consider (such as component resizing for a start)

public class Foo extends JComponent

{

private BufferedImage image = null;

protected void paintComponent(Graphics g)

{

if (image != null)

{

image = createImage(getWidth(), getHeight());

paintBuffer(ig);

}

g.drawImage(image, 0, 0, this);

}

private void paintBuffer()

{

Graphics ig = image.getGraphics();

// do the rendering

ig.dispose();

}

}

itchyscratchy at 2007-7-7 14:52:17 > top of Java-index,Archived Forums,Socket Programming...
# 2
ok, thanks that helped !
Sasa_Ivanovic at 2007-7-7 14:52:17 > top of Java-index,Archived Forums,Socket Programming...
# 3
isn't the resize problem taken care of by this :image = createImage(getWidth(), getHeight());since you inherit from JComponent, and it has it's own resizing methods.Or am i wrong ?
Sasa_Ivanovic at 2007-7-7 14:52:17 > top of Java-index,Archived Forums,Socket Programming...
# 4

Not simply by that, no, since that's in an "if (image == null)" block, so once it's been created it won't be recreated. Obviously the simplest thing to do is to convert this to,

if ((image == null) || (image.getWidth() != getWidth()) || (image.getHeight() != getHeight()))

{

...

}

But depending on the specific requirements something slightly different might be preferable.

itchyscratchy at 2007-7-7 14:52:17 > top of Java-index,Archived Forums,Socket Programming...
# 5
Actually there's a typo in my first post, the test should of course be "if (image == null)" instead of "!=" ...always catching myself out with that one :o(
itchyscratchy at 2007-7-7 14:52:17 > top of Java-index,Archived Forums,Socket Programming...
# 6
thanks, i get it now!
Sasa_Ivanovic at 2007-7-7 14:52:17 > top of Java-index,Archived Forums,Socket Programming...