1 /* File: mStrApplet.java */ 2 3 import java.awt.*; 4 import java.applet.*; 5 6 public class mStrApplet extends Applet implements Runnable { 7 8 // Instance variables: 9 private Thread thread; 10 private mString object[] = new mString[4]; 11 private int x = 150, y = 150; // initial (x,y) coordinates 12 13 // Override java.applet.Applet.init: 14 public void init () { 15 setBackground( Color.black ); 16 17 Font f = new Font( "SansSerif", Font.BOLD, 36 ); 18 19 object[0] = new mString( x, y, "NPAC" ); 20 object[0].setDelta( 0, 1 ); 21 object[0].setColor( Color.red ); 22 object[0].setFont(f); 23 24 object[1] = new mString( x, y, "NPAC" ); 25 object[1].setDelta( 0, -1 ); 26 object[1].setColor( Color.red ); 27 object[1].setFont(f); 28 29 object[2] = new mString( x, y, "NPAC" ); 30 object[2].setDelta( 1, 0 ); 31 object[2].setColor( Color.blue ); 32 object[2].setFont(f); 33 34 object[3] = new mString( x, y, "NPAC" ); 35 object[3].setDelta( -1, 0 ); 36 object[3].setColor( Color.blue ); 37 object[3].setFont(f); 38 } 39 40 // Override java.applet.Applet.start: 41 public void start() { 42 if ( thread == null ) { 43 // Reinitialize (x,y) coordinates: 44 for ( int i = 0; i < object.length; i++ ) { 45 object[i].setPoint( x, y ); 46 } 47 thread = new Thread( this ); 48 thread.start(); 49 } 50 } 51 52 // Override java.applet.Applet.stop: 53 public void stop() { 54 if ( thread != null ) { 55 thread.stop(); 56 thread = null ; 57 } 58 } 59 60 // Implement java.lang.Runnable.run: 61 public void run() { 62 while ( thread != null ) { 63 repaint(); 64 try { 65 Thread.sleep(20); 66 } catch ( InterruptedException e ) { 67 // Do nothing 68 }; 69 } 70 } 71 72 // Override java.awt.Component.update: 73 public void update( Graphics g ) { 74 // Get the size of this applet and color it black: 75 g.setColor( Color.black ); 76 g.fillRect( 0, 0, getSize().width, getSize().height ); 77 // Move each object: 78 for ( int i = 0; i < object.length; i++ ) { 79 object[i].move(); 80 } 81 paint(g); 82 } 83 84 // Override java.awt.Component.paint: 85 public void paint( Graphics g ) { 86 // Paint each object: 87 for ( int i = 0; i < object.length; i++ ) { 88 object[i].paint(g); 89 } 90 } 91 92 } 93