1  /*
  2   *  File:  DrawablePolygonTest2.java
  3   *
  4   *  Animate a regular octagon
  5   *
  6   *  Copyright:  Northeast Parallel Architectures Center
  7   *  
  8   */
  9  
 10  import java.applet.Applet;
 11  import java.awt.Graphics;
 12  import java.awt.Color;
 13  import java.awt.Point;
 14  
 15  public class DrawablePolygonTest2 extends Applet implements Runnable {
 16  
 17     // create a reference to a thread:
 18     private Thread thread;
 19   
 20     // create a reference to an octagon:
 21     private Octagon octagon;
 22     
 23     // create references to eight points:
 24     private Point p1, p2, p3, p4, p5, p6, p7, p8;
 25     
 26     // create a reference to the point of rotation:
 27     private Point rotationPoint;
 28     
 29     public void init() {
 30     
 31        // instantiate eight points (the vertices of a regular octagon):
 32        p1 = new Point( 269, 225 );
 33        p2 = new Point( 331, 225 );
 34        p3 = new Point( 375, 269 );
 35        p4 = new Point( 375, 331 );
 36        p5 = new Point( 331, 375 );
 37        p6 = new Point( 269, 375 );
 38        p7 = new Point( 225, 331 );
 39        p8 = new Point( 225, 269 );
 40        
 41        // instantiate a regular octagon:
 42        octagon = new Octagon( p1, p2, p3, p4, p5, p6, p7, p8 );
 43        
 44        // fix the point of rotation:
 45        java.awt.Rectangle r = octagon.getBounds();
 46        rotationPoint = new Point( r.x, r.y );
 47        
 48     }
 49     
 50     // Override java.applet.Applet.start():
 51     public void start() {
 52        if ( thread == null ) {
 53          // Start a new thread:
 54          thread = new Thread( this );
 55          thread.start();
 56        }
 57     }
 58   
 59     // Override java.applet.Applet.stop():
 60     public void stop() {
 61        if ( thread != null ) {
 62          thread.stop();
 63          thread = null;
 64        }
 65     }
 66   
 67     // Implement java.lang.Runnable.run():
 68     public void run() {
 69        while ( thread != null ) {
 70           repaint();
 71           try {
 72              // sleep for one second:
 73              Thread.sleep( 1000 );
 74           } catch ( InterruptedException e ) {
 75              // Do nothing   
 76           };
 77        }
 78     }
 79  
 80     public void paint( Graphics g ) {
 81  
 82        // make a copy of the octagon:
 83        Octagon octagon = this.octagon;
 84        
 85        // erase the current octagon:
 86        octagon.setColor( getBackground() );
 87        octagon.fill( g );
 88        
 89        // rotate and fill a new octagon:
 90        octagon.setColor( Color.green );
 91        octagon.rotate( Math.PI/16, rotationPoint ).fill( g );
 92  
 93     }
 94  
 95  }