Class Turtle

java.lang.Object
  extended byTurtle

public class Turtle
extends java.lang.Object

The Turtle class is really the core of this project. A Turtle is a small computer creature that can be instructed to move around on a "canvas", the computer screen. It has a "pen" with which it can draw a line behind it as it moves; it can be told to change the color of the pen, or to raise the pen so that it will not leave a line.

When a Turtle is first created, its pen is down (drawing), its color is black, and it is located at the center of the canvas.

The movement of the turtle is controlled with the methods move(), rt(), and lt(), which have it move forward one space, take a right turn, and take a left turn, respectively. These turns are all 90 degrees. So, I could draw a square by creating a turtle and telling it four times to move() and then rt().

For more fine control, you can pass a decimal number to these functions, giving the distance to move() or the number of degrees to rt() or lt(). So, I could draw a triangle of side length 3 by creating a turtle and telling it three times to move(3.) and then rt(120.).

The pen may be set to draw or not draw using the penUp() and penDown() methods. The color can be changed by the setColor() method. So, to make the turtle draw in blue, I would tell it to penDown(Color.blue). Note that in order to use Color, I would need to import java.awt.Color at the top of whatever source file contains that setColor() instruction.

The heading of a turtle is like a compass heading: North is 0, East is 90, South is 180, West is 270. You can set the turtle to point in a particular exact direction by using the method setHeading(). So, to make a turtle face East, regardless of how it was facing before, I would tell it setHeading(90.).

To remove a Turtle from the canvas, call the method die(). If a turtle is dead (or if, in a subclass, you forgot to call super() in the constructor) any attempt to give it instructions will result in a DeadTurtleException.

You can find out the current pen color and whether the pen is down with getColor() and isPenDown(), respectively. You can check whether the turtle has died with isAlive().


Constructor Summary
Turtle()
          Create a new turtle that is located at the center of the screen, facing up, has its pen down, and is drawing in black.
 
Method Summary
 void die()
          Remove this turtle from the canvas.
 void done()
          Most of the Turtle routines count as a single step of the program.
 void draw()
          Not needed for assignment.
You can ignore this method.
 java.awt.Color getColor()
          Return the current pen color of this turtle.
 boolean isAlive()
          Check whether this turtle is alive.
 boolean isPenDown()
          Check whether this turtle's pen is down.
 void lt()
          Make a 90° left turn.
 void lt(double angle)
          Make a left turn.
 void move()
          Move forward one unit.
 void move(double distance)
          Ask the turtle to move forward the specified distance along its current heading.
 void penDown()
          Put this turtle's pen down.
 void penUp()
          Raise this turtle's pen.
 void rt()
          Make a 90° right turn.
 void rt(double angle)
          Make a right turn.
 void setColor(java.awt.Color color)
          Set the color of this turtle's pen.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Turtle

public Turtle()
Create a new turtle that is located at the center of the screen, facing up, has its pen down, and is drawing in black.

Method Detail

die

public void die()
Remove this turtle from the canvas.


done

public void done()
Most of the Turtle routines count as a single step of the program. So, after they run, they stop the controller; the timer running in Turtles will restart the controller when it's time for the next step. The method done() is called whenever you want to end a step of the turtle's motion. By default, it is called after any method that changes the state of the turtle (color, pen, location, heading).


draw

public void draw()
Not needed for assignment.
You can ignore this method. It is here not for your use, but for the Canvas to use when it wants to draw the turtles.


getColor

public java.awt.Color getColor()
Return the current pen color of this turtle.

Returns:
The current pen color.

isAlive

public boolean isAlive()
Check whether this turtle is alive.

Returns:
Whether this turtle is alive, true or false.

isPenDown

public boolean isPenDown()
Check whether this turtle's pen is down.

Returns:
Whether this turtle is alive, true or false.

lt

public void lt()
Make a 90° left turn.

Throws:
DeadTurtleException - if the turtle is dead.

lt

public void lt(double angle)
Make a left turn. See the warning on @see{rt(double angle)}.

Parameters:
angle - How far to turn, in degrees.
Throws:
DeadTurtleException - if the turtle is dead.

move

public void move()
Move forward one unit.

Throws:
DeadTurtleException - if the turtle is dead.

move

public void move(double distance)
Ask the turtle to move forward the specified distance along its current heading. This will draw a trail if the pen is down.

Parameters:
distance - How long a line to draw.
Throws:
DeadTurtleException - if the turtle is dead.

penDown

public void penDown()
Put this turtle's pen down. In other words, make it start leaving a trail.


penUp

public void penUp()
Raise this turtle's pen. In other words, make it stop leaving a trail.


rt

public void rt()
Make a 90° right turn.

Throws:
DeadTurtleException - if the turtle is dead.

rt

public void rt(double angle)
Make a right turn. Note that the turn angle is a double, a decimal number. Be careful of integer vs. double errors when using this: rt(360 / 16) will turn 22°, not 22.5. So, you should instead say rt(360. / 16.).

Parameters:
angle - How far to turn, in degrees.
Throws:
DeadTurtleException - if the turtle is dead.

setColor

public void setColor(java.awt.Color color)
Set the color of this turtle's pen. Note that in order to create a Color object to pass to this method you must import Color: at the top of your source file write:

import java.awt.Color;

Parameters:
color - New pen color.