hypesystem

GD - GD.java

Nov 1st, 2011
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.32 KB | None | 0 0
  1. package gd;
  2.  
  3. import java.util.ArrayList;
  4. import javax.swing.JFrame;
  5.  
  6. /**
  7.  * GD shows the functionality of the shapes defined in the gd package.
  8.  * @author hypesystem
  9.  * @email  [email protected]
  10.  */
  11. public class GD {
  12.     private Shape[] shapes;
  13.     private Composite composite1, composite2;
  14.  
  15.     /**
  16.      * Tests the functionality and displays this by creating objects and calling
  17.      * methods of the gd package.
  18.      */
  19.     public GD() {
  20.         System.out.println("==SHAPE FUNCTIONALITY==\n");
  21.        
  22.         shapes = new Shape[3];
  23.         shapes[0] = new Circle(1);
  24.         shapes[1] = new Rectangle(2,5);
  25.         shapes[2] = new Triangle(3,4,5);
  26.         System.out.println("3 shapes: Circle(1), Rectangle(2,5), "
  27.                 + "Triangle(3,4,5):");
  28.         for(Shape shape : shapes) {
  29.             System.out.println("Circumference: "
  30.                     + shape.circumference());
  31.         }
  32.         pauseOutput();
  33.        
  34.         ArrayList<Shape> composite1_parts = new ArrayList<Shape>();
  35.         composite1_parts.add(new Circle(1));
  36.         composite1_parts.add(new Rectangle(2,5));
  37.         composite1_parts.add(new Triangle(3,4,5));
  38.         composite1 = new Composite(composite1_parts);
  39.         System.out.println("\nComposite shape consisting of same shapes:");
  40.         System.out.println("Composite circ: "+composite1.circumference());
  41.         pauseOutput();
  42.        
  43.         ArrayList<Shape> composite2_parts = new ArrayList<Shape>();
  44.         composite2_parts.add(composite1);
  45.         composite2_parts.add(new Triangle(3,4,5));
  46.         composite2 = new Composite(composite2_parts);
  47.         System.out.println("\nComposite shape consisting of composite above "
  48.                 + "and an additional triangle from above:");
  49.         System.out.println("Composite circ: "+composite2.circumference());
  50.         System.out.println("Simple shapes: "+composite2.countSimple());
  51.         pauseOutput();
  52.        
  53.         System.out.println("\n== MOVABLE SHAPES==\n");
  54.        
  55.         ArrayList<Movable> movable_shapes = new ArrayList<Movable>();
  56.         movable_shapes.add(new Triangle(1,2,3, new Coordinate(1,1)));
  57.         movable_shapes.add(new Rectangle(2,3, new Coordinate(1,5)));
  58.         movable_shapes.add(new Circle(5, new Coordinate(5,1)));
  59.         moveAll(movable_shapes,10,0);
  60.         pauseOutput();
  61.        
  62.         DrawableAndMovable[] drawable = new DrawableAndMovable[3];
  63.         drawable[0] = new Rectangle(30,30, new Coordinate(30,30));
  64.         drawable[1] = new Circle(15, new Coordinate(30,0));
  65.         drawable[2] = new Circle(15, new Coordinate(0,0));
  66.         DrawMove dm = new DrawMove();
  67.         dm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  68.         System.out.println("Next: Drawing to GUI");
  69.         pauseOutput();
  70.         dm.drawThem(drawable); //draws them
  71.         System.out.println("Next: Drawing to GUI and moving");
  72.         pauseOutput();
  73.         dm.drawAndMoveThem(drawable); //draws and moves them
  74.        
  75.         /*
  76.          * (GD.2.vi) see DrawableAndMovable for more information on a different
  77.          *   way to work around this.
  78.          *   At some points in DrawMove it uses methods only defined in Drawable
  79.          *   and at other points it uses methods only defined in Movable. If it
  80.          *   took a Drawable input, it coudln't call Movable methods and vice
  81.          *   versa.
  82.          */
  83.     }
  84.    
  85.     public void moveAll(ArrayList<Movable> movable, int dx, int dy) {
  86.         for(Movable obj : movable) {
  87.             System.out.print("Moved object "+obj+" from "
  88.                     +obj.getCoordinate().getX()+","+obj.getCoordinate().getY());
  89.             obj.move(dx, dy);
  90.             System.out.print(" to "+obj.getCoordinate().getX()+","
  91.                     +obj.getCoordinate().getY()+".\n");
  92.         }
  93.     }
  94.    
  95.     /**
  96.      * Pauses the output stream waiting for an input. Very hacky, and not very
  97.      * correct... but it does the job.
  98.      */
  99.     public void pauseOutput() {
  100.         try {
  101.             System.out.println("Press any key ...");
  102.             int b = System.in.read();
  103.         }
  104.         catch(Exception e) {
  105.             System.out.println(e);
  106.         }
  107.     }
  108.    
  109.     /**
  110.      * Launches the application
  111.      * @param args the command line arguments
  112.      */
  113.     public static void main(String[] args) {
  114.         new GD();
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment