UpdateImagepublic class UpdateImage extends Applet implements Runnable
Fields Summary |
---|
int | arrayLength | int[] | pixels | MemoryImageSource | source | Image | image | int | width | int | height |
Methods Summary |
---|
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 | paint(java.awt.Graphics g)
g.drawImage(image, 0, 0, this);
| 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);
}
|
|