Volatile images & double-buffering

N

nils

In order to learn how to use Volatile-images (in combination with
double-buffering), I'm trying to create a very simple animation
on a JPanel.

The method 'drawMovingSquare()' (see below) draws a little red
retangle on a image-buffer and every it's called, it increases the
X-coord, so the rectangle should be 'moving'.

But to my surprise, the rectangle is drawn only once (?).


These are the keyparts of my code :


imageBuffer = createImage(widthPanel, heightPanel);
offScreenImage = imageBuffer.getGraphics();

public void paint(Graphics g)
{
super.paint(g);
volImage = drawVolatileImage((Graphics2D) g, volImage, 10, 30, imageBuffer);
}

private VolatileImage drawVolatileImage(Graphics2D graphics2d, VolatileImage volatileimage,
int i, int j, Image imgBuffer)
{
if(imgBuffer == null) { return null; }


final int MAX_TRIES = 100;
for(int k = 0; k < MAX_TRIES ; k++)
{
if(volatileimage != null)
{
graphics2d.drawImage(volatileimage, i, j, null);
if(!volatileimage.contentsLost()) { return volatileimage; }
}
else
{
volatileimage = graphics2d.getDeviceConfiguration().createCompatibleVolatileImage(
imgBuffer.getWidth(null), imgBuffer.getHeight(null));
}


switch(volatileimage.validate(graphics2d.getDeviceConfiguration()))
{
case VolatileImage.IMAGE_OK : break;

case VolatileImage.IMAGE_INCOMPATIBLE :
volatileimage.flush();
volatileimage = graphics2d.getDeviceConfiguration().createCompatibleVolatileImage(
imgBuffer.getWidth(null), imgBuffer.getHeight(null));
// fall through

case VolatileImage.IMAGE_RESTORED :
Graphics2D graphics2d1 = volatileimage.createGraphics();
graphics2d1.drawImage(imgBuffer, 0, 0, null);
graphics2d1.dispose();
break;
}
}

graphics2d.drawImage(imgBuffer, i, j, null);
return volatileimage;
}

private final void drawMovingSquare()
{
offScreenImage.setColor(Color.white);
offScreenImage.fillRect(X, Y, 20, 20);

X++;
if(X > widthPanel - 12) { X = 0; }

offScreenImage.setColor(Color.red);
offScreenImage.fillRect(X, Y, 20, 20);
repaint();
}

Can anyone tell me what I'm doing wrong and/or please point me to
some examples on how to use Volatile-images ? The API-documentation
is not a great help to me and I haven't been able to find any other useful
examples so far. Thanks for your effort.
 
K

Knute Johnson

I'm not sure what your problem is but here are the basics to using the
VolatileImage and drawing it on a Panel or JPanel.

This is pseudo code:

class myClass extends JPanel implements Runnable {
VolatileImage image = null;

public myClass() {
new Thread(this).start();
}

public void run() {
render();
repaint();
}

public void render() {
do {
if (image.validate(getGraphicsConfiguration()) ==
VolatileImage.IMAGE_INCOMPATIBLE)
image = createVolatileImage(100,100);
Graphics g = image.createGraphics();
// draw on the Graphics here
g.dispose();
} while (image.contentsLost()) ;
}

public void paintComponent(Graphics g) {
if (image == null)
image = createVolatileImage(100,100);
do {
if (image.validate(getGraphicsConfiguration()) !=
VolatileImage.OK)
render();
g.drawImage(image,?,?);
} while (image.contentsLost()) ;
}
}

That covers the basic framework. You will need to do your actual
drawing and have a procedure for making the animation changes and
timing. I would not call super.paintComponent(), instead I would
completely redraw the volatile image and then draw it onto the JPanel's
Graphics. There are a lot of other little tricks you can employ here
but this should get you started.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top