FileDocCategorySizeDatePackage
Animator.javaAPI DocExample3898Thu Mar 25 21:30:24 GMT 2004None

Animator

public class Animator extends Applet implements Runnable
Animator - move a rectangle diaganolly across the screen using a Thread.

Fields Summary
int
x
Where to draw the moving image: x coordinate
int
y
Where to draw the moving image: y coordinate
boolean
done
This is set true to stop the animation thread.
Constructors Summary
Methods Summary
public voidpaint(java.awt.Graphics g)
paint -- just draw our image at its current location

		g.setColor(Color.red);
		g.fillRect(x, y, 10, 10);
    
public synchronized voidrun()
Move the rectangle around the screen at a 45-degree angle. Called by the Thread when there is CPU time available for me.

		// Get the framesize
		int width = getSize().width;
		int height = getSize().height;

		// Start at a random location in it.
		x = (int)(Math.random() * width);
		y = (int)(Math.random() * height);

		while (!done) {
			// Obtain current size, in case resized.
			width = getSize().width;
			height = getSize().height;
			// Did we go off the deep end? :-)
			if (x++ >= width)
				x=0;	// return to shallow end 
			if (y++ >= height)
				y=0;
			repaint();		// Tell AWT to call our paint().
			try {
				Thread.sleep(250);
			} catch (InterruptedException e) {
				return;
			}
		}
	
public voidstart()
Called by the browser to start the page

		done = false;
		new Thread(this).start();
	
public voidstop()
Called by the browser when the user moves away from the page

		done = true;