1 /* 2 * File: MyPolygonTest2.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 MyPolygonTest2 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 public void init() { 27 28 // instantiate eight points (the vertices of a regular octagon): 29 p1 = new Point( 269, 225 ); 30 p2 = new Point( 331, 225 ); 31 p3 = new Point( 375, 269 ); 32 p4 = new Point( 375, 331 ); 33 p5 = new Point( 331, 375 ); 34 p6 = new Point( 269, 375 ); 35 p7 = new Point( 225, 331 ); 36 p8 = new Point( 225, 269 ); 37 38 // instantiate a regular octagon: 39 octagon = new Octagon( p1, p2, p3, p4, p5, p6, p7, p8 ); 40 41 } 42 43 // Override java.applet.Applet.start: 44 public void start() { 45 if ( thread == null ) { 46 // Start a new thread: 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( 1000 ); 66 } catch ( InterruptedException e ) { 67 // Do nothing 68 }; 69 } 70 } 71 72 public void paint( Graphics g ) { 73 74 // erase the current octagon: 75 g.setColor( getBackground() ); 76 octagon.fill( g ); 77 78 // rotate and fill a new octagon: 79 g.setColor( Color.green ); 80 octagon.rotate( Math.PI/16 ).fill( g ); 81 82 } 83 84 } 85 86