/* * File: MovablePolygon.java * * Custom polygon class for Java 1.1 * * Copyright: Northeast Parallel Architectures Center * */ import java.awt.Polygon; import java.awt.Color; import java.awt.Point; import java.awt.Graphics; import java.awt.Component; /* * MovablePolygon inherits variables xpoints[], ypoints[], * npoints and bounds, and numerous methods from java.awt.Polygon * */ public class MovablePolygon extends Polygon implements Drawable, Movable { /* * Constructors * */ // MovablePolygon constructor #1: public MovablePolygon() { // invoke the no-argument constructor of the superclass: super(); } // MovablePolygon constructor #2 (for compatibility with Polygon): public MovablePolygon( int[] xpoints, int[] ypoints, int npoints ) { // invoke a constructor of the superclass: super( xpoints, ypoints, npoints ); } // MovablePolygon constructor #3: public MovablePolygon( Point[] points, int npoints ) { // invoke MovablePolygon constructor #1: this(); for ( int i = 0; i < npoints; i++ ) { this.addPoint( points[i] ); // TEST! } } // MovablePolygon constructor #4: // (a regular n-gon with center (x,y) and radius r) public MovablePolygon( int x, int y, int r, int n ) { // invoke MovablePolygon constructor #1: this(); // rotation angle: double theta = 2 * Math.PI/n; // initial point is at twelve o'clock: MovablePoint p = new MovablePoint( 0, -r ); this.addPoint( p ); for ( int i = 1; i < n; i++ ) { // rotate the previous point: p = p.rotate( theta ); this.addPoint( p ); } this.translate( x, y ); } /* * Implement the Drawable interface * */ // instance variable: private Color color; public Color getColor() { return color; } public void setColor( Color color ) { this.color = color; } public void draw( Graphics g ) { if ( this.color != null ) g.setColor( this.color ); g.drawPolygon( this ); } public void draw( Component c ) { this.draw( c.getGraphics() ); } public void fill( Graphics g ) { if ( this.color != null ) g.setColor( this.color ); g.fillPolygon( this ); } public void fill( Component c ) { this.fill( c.getGraphics() ); } /* * Implement the Movable interface * */ // Displacement in the x and y directions: private int dx = 1, dy = 1; public void setDelta( int dx, int dy ) { this.dx = dx; this.dy = dy; } public void move() { // the translate method is inherited from Polygon: this.translate( dx, dy ); } public void checkBounds( java.awt.Rectangle r ) { java.awt.Rectangle bb = this.getBounds(); int w = bb.width, h = bb.height; int nx = bb.x + dx, ny = bb.y + dy; if ( ( nx < r.x ) || ( nx + w > r.x + r.width ) ) dx *= -1; if ( ( ny < r.y ) || ( ny + h > r.y + r.height ) ) dy *= -1; } /* * Note: All rotation methods mutate the current polygon! * */ // rotate this polygon about the top lefthand corner // of its bounding box: public MovablePolygon rotate( double theta ) { // return the rotated polygon as a side effect: return this.rotate( theta, new Point( this.getBounds().x, this.getBounds().y ) ); } // rotate this polygon about the center of its bounding box: public MovablePolygon centerRotate( double theta ) { // compute the center of this polygon: java.awt.Rectangle r = this.getBounds(); int x0 = r.x + r.width/2; int y0 = r.y + r.height/2; // return the rotated polygon as a side effect: return this.rotate( theta, new Point( x0, y0 ) ); } // rotate this polygon about an arbitrary point: public MovablePolygon rotate( double theta, Point p ) { // should this by MovablePoint? // things will break! // local variables: int x, y; MovablePoint q; // translate to the origin: this.translate( -p.x, -p.y ); // rotate each vertex of this polygon: for ( int i = 0; i < this.npoints; i++ ) { x = this.xpoints[i]; y = this.ypoints[i]; q = new MovablePoint( x, y ); q.rotate( theta ); this.xpoints[i] = q.x; this.ypoints[i] = q.y; } // the bounding box is no longer valid: this.bounds = null; // translate back to original coordinates: this.translate( p.x, p.y ); // return the rotated polygon as a side effect: return this; } /* * Other instance methods * */ // overload Polygon.addPoint( int, int ): public void addPoint( Point p ) { // should this be MovablePoint? super.addPoint( p.x, p.y ); // things will break! } // get all vertices of this polygon: public Point[] getPoints() { Point[] points = new Point[ npoints ]; for ( int i = 0; i < npoints; i++ ) { points[i] = new Point( xpoints[i], ypoints[i] ); } return points; } }