I'm writting a Java application that runs an animation. It works, but the screen flickers when drawing. Is there a way to fix this?? Someone told me I needed to use Double Buffering -- what's that??
Printable View
I'm writting a Java application that runs an animation. It works, but the screen flickers when drawing. Is there a way to fix this?? Someone told me I needed to use Double Buffering -- what's that??
The idea of the double buffering is to, first draw everything into the object of the Image-class and then flash everyting at once to the screen.
Code example:
Image buffer;
Dimension size;
Graphics bufferGraphics;
.
.
.
Constructor
Dimension size = this.size();
bufferImage = createImage(size.width, size.height);
buffer = bufferImage.getGraphics();
.
.
.
public void paint(Graphics g) {
buffer.setColor(this.getBackground());
buffer.fillRect(0, 0, size.width, size.height);
//"first draw everything into the object of the Image-class"
g.drawImage(buffer, 0, 0, this);
// flash everyting at once to the screen
}
public void update(Graphics g) {
paint(g);
}
// Calling the "update" cleans the screen before updating, this might caused winking. Because of that it is better tocall directly "paint" for the updating.