import java.awt.*; import java.applet.*; public class mRectApplet extends Applet implements Runnable { // Instance variables: Thread thread; mPoint object[] = new mPoint[2]; // Class method: public void initObjects() { object[0] = new mRectangle(10,10,100,100); object[0].setDelta(1,1); object[0].setColor(Color.red); object[1] = new mRectangle(200,10,100,100); object[1].setDelta(-1,1); object[1].setColor(Color.blue); } // Override java.applet.Applet.init: public void init () { initObjects(); } // Override java.applet.Applet.start: public void start() { if (thread == null) { // Reinitialize: initObjects(); 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() invokes update(): repaint(); try { Thread.sleep(20); } catch(InterruptedException e) { // Do nothing }; } } // Override java.awt.Component.paint: public void paint (Graphics g) { g.setColor(Color.black); g.fillRect(0, 0, size().width, size().height); for (int i=0; i<2; i++) object[i].paint(g); } // Override java.awt.Component.update: public void update(Graphics g) { g.setColor(Color.black); g.fillRect(0, 0, size().width, size().height); for(int i=0; i<2; i++) object[i].move(g); } }