import java.awt.Color; import java.awt.Point; import java.io.Serializable; /** * * Shape interface specifying general operations an any geometric shape. * * @author Gene Fisher (gfisher@calpoly.edu) * */ public abstract class Shape implements Comparable, Serializable { /** The fill color */ private Color color; /** True if this shape is filled */ private boolean filled; /** True if this shape is selected. */ public boolean selected = false; /** The awt.Shape that corresponds to this' current value. */ public java.awt.Shape jshape; /** * Construct a shape with the given color and fill. */ public Shape(Color color, boolean filled) { this.color = color; this.filled = filled; } /** * Calculate and return the area of the shape. */ abstract public double getArea(); /** * Return the java.awt.Color of the shape. */ public Color getColor() { return color; } /** * Set the java.awt.Color of the shape. */ public void setColor(Color color) { this.color = color; } /** * Return the filled state of the shape. */ public boolean getFilled() { return filled; } /** * Set the filled state of the shape. */ public void setFilled(boolean filled) { this.filled = filled; } /** * Move the shape by the x and y amounts specified in the given Point. */ public abstract void move(Point delta); /** * Return true if (1) other is not null; (2) this and other are the same * type; (3) this and other have the same values for color and fill. * Return false otherwise. */ public boolean equals(Object other) { if (other == null) { return false; } if (getClass() != other.getClass()) { return false; } if (!color.equals(((Shape)other).color)) { return false; } if (filled != ((Shape)other).filled) { return false; } return true; } /** * Compare this and the given shape by their class names, and if the same * class, by their areas. For class names, the comparison is alphabetic, * e.g., "Circle" is less than "ConvexPolygon". For areas, the comparison * is numeric, on the return value of getArea(). */ public int compareTo(Shape shape) { int order = getClass().getName().compareTo(shape.getClass().getName()); if (order != 0) { return order; } double a = getArea(); double b = shape.getArea(); return (new Double(a)).compareTo(new Double(b)); /* * Alternative hand-built code, instead of calling Double.compareTo: * if (a < b) { return -1; } else if (a > b) { return 1; } return 0; */ } /* * Return the Java AWT representation of this shape, for use in the * selection intersection computation done by WorkSpace.getSelectionAt. */ public abstract java.awt.Shape getAwtRep(int canvasHeight); }