/* File: mStrApplet.java */ import java.awt.*; import java.applet.*; public class mStrApplet extends Applet implements Runnable { // Instance variables: private Thread thread; private mString object[] = new mString[4]; private int x = 150, y = 150; // initial (x,y) coordinates // Override java.applet.Applet.init: public void init () { setBackground( Color.black ); Font f = new Font( "SansSerif", Font.BOLD, 36 ); object[0] = new mString( x, y, "NPAC" ); object[0].setDelta( 0, 1 ); object[0].setColor( Color.red ); object[0].setFont(f); object[1] = new mString( x, y, "NPAC" ); object[1].setDelta( 0, -1 ); object[1].setColor( Color.red ); object[1].setFont(f); object[2] = new mString( x, y, "NPAC" ); object[2].setDelta( 1, 0 ); object[2].setColor( Color.blue ); object[2].setFont(f); object[3] = new mString( x, y, "NPAC" ); object[3].setDelta( -1, 0 ); object[3].setColor( Color.blue ); object[3].setFont(f); } // Override java.applet.Applet.start: public void start() { if ( thread == null ) { // Reinitialize (x,y) coordinates: for ( int i = 0; i < object.length; i++ ) { object[i].setPoint( x, y ); } 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.update: public void update( Graphics g ) { // Get the size of this applet and color it black: g.setColor( Color.black ); g.fillRect( 0, 0, getSize().width, getSize().height ); // Move each object: for ( int i = 0; i < object.length; i++ ) { object[i].move(); } paint(g); } // Override java.awt.Component.paint: public void paint( Graphics g ) { // Paint each object: for ( int i = 0; i < object.length; i++ ) { object[i].paint(g); } } }