import java.awt.*; import java.applet.*; public class javalamp extends Applet { private int height, width; private lampCanvas lc; private animation animate; public void init() { this.width = Integer.parseInt(getParameter("width")); this.height = Integer.parseInt(getParameter("height")); setLayout(new BorderLayout()); lc = new lampCanvas(width, height); add("Center", lc); setBackground(Color.black); } public void start() { if (animate == null) { animate = new animation(lc); animate.start(); } } public void stop() { if (animate != null) { animate.stop(); animate = null; } } } class lampCanvas extends Canvas { static int NUM_BLOBS = 100; private int width, height; private Image offimage; private Graphics offgfx; private Blob[] blob; public lampCanvas(int width, int height) { this.width = width; this.height = height; this.blob = new Blob[NUM_BLOBS]; for(int i = 0; i < NUM_BLOBS; i++) { this.blob[i] = new Blob(width, height); } } public void update(Graphics g) { //create double buffer if (offgfx == null) { offimage = createImage(width, height); offgfx = offimage.getGraphics(); } //clear image offgfx.setColor(this.getBackground()); offgfx.fillRect(0, 0, width, height); //draw image offgfx.setColor(Color.red); for (int i = 0; i < NUM_BLOBS; i++) { blob[i].moveBlob(width, height); blob[i].drawBlob(offgfx); } g.drawImage(offimage, 0, 0, null); } } class Blob { private double x, y, yDir; private int blobXDiam, blobYDiam; private Image blobImage; Blob(int width, int height) { this.blobXDiam = (int)(Math.random() * width / 10) + 10; this.blobYDiam = (int)(Math.random() * blobXDiam / 2) + blobXDiam; this.x = (Math.random() * (width + blobXDiam)) - blobXDiam; this.y = (Math.random() * (height + blobYDiam)) - blobYDiam; this.yDir = Math.random() * width / 100; if ((int)(Math.random() * 2) == 0) yDir = -yDir; } public void moveBlob(int width, int height) { y += yDir ; if (y <= -blobYDiam || y >= height ) { this.blobXDiam = (int)(Math.random() * width / 10) + 10; this.blobYDiam = (int)(Math.random() * blobXDiam / 2) + blobXDiam; this.x = (Math.random() * (width + blobXDiam)) - blobXDiam; this.y = (int)(Math.random() * 2) * (height + blobYDiam) - blobYDiam; this.yDir = Math.random() * width / 100; if (y >= height) yDir = -yDir; } } public void drawBlob(Graphics g) { g.fillOval((int)(x), (int)(y), blobXDiam, blobYDiam); } } class animation extends Thread { private lampCanvas lc; animation(lampCanvas lc) { this.lc = lc; } public void run() { while(true) { lc.repaint(); try { sleep(10); } catch(InterruptedException ie) {} } } }