import java.awt.*; public class animate extends java.applet.Applet implements Runnable
// if want a thread { Thread myThread; // will hold thread object Image imageBuffer; // temporary buffer Graphics bufferG; // graphic area in temp buffer int imageWidth; // width from HTML int imageHeight; // height from HTML // Create image and initialize image buffer public void init() { imageWidth = this.size().width; // get width imageHeight = this.size().height; // get height // create image buffer and get temp graphics object imageBuffer = createImage(imageWidth, imageHeight); bufferG = imageBuffer.getGraphics(); // add anything else for init() to do } // Create and start thread public void start() { if (myThread == null) { myThread = new Thread(this); // create thread myThread.start(); // start it } } // Override update() so it doesn't clear the screen public void update(Graphics g) { paint(g); } // Take image buffer and show on screen public void paint (Graphics g) { g.drawImage(imageBuffer, 0, 0, null); } // Here's the work for the Applet public void run() { ... while(true) { bufferG.clearRect(0, 0, imageWidth, imageHeight); // call drawing methods with bufferG instead of g ... repaint(); pause(5); ... } } void pause(int milliseconds) { try { Thread.sleep(milliseconds); } catch (InterruptedException e) {} } public void stop() { if (myThread != null) { myThread.stop(); // stop the thread myThread = null; // set to null so start recreates } } public void destroy() { bufferG.dispose(); // destroy temp Graphics object } }