package cse1030.test2; import cse1030.drawing.Point2D; /** * An abstract base class for 2D shapes. Each shape has a rectangular * bounding box that fits tightly around the shape. The position of * the shape is given by the center point of the bounding box. * The position of the shape cannot be changed after the shape is * created. * * @author CSE1030_F13_14 * */ public abstract class Shape { /** * The width of the bounding box. */ protected double width; /** * The height of the bounding box. */ protected double height; /** * The position of the center point of the bounding box. */ protected Point2D position; /** * Constructs a shape having a bounding box of width * and height equal to zero, and center position (0, 0) */ protected Shape() { this.position = new Point2D(); this.width = 0.; this.height = 0.; } /** * Constructs a shape having a bounding box with width w, * height h, and center position p. * * @param w * the width of the shape's bounding box * @param h * the height of the shape's bounding box * @param p * the position of the center of the bounding box * @throws IllegalArgumentException * if w < 0 * @throws IllegalArgumentException * if h < 0 */ protected Shape(double w, double h, Point2D p) { if (w < 0) { throw new IllegalArgumentException("width is less than or equal to 0"); } if (h < 0) { throw new IllegalArgumentException("height is less than or equal to 0"); } this.width = w; this.height = h; this.position = new Point2D(p); } /** * Get the position of the center of the bounding box for the shape. * * @return the position of the center of the bounding box */ public final Point2D getPosition() { return new Point2D(this.position); } /** * Get the width of the bounding box for the shape. * @return the width of the bounding box for the shape * */ public final double getWidth() { return this.width; } /** * Get the height of the bounding box for the shape. * @return the height of the bounding box for the shape * */ public final double getHeight() { return this.height; } /** * Get the area of the shape. Shape provides a default * implementation that returns the area of the shape's bounding * box. Subclasses should override this method to provide the * area of the actual shape. * * @return the area of the shape */ public double getArea() { return this.height * this.width; } }