package cse1030; import java.awt.Color; import princeton.introcs.StdDraw; /** * An ellipse shape. An ellipse has a width and height, both of which are always greater than zero. * * @author CSE1030Z * */ public class Ellipse extends Shape { private double width; private double height; /** * Create an ellipse with position (0, 0), color blue, width 1, and height 0.5 */ public Ellipse() { super(); this.width = 1.0; this.height = 0.5; } /** * Create an ellipse with position (x, y), and the given width, height, and color. * * @param x the x coordinate of the position of the ellipse * @param y the x coordinate of the position of the ellipse * @param width the radius of the ellipse * @param height the radius of the ellipse * @param color the color of the ellipse * @throws IllegalArgumentException if either the width or height is zero or negative */ public Ellipse(double x, double y, double width, double height, Color color) { super(x, y, color); this.setWidth(width); this.setHeight(height); } /** * Create an ellipse with the given position, width, height, and color. * * @param position the position of the ellipse * @param width the radius of the ellipse * @param height the radius of the ellipse * @param color the color of the ellipse * @throws IllegalArgumentException if the width or height is zero or negative */ public Ellipse(Point2D position, double width, double height, Color color) { super(position, color); this.setWidth(width); this.setHeight(height); } /** * Get the width of the ellipse. * * @return the width of the ellipse */ public double getWidth() { return this.width; } /** * Set the width of the ellipse. * * @param newWidth * the new width of the ellipse * @throws IllegalArgumentException if the new width is zero or negative */ public void setWidth(double newWidth) { if (newWidth <= 0.0) { throw new IllegalArgumentException("width is zero or negative"); } this.width = newWidth; } /** * Get the height of the ellipse. * * @return the height of the ellipse */ public double getHeight() { return this.height; } /** * Set the height of the ellipse. * * @param newHeight * the new width of the ellipse * @throws IllegalArgumentException if the new height is zero or negative */ public void setHeight(double newHeight) { if (newHeight <= 0.0) { throw new IllegalArgumentException("height is zero or negative"); } this.height = newHeight; } /** * Returns a string representation of the ellipse. * *
* The string returned has the form: * *
* position: (x, y), color: (r, g, b), width: W, height: H
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,
* W
is the width, and H
is the height.
*
* @return a string representation of the ellipse
*/
@Override
public String toString() {
return super.toString() + ", xradius: " + this.getWidth() + ", yradius: " + this.getHeight();
}
/**
* Draws a filled ellipse to StdDraw
using the
* position, width, height, and color of this ellipse.
*/
@Override
public void draw() {
StdDraw.setPenColor(this.getColor());
StdDraw.filledEllipse(this.getPosition().getX(), this.getPosition().getY(),
this.width / 2., this.height / 2.);
}
}