import java.awt.*;
    import java.awt.image.*;
    import java.applet.*;

    public class AnimationExample extends Applet implements Runnable {
        Thread anim;
        MemoryImageSource imgsrc;
        Image memimg;
        int[] pixels;

        public void init() {
            pixels = new int[100 * 100];
            imgsrc = new MemoryImageSource(100, 100, pixels, 0, 100);
            imgsrc.setAnimated(true);
            memimg = createImage(imgsrc);

        }

        public void start() {
            anim = new Thread(this);
            anim.start();
        }

        public synchronized void stop() {
            anim = null;
            notify();
        }

        public synchronized void run() {
            while (Thread.currentThread() == anim) {
                int x = (int) (Math.random() * 100);
                int y = (int) (Math.random() * 100);
                int r = (int) (Math.random() * 255);
                int g = (int) (Math.random() * 255);
                int b = (int) (Math.random() * 255);
                pixels[y * 100 + x] =
                    ((int) (Math.random() * 0xffffff)) | (0xff<<24);
                imgsrc.newPixels(x, y, 1, 1);
                repaint();
                try {wait(100);} catch (InterruptedException e) {return;}
            }
        }

        public void paint(Graphics g) {
            // Draw the animated image
            g.drawImage(memimg, 0, 0, this);
        }
    }