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.
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.