예제 #1
0
  public static void main(String asdf[]) {
    World w; // ticket to hold where our World is in memory
    Turtle mt; // to hold where my Turtle is
    w = new World(); // go to the World factory and, please, make me a World
    mt = new Turtle(w); // make a new Turtle, like mamma turtle does in her belly

    mt.setPenWidth(6);

    mt.turn(180.0); // tells Turtle to turn all the way around, same effect as turning right twice.
    mt.forward();
    mt.turnLeft(); // class voted to make an L by turning left
    mt.forward(); // we now have two lines of the L
    mt.turnLeft();
    mt.forward(30); // the toe was too long, so we tried a shorter length
    // forward() and forward(int pixels) are two "polymorphic"
    // methods: Different methods with the SAME name but
    // different because of signature.
    mt.turnLeft();
    mt.forward(30 / 3); // add a hook to the toe.  The hook was too long,
    // so we program dividing the original length by 3
    mt.hide(); // makes the Turtle's body become hidden, so the letter looks nicer.
    // DONE! RELEASE IT! 1/31/2012
  }
예제 #2
0
  public static void main(String[] args) throws InterruptedException {
    Turtle t = new Turtle();
    t.hide();
    t.speed(0);
    double x = 0;
    double y = 0;

    t.onKey("clearScreen", "c");

    System.out.println("-----2.5: THREE SHAPES-----");
    System.out.println("\nLeft-click the canvas to draw a square.");
    System.out.println("Middle-click the canvas to draw a circle.");
    System.out.println("Right-click the canvas to draw a triangle.");
    System.out.println("Press 'c' to clear the canvas.");

    while (true) {
      if (t.mouseButton1()) {
        x = t.mouseX();
        y = t.mouseY();

        x = t.canvasX(x);
        y = t.canvasY(y);

        t.penColor("pink");
        t.setDirection(0.0);
        t.up();
        t.setPosition(x, y);
        t.down();
        for (int i = 0; i < 4; i++) {
          t.forward(50.0);
          t.right(90.0);
        }
        Thread.sleep(100);
      } else if (t.mouseButton2()) {
        x = t.mouseX();
        y = t.mouseY();

        x = t.canvasX(x);
        y = t.canvasY(y);

        t.penColor("olive");
        t.setDirection(0.0);
        t.up();
        t.setPosition(x, y);
        t.down();
        for (int i = 0; i < 360; i++) {
          t.forward(0.5);
          t.right(1.0);
        }
        Thread.sleep(100);
      } else if (t.mouseButton3()) {
        x = t.mouseX();
        y = t.mouseY();

        x = t.canvasX(x);
        y = t.canvasY(y);

        t.penColor("light blue");
        t.setDirection(0.0);
        t.up();
        t.setPosition(x, y);
        t.down();
        for (int i = 0; i < 3; i++) {
          t.forward(50.0);
          t.right(120.0);
        }
        Thread.sleep(100);
      }
    }
  }