/* * File: MovablePoint.java * * Custom point class for Java 1.1 * * Copyright: Northeast Parallel Architectures Center * */ import java.awt.Point; import java.awt.Color; import java.awt.Graphics; import java.awt.Component; /* * MovablePoint inherits variables x and y from Point * */ public class MovablePoint extends Point implements Drawable, Movable { /* * Constructors * */ // MovablePoint constructor #1: public MovablePoint() { // invoke the no-argument constructor of the superclass: super(); } // MovablePoint constructor #2: public MovablePoint( int x, int y ) { // invoke a constructor of the superclass: super( x, y ); } // MovablePoint constructor #3: public MovablePoint( Point p ) { // invoke a constructor of the superclass: super( p ); } /* * 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.drawLine( x, y, x, y ); } public void draw( Component c ) { this.draw( c.getGraphics() ); } public void fill( Graphics g ) { this.draw( g ); } 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 Point: this.translate( dx, dy ); } public void checkBounds( java.awt.Rectangle r ) { if ( ( x < r.x ) || ( x > r.x + r.width ) ) dx *= -1; if ( ( y < r.y ) || ( y > r.y + r.height ) ) dy *= -1; } /* * The rotate method mutates the current point! * */ // rotate a point about the origin: public MovablePoint rotate( double theta ) { final double cos_theta = Math.cos( theta ); final double sin_theta = Math.sin( theta ); int new_x = ( int ) Math.round( x * cos_theta - y * sin_theta ); int new_y = ( int ) Math.round( x * sin_theta + y * cos_theta ); this.move( new_x, new_y ); // return the rotated point as a side effect: return this; } }