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 ) ?
# 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();
}
}
# 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 ?
# 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.