1 import java.awt.*; 2 import java.applet.*; 3 4 public class mRectApplet extends Applet implements Runnable { 5 6 // Instance variables: 7 Thread thread; 8 mPoint object[] = new mPoint[2]; 9 10 // Class method: 11 public void initObjects() { 12 object[0] = new mRectangle(10,10,100,100); 13 object[0].setDelta(1,1); 14 object[0].setColor(Color.red); 15 16 object[1] = new mRectangle(200,10,100,100); 17 object[1].setDelta(-1,1); 18 object[1].setColor(Color.blue); 19 } 20 21 // Override java.applet.Applet.init: 22 public void init () { 23 initObjects(); 24 } 25 26 // Override java.applet.Applet.start: 27 public void start() { 28 if (thread == null) { 29 // Reinitialize: 30 initObjects(); 31 thread = new Thread(this); 32 thread.start(); 33 } 34 } 35 36 // Override java.applet.Applet.stop: 37 public void stop() { 38 if (thread != null) { 39 thread.stop(); 40 thread = null ; 41 } 42 } 43 44 // Implement java.lang.Runnable.run: 45 public void run() { 46 while(thread != null) { 47 // repaint() invokes update(): 48 repaint(); 49 try { 50 Thread.sleep(20); 51 } 52 catch(InterruptedException e) { 53 // Do nothing 54 }; 55 } 56 } 57 58 // Override java.awt.Component.paint: 59 public void paint (Graphics g) { 60 g.setColor(Color.black); 61 g.fillRect(0, 0, size().width, size().height); 62 for (int i=0; i<2; i++) 63 object[i].paint(g); 64 } 65 66 // Override java.awt.Component.update: 67 public void update(Graphics g) { 68 g.setColor(Color.black); 69 g.fillRect(0, 0, size().width, size().height); 70 for(int i=0; i<2; i++) 71 object[i].move(g); 72 } 73 74 } 75