Volatile images & double-buffering

Discussion in 'Java' started by nils@brickwall.org, Aug 28, 2003.

  1. Guest

    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.
    , Aug 28, 2003
    #1
    1. Advertising

  2. 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...
    Knute Johnson, Aug 29, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sandeep

    Double Buffering in ASP.NET

    Sandeep, Aug 10, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    810
    bruce barker
    Aug 10, 2004
  2. Rob Love
    Replies:
    1
    Views:
    3,279
    dhek bhun kho
    Jul 2, 2003
  3. David

    Help on double buffering

    David, Nov 23, 2003, in forum: Java
    Replies:
    0
    Views:
    316
    David
    Nov 23, 2003
  4. Sydex
    Replies:
    12
    Views:
    6,451
    Victor Bazarov
    Feb 17, 2005
  5. ben
    Replies:
    5
    Views:
    579
    Ulrich Eckhardt
    Jan 11, 2005
Loading...

Share This Page