Hi all,
I am working on the "Pacman" game and I want to use the "double buffering" technique for animation.

By now, I have used this code:

class GamePanel extends JPanel
    public void paint(Graphics g)
        // Do some drawing here...

and there was by default used double buffering (because in "JPanel" there is double buffering technique used by default).

But sometimes later I realized that I want to do all my painting code in my own method, not in "paint()" one (I don't want to explain why, it is another long story, lol).

And there is a question: how can I implement the double-buffering myself?
And after some time I have written this solution:

class GamePanel extends JPanel
    private Image backBuffer;
    private Graphics backBufferGraphics = backBuffer.getGraphics();

    public void myDrawingMethod()
        // Get the painting area which is visible on the screen
        Graphics frontBufferGraphics = this.getGraphics();

        // Some sample painting code (executing on the back buffer
        // which is not visible on screen)
        backBufferGraphics.drawLine(10, 10, 20, 20);
        backBufferGraphics.drawRect(3, 5, 40, 32);
        backBufferGraphics.drawPoint(20, 30);
        // ....
        // .... etc...
        // ....

        // And finally draw the whole graphics to the screen
        frontBufferGraphics.drawImage(backBuffer, 0, 0, this);

My question is:
Is this the good technique (I mean in performance) to do all the drawing on some image and then draw that image at once to the screen, or is there some better way (in performance) to do this (maybe some pointer changing, page flipping, or something similar,...)?



8 Years
Discussion Span
Last Post by Ezzaral

It would be interesting to hear why you have rejected using paint() (or paintComponent()), because that's the "right" way to do it, and guarantees that you will handle things like windows being re-sized or covered/uncovered properly. Looking at your code as it stands, I expect that Swing will use paint() to overwrite whatever your method is doing at unpredictable (to your code) intervals.


If you're into Windows operational system ->
Study SUN example:
For your use

import com.sun.animation.SmoothAnimation;


SmoothAnimation smoothAnimation;

In constructor

smoothAnimation = new SmoothAnimation();

and in your overrided method

public synchronized void paintComponent(java.awt.Graphics g)

invoke NATIVE method (at end)


Remember abut


>Is this the good technique (I mean in performance) to do all the drawing on some image and then draw that image at once to the screen
Yes, that is typically what is done. You can update portions of the image as you wish and paintComponent() just renders that image to the screen. Be sure to dispose() of Graphics references that you obtain from getGraphics() calls.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.