hypesystem

GD - Composite.java

Nov 1st, 2011
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.15 KB | None | 0 0
  1. package gd;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. /**
  6.  * A shape consisting of multiple shapes held in an arraylist (UNLIMITED POWER!)
  7.  * @author hypesystem
  8.  * @email  [email protected]
  9.  */
  10. public class Composite extends Shape {
  11.     private ArrayList<Shape> shapes;
  12.    
  13.     /**
  14.      * Creates a composite shape consisting of serveral other shapes (that can
  15.      * be either composite shapes themselves or simple shapes).
  16.      * @param shapes Array of shapes to consist of
  17.      */
  18.     public Composite(ArrayList<Shape> shapes) {
  19.         super(null);
  20.         this.shapes = shapes;
  21.     }
  22.    
  23.     /**
  24.      * Returns the area of this composite shape (the sum of the areas of the
  25.      * simple shapes).
  26.      * @return area
  27.      */
  28.     @Override
  29.     public double area() {
  30.         double area = 0;
  31.         for(Shape shape : shapes) {
  32.             area += shape.area();
  33.         }
  34.         return area;
  35.     }
  36.    
  37.     /**
  38.      * Notice the text before the circumference method in TwoShapes for comments
  39.      * on this method.
  40.      * @return sum of circumferences of simple shapes
  41.      */
  42.     @Override
  43.     public double circumference() {
  44.         double circ = 0;
  45.         for(Shape shape : shapes) {
  46.             circ += shape.circumference();
  47.         }
  48.         return circ;
  49.     }
  50.    
  51.     /**
  52.      * Returns the amount of simple shapes in this composite shape
  53.      * @return amount of simple shapes
  54.      */
  55.     @Override
  56.     public int countSimple() {
  57.         int simple = 0;
  58.         for(Shape shape : shapes) {
  59.             simple += shape.countSimple();
  60.         }
  61.         return simple;
  62.     }
  63.    
  64.     /**
  65.      * Adds a shape to the composite
  66.      * @param shape shape to add
  67.      */
  68.     public void addShape(Shape shape) {
  69.         shapes.add(shape);
  70.     }
  71.    
  72.     /**
  73.      * Removes a shape from the composite by its index number
  74.      * @param index index of shape to remove
  75.      */
  76.     public void removeShape(int index) {
  77.         shapes.remove(index);
  78.     }
  79.    
  80.     /**
  81.      * Removes a shape from the composite
  82.      * @param shape shape to remove
  83.      */
  84.     public void removeShape(Shape shape) {
  85.         shapes.remove(shape);
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment