Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Volatile images & double-buffering

Reply
Thread Tools

Volatile images & double-buffering

 
 
nils@brickwall.org
Guest
Posts: n/a
 
      08-28-2003
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().createCompatib leVolatileImage(
imgBuffer.getWidth(null), imgBuffer.getHeight(null));
}


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

case VolatileImage.IMAGE_INCOMPATIBLE :
volatileimage.flush();
volatileimage = graphics2d.getDeviceConfiguration().createCompatib leVolatileImage(
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.
 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      08-29-2003
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.

--

Knute Johnson
email s/nospam/knute/
Molon labe...

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ERROR [HY000] [Microsoft][ODBC Microsoft Access Driver]General error Unable to open registry key 'Temporary (volatile) Jet DSN for process 0xffc Thread 0x228 DBC 0x437b94 Jet'. ERROR [IM006] [Microsoft][ODBC Driver Manager] Driver's SQLSetConnectAttr bazzer ASP .Net 0 03-30-2006 03:16 PM
ERROR [HY000] [Microsoft][ODBC Microsoft Access Driver]General error Unable to open registry key 'Temporary (volatile) Jet DSN for process 0x8fc Thread 0x934 DBC 0x437b94 Jet'. ERROR [IM006] [Microsoft][ODBC Driver Manager] Driver's SQLSetConnectAttr bazzer ASP .Net 1 03-24-2006 04:20 PM
ERROR [HY000] [Microsoft][ODBC Microsoft Access Driver]General error Unable to open registry key 'Temporary (volatile) Jet DSN for process 0x8fc Thread 0x934 DBC 0x437b94 Jet'. ERROR [IM006] [Microsoft][ODBC Driver Manager] Driver's SQLSetConnectAttr bazzer ASP .Net 0 03-24-2006 02:22 PM
Use of the Volatile keyword for a pointer to a volatile memory block ben C Programming 5 01-11-2005 05:38 PM
Re: Volatile? Knute Johnson Java 17 07-03-2003 03:31 AM



Advertisments