package cse1030; import java.awt.Color; /** * An abstract class for representing simple geometric shapes. A shape * is a composition of a position (the center of the shape) and an * aggregation of a color. * * Shape has an abstract method draw that subclasses * must implement that draws a shape filled with the color of the * shape. * * @author CSE1030Z * */ public abstract class Shape { private Point2D position; private Color color; /** * Create a shape with position (0., 0.) and color blue. */ public Shape() { this.position = new Point2D(); this.color = Color.BLUE; } /** * Create a shape with position (x, y) and the given color. * * @param x the x coordinate of the position of the shape * @param y the y coordinate of the position of the shape * @param color the color of the shape */ public Shape(double x, double y, Color color) { this.position = new Point2D(x, y); this.color = color; } /** * Create a shape with the given position and color. * * @param position the position of the shape * @param color the color of the shape */ public Shape(Point2D position, Color color) { this.position = new Point2D(position); this.color = color; } /** * Get the position of the shape. * * @return the position of the shape */ public final Point2D getPosition() { return new Point2D(this.position); } /** * Set the position of the shape. * * @param newPosition the new position of the shape. */ public final void setPosition(Point2D newPosition) { this.position = new Point2D(newPosition); } /** * Set the position of the shape. * * @param x the x coordinate of the position of the shape * @param y the y coordinate of the position of the shape */ public final void setPosition(double x, double y ) { this.position.setX(x); this.position.setY(y); } /** * Get the color of the shape. * * @return the color of the shape */ public final Color getColor() { return this.color; } /** * Set the color of the shape. * * @param newColor the new color of the shape */ public final void setColor(Color newColor) { this.color = newColor; } /** * Returns a string representation of the shape. * *

* The string returned has the form: * *

* position: (x, y), color: (r, g, b) where x * and y are the coordinates of the position, and * r, g, and b are the * red, green, and blue components of the color in the range 0-255. * *

* Subclasses might add additional information to the end of the string. * * @return a string representation of the shape */ @Override public String toString() { Color c = this.getColor(); String cString = String.format("(%d, %d, %d)", c.getRed(), c.getGreen(), c.getBlue()); return "position: " + this.getPosition() + ", color: " + cString; } /** * Draws the shape to StdDraw. */ public abstract void draw(); }