An outline of the basic program structure for double buffering images is given below. It follows the strategy outlined in the previous section. In the next section, an example program using this structure is presented.
import java.applet.*;
import java.awt.*;
import java.util.*;
public class DoubleBuf extends Applet implements Runnable {
// ************* init
public void init () {
} // end init
// ************* start
public void start () {
runThread = new Thread ( this );
runThread.start ();
} // end start
// ************* stop
public void stop () {
if ( runThread != null ) runThread.stop ();
runThread = null;
} // end stop
// ************* mouseDown
public boolean mouseDown ( Event event, int x, int y) {
} // end stop
// ************* paint
public void paint ( Graphics g, Dimension d ) {
// *** Draw on the image here ***
} // end paint
// ************* run thread
public void run () {
while ( ! pause ) {
g = offScreen.getGraphics ();
paint ( g, d );
g = this.getGraphics ();
g.drawImage ( offScreen, 0, 0, Color.white, this );
try {Thread.sleep ( 10 );} catch ( InterruptedException e) {;}
} // end while
runThread = null;
} // end run
} // end DoubleBuf