1 /* 2 * File: DrawablePoint.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 * DrawablePoint inherits variables x and y from Point 17 * 18 */ 19 20 public class DrawablePoint extends Point implements Drawable { 21 22 // instance variable: 23 private Color color; 24 25 // DrawablePoint constructor #1: 26 public DrawablePoint() { 27 28 // invoke the no-argument constructor of the superclass: 29 super(); 30 31 } 32 33 // DrawablePoint constructor #2: 34 public DrawablePoint( int x, int y ) { 35 36 // invoke a constructor of the superclass: 37 super( x, y ); 38 39 } 40 41 // DrawablePoint constructor #3: 42 public DrawablePoint( Point p ) { 43 44 // invoke a constructor of the superclass: 45 super( p ); 46 47 } 48 49 // implement the methods of the Drawable interface: 50 public Color getColor() { return color; } 51 public void setColor( Color color ) { 52 this.color = color; 53 } 54 public void draw( Graphics g ) { 55 if ( this.color != null ) g.setColor( this.color ); 56 g.drawLine( x, y, x, y ); 57 } 58 public void draw( Component c ) { 59 this.draw( c.getGraphics() ); 60 } 61 public void fill( Graphics g ) { 62 this.draw( g ); 63 } 64 public void fill( Component c ) { 65 this.fill( c.getGraphics() ); 66 } 67 68 // rotate a point about the origin: 69 public DrawablePoint rotate( double theta ) { 70 71 final double cos_theta = Math.cos( theta ); 72 final double sin_theta = Math.sin( theta ); 73 74 int new_x = ( int ) Math.round( x * cos_theta - y * sin_theta ); 75 int new_y = ( int ) Math.round( x * sin_theta + y * cos_theta ); 76 77 this.move( new_x, new_y ); 78 79 // return the rotated point as a side effect: 80 return this; 81 82 } 83 84 }