1 /* 2 * File: MovablePoint.java 3 * 4 * Custom point class for Java 1.1 5 * 6 * Copyright: Northeast Parallel Architectures Center 7 * 8 */ 9 10 import java.awt.Point; 11 import java.awt.Color; 12 import java.awt.Graphics; 13 import java.awt.Component; 14 15 /* 16 * MovablePoint inherits variables x and y from Point 17 * 18 */ 19 20 public class MovablePoint extends Point implements Drawable, Movable { 21 22 /* 23 * Constructors 24 * 25 */ 26 27 // MovablePoint constructor #1: 28 public MovablePoint() { 29 30 // invoke the no-argument constructor of the superclass: 31 super(); 32 33 } 34 35 // MovablePoint constructor #2: 36 public MovablePoint( int x, int y ) { 37 38 // invoke a constructor of the superclass: 39 super( x, y ); 40 41 } 42 43 // MovablePoint constructor #3: 44 public MovablePoint( Point p ) { 45 46 // invoke a constructor of the superclass: 47 super( p ); 48 49 } 50 51 /* 52 * Implement the Drawable interface 53 * 54 */ 55 56 // instance variable: 57 private Color color; 58 59 public Color getColor() { return color; } 60 public void setColor( Color color ) { 61 this.color = color; 62 } 63 64 public void draw( Graphics g ) { 65 if ( this.color != null ) g.setColor( this.color ); 66 g.drawLine( x, y, x, y ); 67 } 68 public void draw( Component c ) { 69 this.draw( c.getGraphics() ); 70 } 71 72 public void fill( Graphics g ) { 73 this.draw( g ); 74 } 75 public void fill( Component c ) { 76 this.fill( c.getGraphics() ); 77 } 78 79 /* 80 * Implement the Movable interface 81 * 82 */ 83 84 // Displacement in the x and y directions: 85 private int dx = 1, dy = 1; 86 87 public void setDelta( int dx, int dy ) { 88 this.dx = dx; this.dy = dy; 89 } 90 91 public void move() { 92 // the translate method is inherited from Point: 93 this.translate( dx, dy ); 94 } 95 96 public void checkBounds( java.awt.Rectangle r ) { 97 98 if ( ( x < r.x ) || ( x > r.x + r.width ) ) dx *= -1; 99 if ( ( y < r.y ) || ( y > r.y + r.height ) ) dy *= -1; 100 101 } 102 103 /* 104 * The rotate method mutates the current point! 105 * 106 */ 107 108 // rotate a point about the origin: 109 public MovablePoint rotate( double theta ) { 110 111 final double cos_theta = Math.cos( theta ); 112 final double sin_theta = Math.sin( theta ); 113 114 int new_x = ( int ) Math.round( x * cos_theta - y * sin_theta ); 115 int new_y = ( int ) Math.round( x * sin_theta + y * cos_theta ); 116 117 this.move( new_x, new_y ); 118 119 // return the rotated point as a side effect: 120 return this; 121 122 } 123 124 }