galapagos
Class Turtle

java.lang.Object
  extended by galapagos.Turtle
All Implemented Interfaces:
java.lang.Runnable

public class Turtle
extends java.lang.Object
implements java.lang.Runnable

This Turtle draws geometric shapes a DrawingCanvas. This turtle will run in a separate Thread so multiple turtles can be drawing on a single canvas. Date Modified: February, 2009 Made the class up-to-date Java 5.0 and cleaned up the code a bit.

Author:
Dr Caffeine

Field Summary
static int CREATE_DEFAULT_WINDOW
          Constant for creating a default drawing window
static int NO_DEFAULT_WINDOW
          Constant for not creating any default drawing window
 
Constructor Summary
Turtle()
          A default constructor that creates an instance of the Turtle class with a default drawing window.
Turtle(int choice)
          Constructs a standard turtle with or without default drawing window assigned.
 
Method Summary
 void backup(double length)
          Moves this turtle backward for a specified length.
 void bodyColor(java.awt.Color color)
          Changes the body color of this turtle.
 void bodyShape(java.awt.Point[] point)
          Sets the shape of this turtle to the give shape expressed in an array of Point objects.
 void forward(double length)
          Moves this turtle forward for a given length.
 void heading(double degree)
          Sets this turtle's heading for a specified degree.
 void hide()
          Hides this turtle, that is, the turtle body is not displayed..
 void init()
          Initializes this turtle.
 void jumpTo(double x, double y)
          Jumps to the given (x, y).
 void move(double length)
          Moves this turtle's for a specified length.
 void moveTo(double targetX, double targetY)
          Moves this turtle to the specified target (x, y) position.
 void pause()
          Stops this turtle from moving.
 void penColor(java.awt.Color color)
          Sets the color of the turtle's pen.
 void penDown()
          Sets the pen state to down.
 void penSize(int penSize)
          Sets the size of this turtle's pen.
 void penUp()
          Sets the pen state to up.
 void print(java.lang.String message)
          Prints the specified message at the current position.
 void redraw(java.awt.Graphics g)
          Required method to implement the DrawingController interface.
 void run()
          Required method to implement the Runnable interface.
 void setCanvas(galapagos.DrawingCanvas canvas)
          Required method to implement the DrawingController interface.
 void show()
          Shows this turtle, that is, the turtle body is displayed.
 void speed(int speed)
          Changes the speed of this turtle
 void start()
          Starts moving this turtle in its own thread.
 void turn(double degree)
          Turns the turtle
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CREATE_DEFAULT_WINDOW

public static final int CREATE_DEFAULT_WINDOW
Constant for creating a default drawing window

See Also:
Constant Field Values

NO_DEFAULT_WINDOW

public static final int NO_DEFAULT_WINDOW
Constant for not creating any default drawing window

See Also:
Constant Field Values
Constructor Detail

Turtle

public Turtle()
A default constructor that creates an instance of the Turtle class with a default drawing window. The orientation of this turtle is 0 degree, i.e., facing East where the top of the screen is North. The pen is down with its color black and pen size 2 logical units wide.


Turtle

public Turtle(int choice)
Constructs a standard turtle with or without default drawing window assigned. The orientation of this turtle is 0 degree, i.e., facing East where the top of the screen is North. The pen is down with its color black and pen size 2 logical units wide.

Parameters:
choice - zero means no default window; non-zero will create a default window
Method Detail

backup

public void backup(double length)
Moves this turtle backward for a specified length.

Parameters:
length - the length to move backward

bodyColor

public void bodyColor(java.awt.Color color)
Changes the body color of this turtle. You cannot change the body color while the turtle is moving. Basically you want to set the turtle's body color once.

Parameters:
color - a new body color

bodyShape

public void bodyShape(java.awt.Point[] point)
Sets the shape of this turtle to the give shape expressed in an array of Point objects. The shape can be any polygon. You cannot change the body shape while the turtle is moving. Basically you want to set the turtle's body shape once.

Parameters:
point - an array of Point objects representing a polygon

forward

public void forward(double length)
Moves this turtle forward for a given length. This method is the same as the move method with a positive argument.

Parameters:
length - the length to move forward

heading

public void heading(double degree)
Sets this turtle's heading for a specified degree. The current heading is irrelevant. If the specified degree is negative, then it is a clockwise direction

Parameters:
degree - the new orientation of this turtle

hide

public void hide()
Hides this turtle, that is, the turtle body is not displayed..


init

public void init()
Initializes this turtle. When a turtle is first created, it is initialized automatically. The user needs to call this method explicitly, when a new drawing is required by the same turtle.


jumpTo

public void jumpTo(double x,
                   double y)
Jumps to the given (x, y). This is a warp jump so no drawing will take place, even when the pen is down.

Parameters:
x - the x coordinate of the target position
y - the y coordinate of the target position

move

public void move(double length)
Moves this turtle's for a specified length. Positive length moves the turtle forward and the negative length moves the turtle backward relative to its orientation. The position of this turtle is set to the new location. The orientation remains the same.


moveTo

public void moveTo(double targetX,
                   double targetY)
Moves this turtle to the specified target (x, y) position. The turtle's orientation will be adjusted to face the the target (x,y) position, regardless where this turtle is facing before this method is called.

Parameters:
targetX - the x coordinate of the target point
targetY - the y coordinate of the target point

pause

public void pause()
Stops this turtle from moving. Once in the pause mode this turtle needs to be activated by calling the start method. Otherwise the turtle will never move again.


penColor

public void penColor(java.awt.Color color)
Sets the color of the turtle's pen.

Parameters:
color - a new pen color for subsequent drawing

penDown

public void penDown()
Sets the pen state to down. Subsequent movement by this turtle will result in drawing the line.


penSize

public void penSize(int penSize)
Sets the size of this turtle's pen.

Parameters:
penSize - the new pen size

penUp

public void penUp()
Sets the pen state to up. Subsequent movement by this turtle will not result in drawing the line, i.e., this turtle moves to a specified location without drawing.


print

public void print(java.lang.String message)
Prints the specified message at the current position. The color of text is fixed to black. You cannot change the text color.

Parameters:
message - the text to display

redraw

public void redraw(java.awt.Graphics g)
Required method to implement the DrawingController interface. Redraws the shape currently drawn by the turtle. This method is called from DrawingCanvas when the repaint method is invoked by the system. Do not call this method directly from your code.

Parameters:
g - the Graphics object where this turtle can draw

run

public void run()
Required method to implement the Runnable interface. This method is called eventually by the system, and when called, will execute the commands in the instruction buffer provided that this turtle is in the motion mode (i.e. the pause method is not called).

Specified by:
run in interface java.lang.Runnable

setCanvas

public void setCanvas(galapagos.DrawingCanvas canvas)
Required method to implement the DrawingController interface. This method is called by a DrawingCanvas object. Do not call this method directly from your code.

Parameters:
canvas - the object that called this method

show

public void show()
Shows this turtle, that is, the turtle body is displayed.


speed

public void speed(int speed)
Changes the speed of this turtle

Parameters:
speed - the new speed of this turtle

start

public void start()
Starts moving this turtle in its own thread.


turn

public void turn(double degree)
Turns the turtle