hypesystem

GD - Circle.java

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