/* Copyright 2001 Michael R. Wick */ package Shapes; import java.awt.*; public abstract class Shape { protected Point theCenter; protected Color theColor; public Shape(Point p, Color c){ theCenter = new Point(p); theColor = c; } /* C++-like copy constructor used during Kaleidoscope display */ public Shape( Shape orig ){ theCenter = new Point(orig.theCenter) ; theColor = orig.theColor; } public abstract Shape copy(); /* Helper function to convert degrees to radians. */ protected double degreesToRadians( int degrees ){ return (Math.PI / 180.0 * degrees); } /* Helper function to calculate the distance between two points. */ protected static int distance(Point p1, Point p2){ return( (int) Math.sqrt( (p2.x - p1.x) * (p2.x - p1.x) + (p2.y - p1.y) * (p2.y - p1.y) )); } public void translateTo(Point p){ translate(p.x - theCenter.x, p.y - theCenter.y); } public void flipHorizontalAbout(Point p){ Point ctr = new Point( theCenter ); ctr.x = 2 * p.x - ctr.x; theCenter = ctr; flipBorderHorizontalAbout(p); } protected abstract void flipBorderHorizontalAbout(Point p); public void translate(int dx, int dy){ Point ctr = new Point(theCenter); ctr.x += dx; ctr.y += dy; theCenter = ctr; translateBorder(dx, dy); } protected abstract void translateBorder(int dx, int dy); public void flipVerticalAbout(Point p){ Point ctr = new Point(theCenter); ctr.y = 2 * p.y - ctr.y; theCenter = ctr; flipBorderVerticalAbout(p); } protected abstract void flipBorderVerticalAbout(Point p); public void flipDiagonalAbout(Point p){ flipVerticalAbout(p); flipHorizontalAbout(p); } public void draw(Graphics g){ g.setColor(theColor); drawBorder(g); } public abstract void drawBorder(Graphics g); protected Point rotateAbout(Point p, Point data, double radians){ double cos = Math.cos(radians), sin = Math.sin(radians); int x, y; x = data.x; y = data.y; return ( new Point((int)(x*cos - y*sin + p.x*(1 - cos) + p.y*sin), (int)(x*sin + y*cos + p.y*(1 - cos) - p.x*sin)) ); } protected static Point translate(Point data, int dx, int dy){ return (new Point( data.x + dx, data.y + dy ) ); } public void rotateAbout(Point p, int degrees){ theCenter = rotateAbout(p, theCenter, degreesToRadians(degrees)); rotateBorderAbout(p, degrees); } protected abstract void rotateBorderAbout(Point p, int degrees); public void setColor(Color c){ theColor = c; } public Color getColor(){ return theColor; } public Point getCenter(){ return new Point(theCenter); } }