Lecture 6 (Tychonievich) - Turtles 2

Lecture Date: Monday, January 26

Today we conclude a 2-day whirlwind tour of most of the semester.

Last time we wrote code like

Example Turtle Code
1
2
3
4
5
6
7
8
9
10
11
12
13
 public static void main(String[] args) {
      World earth = new World();
      Turtle carmen = new Turtle(earth);

      drawPolygon(carmen, 40, 5);
  }

  public static void drawPolygon(Turtle bob, int size, int sides) {
      for (int i = 0; i < sides; i++) {
          bob.forward(size);
          bob.right(360/sides);
      }
  }

Today we’ll look at other Turtle and World methods listed below, as well as textual interations with the user (System.out.println, which we saw before, and new Scanner(System.in)’s method nextInt). We’ll also draw shrubs, using the directions below.

Things you can tell a world

CommandAction
World w = new World();Makes a new 600-by-600 pixel world
World w = new World(w, h, Color.RED);Makes a new w-by-h pixel world with a red background
w.clearGround();Erases all turtle trails
w.saveAs("someImageName.png");Saves the current image in your project folder (path/to/your/eclipse/workspace/Your Project Name/someImageName.png)

Things you can tell a turtle

CommandAction
Turtle t = new Turtle(someWorld);Makes a new turtle in the center of the world
Turtle t = new Turtle(wrld, x, y);Makes a new turtle at the specified point of the world
t.forward(100);t moves forward 100 pixels
t.backward(83);t moves backward 83 pixels
t.left(47);t rotates to its left (counter-clockwise) 47°
t.right(306);t rotates to its right (clockwise) 306°
t.setColor(Color.RED);t becomes a red turtle.
t.pickPenUp();t stops leaving trails as it moves.
t.setShellSize(200);t becomes a very large turtle
t.setPenWidth(20);trails t leaves are now 20-pixels wide
t.dropPicture
("http://example.com/logo.png", 30);
t draws a picture about 30 pixels wide where it is standing

How do draw a shrub:

  1. turn left
  2. go forward
  3. draw a (smaller) shrub
  4. go backward
  5. turn right
  6. go forward
  7. draw a (smaller) shrub
  8. go backward
  9. straighten out (by turning left)

From lecture:

Code: TurtlesDay2.java, which draws wonky trees

Audio: