Home Articles FAQs XREF Games Software Instant Books About Feedback Search Site-Map
irt.org logo

Q4003 How can I minimise "Flicker" in animation?

irt.org | Knowledge Base | Java | Q4003 [ previous next ]

Q4003 How can I minimise "Flicker" in animation?

Solution 1:

Override update() : Flickering in animation occurs because default update() method clears the screen of any existing contents and then calls paint(). To reduce flickering, therefore, override update(). Here is how just add the following code to your applet:

public void update(Graphics g) {
   paint(g);
}

What the update is now doing is just calling paint() and not clearing it, a further refinement to the above will be to override update() method and painting only the region where the changes are taking place. Here is how:

public void update(Graphics g) {
   g.clipRect(x, y, w, h);
     paint(g);
}

Solution 2:

Use double-buffering : double buffering is the process of doing all your drawing to an offscreen and then displaying the entire screen at once. It is called double buffering because there are two drawing buffers and you switch between them. Use double buffering only if the above solution alone does not work. The following code snippet describes how to do it.

Image offscreenImage;
Graphics offscreenGraphics;
offscreenImage = createImage(size().width, size().height);
offscreenGraphics = offscreenImage.getGraphics();
offscreenGraphics.drawImage(img, 10, 10, this);
g.drawImage(offscreenImage, 0, 0, this);

Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


Last Updated: 6th July 2009. Maintained by: Martin Webb
irt.org liability, trademark, document use, privacy statement and software licensing rules apply.
Copyright © 1996-2009 irt.org, All Rights Reserved.