Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package gd;
- import java.util.ArrayList;
- import javax.swing.JFrame;
- /**
- * GD shows the functionality of the shapes defined in the gd package.
- * @author hypesystem
- * @email [email protected]
- */
- public class GD {
- private Shape[] shapes;
- private Composite composite1, composite2;
- /**
- * Tests the functionality and displays this by creating objects and calling
- * methods of the gd package.
- */
- public GD() {
- System.out.println("==SHAPE FUNCTIONALITY==\n");
- shapes = new Shape[3];
- shapes[0] = new Circle(1);
- shapes[1] = new Rectangle(2,5);
- shapes[2] = new Triangle(3,4,5);
- System.out.println("3 shapes: Circle(1), Rectangle(2,5), "
- + "Triangle(3,4,5):");
- for(Shape shape : shapes) {
- System.out.println("Circumference: "
- + shape.circumference());
- }
- pauseOutput();
- ArrayList<Shape> composite1_parts = new ArrayList<Shape>();
- composite1_parts.add(new Circle(1));
- composite1_parts.add(new Rectangle(2,5));
- composite1_parts.add(new Triangle(3,4,5));
- composite1 = new Composite(composite1_parts);
- System.out.println("\nComposite shape consisting of same shapes:");
- System.out.println("Composite circ: "+composite1.circumference());
- pauseOutput();
- ArrayList<Shape> composite2_parts = new ArrayList<Shape>();
- composite2_parts.add(composite1);
- composite2_parts.add(new Triangle(3,4,5));
- composite2 = new Composite(composite2_parts);
- System.out.println("\nComposite shape consisting of composite above "
- + "and an additional triangle from above:");
- System.out.println("Composite circ: "+composite2.circumference());
- System.out.println("Simple shapes: "+composite2.countSimple());
- pauseOutput();
- System.out.println("\n== MOVABLE SHAPES==\n");
- ArrayList<Movable> movable_shapes = new ArrayList<Movable>();
- movable_shapes.add(new Triangle(1,2,3, new Coordinate(1,1)));
- movable_shapes.add(new Rectangle(2,3, new Coordinate(1,5)));
- movable_shapes.add(new Circle(5, new Coordinate(5,1)));
- moveAll(movable_shapes,10,0);
- pauseOutput();
- DrawableAndMovable[] drawable = new DrawableAndMovable[3];
- drawable[0] = new Rectangle(30,30, new Coordinate(30,30));
- drawable[1] = new Circle(15, new Coordinate(30,0));
- drawable[2] = new Circle(15, new Coordinate(0,0));
- DrawMove dm = new DrawMove();
- dm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- System.out.println("Next: Drawing to GUI");
- pauseOutput();
- dm.drawThem(drawable); //draws them
- System.out.println("Next: Drawing to GUI and moving");
- pauseOutput();
- dm.drawAndMoveThem(drawable); //draws and moves them
- /*
- * (GD.2.vi) see DrawableAndMovable for more information on a different
- * way to work around this.
- * At some points in DrawMove it uses methods only defined in Drawable
- * and at other points it uses methods only defined in Movable. If it
- * took a Drawable input, it coudln't call Movable methods and vice
- * versa.
- */
- }
- public void moveAll(ArrayList<Movable> movable, int dx, int dy) {
- for(Movable obj : movable) {
- System.out.print("Moved object "+obj+" from "
- +obj.getCoordinate().getX()+","+obj.getCoordinate().getY());
- obj.move(dx, dy);
- System.out.print(" to "+obj.getCoordinate().getX()+","
- +obj.getCoordinate().getY()+".\n");
- }
- }
- /**
- * Pauses the output stream waiting for an input. Very hacky, and not very
- * correct... but it does the job.
- */
- public void pauseOutput() {
- try {
- System.out.println("Press any key ...");
- int b = System.in.read();
- }
- catch(Exception e) {
- System.out.println(e);
- }
- }
- /**
- * Launches the application
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- new GD();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment