java.lang.ObjectTurtle
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 |
public Turtle()
| Method Detail |
public void die()
public void done()
public void draw()
public java.awt.Color getColor()
public boolean isAlive()
true or false.public boolean isPenDown()
true or false.public void lt()
DeadTurtleException - if the turtle is dead.public void lt(double angle)
angle - How far to turn, in degrees.
DeadTurtleException - if the turtle is dead.public void move()
DeadTurtleException - if the turtle is dead.public void move(double distance)
distance - How long a line to draw.
DeadTurtleException - if the turtle is dead.public void penDown()
public void penUp()
public void rt()
DeadTurtleException - if the turtle is dead.public void rt(double angle)
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.).
angle - How far to turn, in degrees.
DeadTurtleException - if the turtle is dead.public void setColor(java.awt.Color color)
import java.awt.Color;
color - New pen color.