FileDocCategorySizeDatePackage
UpdateImage.javaAPI DocExample1060Tue Dec 02 02:59:40 GMT 1997None

UpdateImage.java

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

public class UpdateImage extends java.applet.Applet implements Runnable {
	int arrayLength, pixels[];
	MemoryImageSource source;
	Image image;
	int width, height;

	public void init() {
		width = getSize().width; height = getSize().height;
		arrayLength = width * height;
		pixels = new int [arrayLength];

		source = new MemoryImageSource(width, height, pixels, 0, width);
		source.setAnimated(true);
		image = createImage(source);
		// bad form... start threads in start(), stop them in stop()
		new Thread(this).start();
	}

	public void run() {
		while (true) {
			try {
				Thread.sleep(1000/24);
			} catch( InterruptedException e ) { /* die */ }

			for (int x = 0; x < width; x++) 
				for (int y = 0; y < height; y++)  {
					boolean rand = Math.random() > 0.5;
					pixels[y*width+x] = 
						rand ? Color.black.getRGB() : Color.white.getRGB();
				}

			// Push out the new data
			source.newPixels(0, 0, width, height);
		}
	}

	public void paint( Graphics g ) {
		g.drawImage(image, 0, 0, this);
	}
}