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,...)?
Thanks.
Petike