1  /*
  2   *  File:  DrawablePolygon.java
  3   *
  4   *  Custom polygon class for Java 1.1
  5   *
  6   *  Copyright:  Northeast Parallel Architectures Center
  7   *  
  8   */
  9  
 10  import java.awt.Polygon;
 11  import java.awt.Color;
 12  import java.awt.Point;
 13  import java.awt.Graphics;
 14  import java.awt.Component;
 15  
 16  /*
 17   *  DrawablePolygon inherits variables xpoints[], ypoints[], 
 18   *  npoints and bounds, and numerous methods from Polygon
 19   *  
 20   */
 21  
 22  public class DrawablePolygon extends Polygon implements Drawable {
 23  
 24     // instance variable:
 25     private Color color;
 26  
 27     // DrawablePolygon constructor #1:
 28     public DrawablePolygon() {
 29  
 30        // invoke the no-argument constructor of the superclass:
 31        super();
 32  
 33     }
 34  
 35     // DrawablePolygon constructor #2 (for compatibility with Polygon):
 36     public DrawablePolygon( int[] xpoints, int[] ypoints, int npoints ) {
 37         
 38        // invoke a constructor of the superclass:
 39        super( xpoints, ypoints, npoints );
 40        
 41     }
 42  
 43     // DrawablePolygon constructor #3:
 44     public DrawablePolygon( Point[] points, int npoints ) {
 45        
 46        // invoke DrawablePolygon constructor #1:
 47        this();
 48  
 49        for ( int i = 0; i < npoints; i++ ) {
 50           this.addPoint( points[i] );        // TEST!
 51        }
 52        
 53     }
 54  
 55     /*
 56      *  DrawablePolygon constructor #4
 57      *
 58      *  A regular n-gon with center (x,y) and radius r
 59      *
 60      */
 61      
 62     public DrawablePolygon( int x, int y, int r, int n ) {
 63         
 64        // invoke DrawablePolygon constructor #1:
 65        this();
 66        
 67        // rotation angle:
 68        double theta = 2 * Math.PI/n;
 69        
 70        // initial point is at twelve o'clock:
 71        DrawablePoint p = new DrawablePoint( 0, -r );
 72        this.addPoint( p );
 73        for ( int i = 1; i < n; i++ ) {
 74           // rotate the previous point:
 75           p = p.rotate( theta );
 76           this.addPoint( p );
 77        }
 78        this.translate( x, y );
 79        
 80     }
 81  
 82     // implement the methods of the Drawable interface:
 83     public Color getColor() { return color; }
 84     public void setColor( Color color ) {
 85        this.color = color;
 86     }
 87     public void draw( Graphics g ) {
 88        if ( this.color != null ) g.setColor( this.color );
 89        g.drawPolygon( this );
 90     }
 91     public void draw( Component c ) {
 92        this.draw( c.getGraphics() );
 93     }
 94     public void fill( Graphics g ) {
 95        if ( this.color != null ) g.setColor( this.color );
 96        g.fillPolygon( this );
 97     }
 98     public void fill( Component c ) {
 99        this.fill( c.getGraphics() );
100     }
101     
102     /*
103      *  Note:  All rotation methods mutate the current polygon!
104      *
105      */
106      
107     // rotate this polygon about the top lefthand corner
108     // of its bounding box:
109     public DrawablePolygon rotate( double theta ) {
110     
111        // return the rotated polygon as a side effect:
112        return this.rotate( theta, new Point( this.getBounds().x,
113                                              this.getBounds().y  ) );
114        
115     }
116     
117     // rotate this polygon about the center of its bounding box:
118     public DrawablePolygon centerRotate( double theta ) {
119     
120        // compute the center of this polygon:
121        java.awt.Rectangle r = this.getBounds();
122        int x0 = r.x + r.width/2;
123        int y0 = r.y + r.height/2;
124  
125        // return the rotated polygon as a side effect:
126        return this.rotate( theta, new Point( x0, y0 ) );
127        
128     }
129     
130     // rotate this polygon about an arbitrary point:
131     public DrawablePolygon rotate( double theta, Point p ) {  // should this by DrawablePoint?
132                                                               // things will break!
133        // local variables:
134        int x, y; DrawablePoint q;
135  
136        // translate to the origin:
137        this.translate( -p.x, -p.y );
138        
139        // rotate each vertex of this polygon:
140        for ( int i = 0; i < this.npoints; i++ ) {
141           x = this.xpoints[i]; y = this.ypoints[i];
142           q = new DrawablePoint( x, y );
143           q.rotate( theta );
144           this.xpoints[i] = q.x;
145           this.ypoints[i] = q.y;
146        }
147        
148        // the bounding box is no longer valid:
149        this.bounds = null;
150        
151        // translate back to original coordinates:
152        this.translate( p.x, p.y );
153  
154        // return the rotated polygon as a side effect:
155        return this;
156        
157     }
158  
159     // overload Polygon.addPoint( int, int ):
160     public void addPoint( Point p ) {  // should this be DrawablePoint?
161        super.addPoint( p.x, p.y );     // things will break!
162     }
163     
164     // get all vertices of this polygon:
165     public Point[] getPoints() {
166     
167        Point[] points = new Point[ npoints ];
168        for ( int i = 0; i < npoints; i++ ) {
169           points[i] = new Point( xpoints[i], ypoints[i] );
170        }
171        return points;
172        
173     }
174     
175  }