1 /* 2 * File: MovablePolygon.java 3 * 4 * Movable polygon class for Java 1.1 5 * 6 * Copyright: Northeast Parallel Architectures Center 7 * 8 */ 9 10 import java.awt.*; 11 12 public class MovablePolygon extends DrawablePolygon { 13 14 // Displacement in the x and y directions: 15 private int dx, dy; 16 17 // MovablePolygon constructor #1: 18 public MovablePolygon() { 19 20 // invoke the no-argument constructor of the superclass: 21 super(); 22 23 dx = dy = 1; //default values 24 25 } 26 27 // MovablePolygon constructor #2: 28 public MovablePolygon( int[] xpoints, int[] ypoints, int npoints ) { 29 30 // invoke a constructor of the superclass: 31 super( xpoints, ypoints, npoints ); 32 33 dx = dy = 1; //default values 34 35 } 36 37 // MovablePolygon constructor #3: 38 public MovablePolygon( Point[] points, int npoints ) { 39 40 // invoke a constructor of the superclass: 41 super( points, npoints ); 42 43 dx = dy = 1; //default values 44 45 } 46 47 /* 48 * MovablePolygon constructor #4 49 * 50 * A regular n-gon with center (x,y) and radius r 51 * 52 */ 53 54 public MovablePolygon( int x, int y, int r, int n ) { 55 56 // invoke a constructor of the superclass: 57 super( x, y, r, n ); 58 59 dx = dy = 1; //default values 60 61 } 62 63 public void setDelta( int dx, int dy ) { 64 this.dx = dx; this.dy = dy; 65 } 66 67 public void move() { 68 // the translate method is inherited from Polygon: 69 this.translate( dx, dy ); 70 } 71 72 public void checkBounds( java.awt.Rectangle r ) { 73 74 java.awt.Rectangle bb = this.getBounds(); 75 76 int w = bb.width, h = bb.height; 77 int nx = bb.x + dx, ny = bb.y + dy; 78 79 if ( ( nx < r.x ) || ( nx + w > r.x + r.width ) ) dx *= -1; 80 if ( ( ny < r.y ) || ( ny + h > r.y + r.height ) ) dy *= -1; 81 82 } 83 84 }