Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package gd;
- import java.awt.Graphics;
- /**
- * A specific shape: A rectangle.
- * This rectangle inherits from Shape.
- * @author hypesystem
- * @email [email protected]
- */
- public class Rectangle extends Shape implements Drawable, DrawableAndMovable {
- private double width, height;
- /**
- * Creates a rectangle witout a coordinate argument (sets coordinates to
- * 0,0).
- * @param width width of rectangle
- * @param height height of rectangle
- */
- public Rectangle(double width, double height) {
- super(new Coordinate(0,0));
- setAttr(width, height);
- }
- /**
- * Creates a rectangle with a coordinate argument.
- * @param width width of rectangle
- * @param height height of rectangle
- * @param coordinates coordinates of rectangle.
- */
- public Rectangle(double width, double height, Coordinate coordinates) {
- super(coordinates);
- setAttr(width, height);
- }
- /**
- * Sets the attributes of the Rectangle. Called from constructors (hence
- * private)
- * @param width width of rectangle
- * @param height height of rectangle
- */
- private void setAttr(double width, double height) {
- this.width = width;
- this.height = height;
- }
- /**
- * Calculates and returns the circumference of the rectangle. Inherited from
- * Shape.
- * @return circumference of rectangle
- */
- @Override
- public double circumference() {
- return 2 * (width + height);
- }
- /**
- * Calculates and returns the area of the rectangle Inherited from Shape.
- * @return area of rectangle
- */
- @Override
- public double area() {
- return (width * height);
- }
- /**
- * Counts the amount of simple shapes in this shape. This is a simple shape.
- * Inherited from Shape.
- * @return 1
- */
- @Override
- public int countSimple() {
- return 1;
- }
- /**
- * Draws the rectangle to a specified graphic. Implemented from Drawable.
- * @param g graphic to draw to
- */
- @Override
- public void draw(Graphics g) {
- g.fillRect(getCoordinate().getX(), getCoordinate().getY(),
- (int)width, (int)height);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment