Option 5: Double Buffering

1. Idea

Draw into an off-screen pixmap, then draw that entire pixmap onto window.

2. Outline

2.1 Override update to simply call paint.

This prevents the flicker that would normally occur each time update clears the screen before calling paint.

2.2 Allocate an Image using createImage.

Note that since this image uses native window-system support, it cannot be done until a window actually appears. For instance, you should call createImage in an applet from init (or later), not in the direct initialization of an instance variable. For an application, you should wait until after the initial frame has been displayed (e.g., by show or setVisible) before calling createImage. However, calling it earlier will not result in an error message; null will simply be returned.

2.3 Look up its Graphics object using getGraphics.

Unlike with windows, where you need to look up the Graphics context each time you draw, with images it is reliable to look it up once, store it, and reuse the same reference thereafter.

2.4 For each step, clear the image and redraw all objects onto it.

This will be dramatically faster than drawing onto a visible window.

2.5 Draw the offscreen image onto the window.

Use drawImage for this.

© 1996-99 Marty Hall, 1999 Lawrence M. Brown