Skip to main content.

Thursday, August 24, 2006

Preliminaries

Before getting started, you will need to download some additional Java files for this portion of the lab. Download this compressed file to your hard drive: turtlegraphics.zip. Uncompress the file (with Windows, you should be able to just double-click on it to open it) and copy the files inside to a subdirectory named "turtlegraphics" (spelled exactly that way) in your personal working directory.

Turtle Graphics Overview

Have you ever used an Etch-a-Sketch(R) toy? In this lab we will experiment with a special programming system called turtle graphics. In turtle graphics, the program controls a make-believe turtle on the screen. As the turtle moves around, it leaves a line behind. (Materials for this lab are adapted from software for Programming and Problem Solving with Java by James Slack.)

The turtle and its world

The turtle is an imaginary creature that moves around on the computer screen. We control the turtle with instructions in a Java program, such as move and turnRight. As it moves, the turtle draws lines with an imaginary pen. If the turtle's pen is down, the turtle leaves a line behind when it moves.

If the turtle lifts its pen, it can move without drawing a line. To draw a dotted line, for example, we have the turtle move a little and pick up its pen. We then move it a little more and put its pen down. We continue moving the turtle this way, alternately moving with the pen up and then down.

The turtle's world is the drawing window (the one labeled "Turtle Graphics") on the computer screen. The turtle can move anywhere within this drawing window. The turtle measures distance in turtle steps (t-steps for short), where one t-step is the smallest distance the turtle can move. The distance from the left side of the drawing window to the right is 1200 t-steps, and the distance from the top to the bottom is also 1200 t-steps. (These measurements hold no matter how big or small the window is.)

To make the turtle draw something, you write instructions in a Java program and compile the program. When you run the program, the turtle will move around the screen according to the instructions in the program.

The turtle starts in the center of its world, facing straight up, with its pen down.

Turtle Methods

There are several useful turtle graphics methods.

The move Method

The move instruction tells the turtle to move in the direction it's facing. If the turtle's pen is down, the turtle draws a line as it moves. When you use move, you need to tell the turtle how far to go. Here's how to move the turtle myTurtle 300 t-steps, for example.

myTurtle.move(300);

You should try to think of this statement as a request to the turtle myTurtle to move 300 t-steps. Try to imagine myTurtle as an object that can respond to requests. (That's what it is, after all. Later in the course, we'll learn more about objects and object-oriented programming...)

The reason you should think of myTurtle.move(300) as a request (instead of a command) is that the turtle may decide not to follow your request. For example, the turtle will refuse to move off the drawing window - so be careful when the turtle is near an edge. The turtle won't move zero or a negative number of t-steps, either.

When the turtle refuses to follow a request, it gives up on the program. The computer stops the program and displays an error message.

The turnRight Method

To make the turtle face a different direction, ask the turtle to turnRight. The turtle will turn any number of degrees from 0 to 180, and will refuse to turn any amount outside that range.

To turn the turtle myTurtle around so it faces the opposite direction, for example, you'd write this in a program.

myTurtle.turnRight(180);

The penUp Method

Use the penUp instruction to ask the turtle to pick up its pen. When the turtle's pen is up, it won't leave a line behind as it moves. The turtle refuses to pick up its pen if it's already up.

Here's how to ask the turtle myTurtle to pick up its pen.

myTurtle.penUp();

The penDown Method

The penDown instruction is the opposite of penUp. The penDown instruction asks the turtle to put its pen down, so it leaves a line behind as it moves. The turtle refuses to put its pen down if it's already down.

Here's how to ask the turtle myTurtle put down its pen.

myTurtle.penDown();

Additional methods

Some other methods that you may experiment with on your own (they may be more useful once you know more Java programming constructs):

Putting it all together

As a simple example, we'll have the turtle draw a square. Let's think through the process before we write the program. The easiest way to design a turtle graphics program is to imagine it from the turtle's point of view. From this point of view, the process of drawing a square consists of these steps.

You might have noticed that we don't need the last turn. However, it's good practice to leave the turtle in the same position and direction in which it started.

Before writing the program, we need to decide how big to make the square. Remember that the drawing window is 1200 t-steps across and 1200 t-steps down. The turtle starts in the middle of the drawing window, so we'd better make the size of the square less than 600-- let's use 500.

Here's the program. Notice that it follows the steps above. Click the Download link below to copy the program to your computer. Compile and run the program on your computer.

DrawSquare.java Download

// This program draws a square on the screen

import turtlegraphics.*;

public class DrawSquare
{
  public static void main(String[] args)
  throws TurtleException
  {
    Turtle myTurtle = new Turtle();
    
    myTurtle.move(500);
    myTurtle.turnRight(90);
    myTurtle.move(500);
    myTurtle.turnRight(90);
    myTurtle.move(500);
    myTurtle.turnRight(90);
    myTurtle.move(500);
    myTurtle.turnRight(90);
  }
}
DrawSquare.java Download