/* File: mRectApplet.java */ import java.awt.*; import java.applet.*; public class mRectApplet extends Applet implements Runnable { // Instance variables: private Thread thread; private mPoint object[] = new mPoint[2]; private int x[] = { 10, 200 }; // initial x coordinates private int y[] = { 10, 10 }; // initial y coordinates // Override java.applet.Applet.init: public void init () { setBackground( Color.black ); object[0] = new mRectangle( x[0], y[0], 100, 100 ); object[0].setDelta( 1, 1 ); object[0].setColor( Color.red ); object[1] = new mRectangle( x[1], y[1], 100, 100 ); object[1].setDelta( -1, 1 ); object[1].setColor( Color.blue ); } // Override java.applet.Applet.start: public void start() { if ( thread == null ) { // Reinitialize (x,y) coordinates: object[0].setPoint( x[0], y[0] ); object[1].setPoint( x[1], y[1] ); // Start a new thread: thread = new Thread( this ); thread.start(); } } // Override java.applet.Applet.stop: public void stop() { if ( thread != null ) { thread.stop(); thread = null ; } } // Implement java.lang.Runnable.run: public void run() { while ( thread != null ) { repaint(); try { Thread.sleep(20); } catch ( InterruptedException e ) { }; // Do nothing } } // Override java.awt.Component.paint: public void paint( Graphics g ) { // Move each object and then paint it: for ( int i = 0; i < object.length; i++ ) { object[i].move(); object[i].paint(g); } } }