1 /* 2 * File: DrawablePolygonTest1.java 3 * 4 * Rotate 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 DrawablePolygonTest1 extends Applet { 16 17 // create a reference to an octagon: 18 private Octagon octagon; 19 20 // create references to eight points: 21 private Point p1, p2, p3, p4, p5, p6, p7, p8; 22 23 // create a reference to the point of rotation: 24 private Point rotationPoint; 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 // fix the point of rotation: 42 java.awt.Rectangle r = octagon.getBounds(); 43 rotationPoint = new Point( r.x, r.y ); 44 45 } 46 47 public void paint( Graphics g ) { 48 49 // make a copy of the octagon: 50 Octagon octagon = this.octagon; 51 52 // fill the octagon: 53 octagon.setColor( Color.red ); 54 octagon.fill( g ); 55 56 // rotate and fill the octagon: 57 octagon.rotate( Math.PI/2, rotationPoint ).fill( g ); 58 59 // change the color, rotate, and fill the octagon: 60 octagon.setColor( Color.yellow ); 61 octagon.rotate( Math.PI/2, rotationPoint ).fill( g ); 62 octagon.rotate( Math.PI/2, rotationPoint ).fill( g ); 63 64 } 65 66 }