This example derived from Exploring Java, 2nd ed, chapter 16, shows an image with "terrible flicker" as it is moved:
| |
import java.awt.*; public
class TerribleFlicker extends Applet
int grid = 10;
public void init() {
public void mouseDragged( MouseEvent e ) { // Draw the checkerboard background
|
Since we redraw the whole applet anyway, no need for update to waste time clearing it. So override it with an update that just calls paint():
| |
import java.awt.*; public
class UpdateOverride extends Applet ... |
Finally, we use the most powerful method for eliminating flicker: the double buffer.
The process of sending drawing instructions to the screen involves a lot of overhead. There are lots of communications calls between the AWT and the host that take time.
By doing the drawing all onto an image within Java and then just sending the whole image in one call, there is a considerable speed up in the drawing.
Below we create an image and then get its graphics context object for doing the drawing onto it. Once this image is ready, we just draw it to the screen in one call.
Note that once a graphics context object is clipped, it cannot be unclipped. So we need to get a new one each time around.
| |
import java.awt.*; public class
DoubleBufferedClipped extends Clipped { public void
update( Graphics g ) { // Do the clipping on both the off // Now draw on the offscreen image.
// Don't bother to call paint,
// Get rid of the offscreen graphics
context. |
Maintained by John Loomis, last updated 17 July 2003