Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q4003 How can I minimise "Flicker" in animation?

You are here: irt.org | FAQ | Java | Q4003 [ previous next ]

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);

©2018 Martin Webb