Lecture 5 Sherriff - Turtle Power!

Lecture Date: Friday, January 23

Today, we are going to move a bit quick, but mainly so you can see a quick overview of a lot of concepts.

First, you need to get everything setup to go!

  1. Open Eclipse. Remember, it MUST NOT be removed from the folder it came in, thoguh the whole folder may be moved.

  2. If it asks you about a workspace, pick the folder where you’d like all the files we write this semester to be located.

  3. In the welcome page there’s a button labeled Workbench on the right. Click it.

  4. Select File → New → Java Project (you might need to select just Project and then select Java Project on the next screen)

  5. Type the project name as TurtleDemo (or something else if you want; I’ll assume TurtleDemo)

  6. Leave everything else in its default state and press the Finish button

  7. If it asks about the Java perspective check remember my decision and click Yes

  8. Click the little plus sign next to the project name:

Project Plus

You should now see a folder called src; we’ll be putting files in that.

  1. Download Turtle.java and World.java

  2. Open your operating system’s file viewer to where you downloaded the .java files

  3. Drag Turtle.java and World.java from your file viewer onto the src folder icon in Eclipse. If it asks, chose copy rather than link. You should now see something like the following:

Setup

If you do not, click + icons and/or drag files around until you do.

  1. Select File → New → Class

  2. Type Demo1 in the Name field and check the box next to public static void main(String[] args), then click Finish

Example Code

Example Turtle Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.awt.Color;

public class TurtleTest {
    public static void main(String[] args) {
        World cruton = new World(800, 500, Color.WHITE);
        Turtle sasha = new Turtle(cruton, -50, 100);
        sasha.forward(200);
        sasha.right(76);
        sasha.forward(100);
        sasha.right(76);
        sasha.backward(50);
        sasha.goTo(0,0);
    }
}

The API (i.e. things you can tell this stuff to do)

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/TurtleDemo/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

There are a few other methods too, like t.putPenDown() and get methods for most of the set methods.