hypesystem

GD - Rectangle.java

Nov 1st, 2011
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.27 KB | None | 0 0
  1. package gd;
  2.  
  3. import java.awt.Graphics;
  4.  
  5. /**
  6.  * A specific shape: A rectangle.
  7.  * This rectangle inherits from Shape.
  8.  * @author hypesystem
  9.  * @email  [email protected]
  10.  */
  11. public class Rectangle extends Shape implements Drawable, DrawableAndMovable {
  12.     private double width, height;
  13.    
  14.     /**
  15.      * Creates a rectangle witout a coordinate argument (sets coordinates to
  16.      * 0,0).
  17.      * @param width width of rectangle
  18.      * @param height height of rectangle
  19.      */
  20.     public Rectangle(double width, double height) {
  21.         super(new Coordinate(0,0));
  22.         setAttr(width, height);
  23.     }
  24.    
  25.     /**
  26.      * Creates a rectangle with a coordinate argument.
  27.      * @param width width of rectangle
  28.      * @param height height of rectangle
  29.      * @param coordinates coordinates of rectangle.
  30.      */
  31.     public Rectangle(double width, double height, Coordinate coordinates) {
  32.         super(coordinates);
  33.         setAttr(width, height);
  34.     }
  35.    
  36.     /**
  37.      * Sets the attributes of the Rectangle. Called from constructors (hence
  38.      * private)
  39.      * @param width width of rectangle
  40.      * @param height height of rectangle
  41.      */
  42.     private void setAttr(double width, double height) {
  43.         this.width = width;
  44.         this.height = height;
  45.     }
  46.    
  47.     /**
  48.      * Calculates and returns the circumference of the rectangle. Inherited from
  49.      * Shape.
  50.      * @return circumference of rectangle
  51.      */
  52.     @Override
  53.     public double circumference() {
  54.         return 2 * (width + height);
  55.     }
  56.    
  57.     /**
  58.      * Calculates and returns the area of the rectangle Inherited from Shape.
  59.      * @return area of rectangle
  60.      */
  61.     @Override
  62.     public double area() {
  63.         return (width * height);
  64.     }
  65.    
  66.     /**
  67.      * Counts the amount of simple shapes in this shape. This is a simple shape.
  68.      * Inherited from Shape.
  69.      * @return 1
  70.      */
  71.     @Override
  72.     public int countSimple() {
  73.         return 1;
  74.     }
  75.    
  76.     /**
  77.      * Draws the rectangle to a specified graphic. Implemented from Drawable.
  78.      * @param g graphic to draw to
  79.      */
  80.     @Override
  81.     public void draw(Graphics g) {
  82.         g.fillRect(getCoordinate().getX(), getCoordinate().getY(),
  83.                 (int)width, (int)height);
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment