package cse1030; import java.awt.Color; import princeton.introcs.StdDraw; /** * A circle shape. A circle has a radius that is always greater than zero. * * @author CSE1030Z * */ public class Circle extends Shape { private double radius; /** * Create a circle with position (0, 0), color blue, and radius 1. */ public Circle() { super(); this.radius = 1.; } /** * Create a circle with position (x, y), and the given radius and color. * * @param x the x coordinate of the position of the circle * @param y the y coordinate of the position of the circe * @param radius the radius of the circle * @param color the color of the circle * @throws IllegalArgumentException if the radius is zero or negative */ public Circle(double x, double y, double radius, Color color) { super(x, y, color); this.setRadius(radius); } /** * Create a circle with the given position, radius, and color. * * @param position the position of the circle * @param radius the radius of the circle * @param color the color of the circle * @throws IllegalArgumentException if the radius is zero or negative */ public Circle(Point2D position, double radius, Color color) { super(position, color); this.setRadius(radius); } /** * Get the radius of the circle. * * @return the radius of the circle */ final public double getRadius() { return this.radius; } /** * Set the radius of the circle. The radius must be greater than zero. * * @param newRadius the new radius of the circle * @throws IllegalArgumentException if the radius is zero or negative */ final public void setRadius(double newRadius) { if (newRadius <= 0.) { throw new IllegalArgumentException("radius is zero or negative"); } this.radius = newRadius; } /** * Returns a string representation of the circle. * *

* The string returned has the form: * *

* position: (x, y), color: (r, g, b), radius: R where x * and y are the coordinates of the position, * r, g, and b are the * red, green, and blue components of the color in the range 0-255, * and R is the radius. * * * @return a string representation of the circle */ @Override public String toString() { return super.toString() + ", radius: " + this.getRadius(); } /** * Draws a filled circle to StdDraw using the * position, radius, and color of this circle. */ @Override public void draw() { StdDraw.setPenColor(this.getColor()); StdDraw.filledCircle(this.getPosition().getX(), this.getPosition().getY(), this.radius); } }