package cse1030; import java.awt.Color; import princeton.introcs.StdDraw; /** * A rectangle shape. A rectangle has a width and height greater than zero. * * @author CSE1030Z * */ public class Rectangle extends Shape { private double width; private double height; /** * Create a rectangle with position (0, 0), color blue, width 1, and height 2. */ public Rectangle() { super(); this.width = 1.0; this.height = 2.0; } /** * Create a rectangle with position (x, y), and the given width, height, and color. * * @param x the x coordinate of the position of the rectangle * @param y the y coordinate of the position of the rectangle * @param width the width of the rectangle * @param height the height of the rectangle * @param color the color of the rectangle * @throws IllegalArgumentException if the width or height is zero or negative */ public Rectangle(double x, double y, double width, double height, Color color) { super(x, y, color); this.setWidth(width); this.setHeight(height); } /** * Create a rectangle with the given position, width, height, and color. * * @param position the position of the rectangle * @param width the width of the rectangle * @param height the height of the rectangle * @param color the color of the rectangle * @throws IllegalArgumentException if the width or height is zero or negative */ public Rectangle(Point2D position, double width, double height, Color color) { super(position, color); this.width = width; this.height = height; } /** * Get the width of the rectangle. * * @return the width of the rectangle. */ public double getWidth() { return width; } /** * Set the width of the rectangle. The width must be greater than zero. * * @param newWidth the new width of the rectangle * @throws IllegalArgumentException if the new width is zero or negative */ public void setWidth(double newWidth) { if (newWidth <= 0.) { throw new IllegalArgumentException("width is zero or negative"); } this.width = newWidth; } /** * Get the height of the rectangle. * * @return the height of the rectangle. */ public double getHeight() { return height; } /** * Set the height of the rectangle. The height must be greater than zero. * * @param newHeight the new height of the rectangle * @throws IllegalArgumentException if the new height is zero or negative */ public void setHeight(double newHeight) { if (newHeight <= 0.) { throw new IllegalArgumentException("height is zero or negative"); } this.height = newHeight; } /** * Returns a string representation of the rectangle. * *
* 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 rectangle
*/
@Override
public String toString() {
return super.toString() + ", width: " + this.getWidth() + ", height: " + this.getHeight();
}
/**
* Draws a filled rectangle to StdDraw
using the
* position, width, height, and color of this rectangle.
*/
@Override
public void draw() {
// TODO Auto-generated method stub
StdDraw.setPenColor(this.getColor());
StdDraw.filledRectangle(this.getPosition().getX(), this.getPosition().getY(),
this.getWidth() / 2., this.getHeight() / 2.);
}
}