Class Turtle

java.lang.Object
  extended by Turtle

public class Turtle
extends Object

Turtle is a class representing a fun-loving reptile residing in a World. It contains attributes for x and y position, direction, color, etc. The turtle is dynamic and can be moved, turned, made (in)visible, re-colored, etc. Responsibility for drawing the turtle according to its attributes belong to the World-class. The idea is to allow expansion of its capabilities by writing additional methods for more complex behavior and operations.


Field Summary
static double RADIUS
           
 
Constructor Summary
Turtle(World w)
          Constructs a turtle and places it at the center of the world.
Turtle(World w, int x, int y)
          Constructs a turtle and places it at the specified position in the world.
 
Method Summary
 void disablePath()
          Disables drawing of the path when the turtle moves.
 double distanceTo(int x, int y)
          Calculates the Euclidean distance between a turtle and a given other point.
 void enablePath()
          Enables drawing of the path when the turtle moves.
 Color getColor()
          Returns the main Color of the turtle (used to draw its body).
 int getDirection()
          Returns the direction of the turtle in degrees.
 Color getLimbColor()
          Returns the Color of the turtle's limbs (head and legs).
 double getRadius()
          Returns the radius corresponding to the current scaling factor of the turtle.
 double getSize()
          Returns the scaling factor for the turtle where 1.0 is the default size and 2.0 is double the default size and 0.5 is half the default size.
 World getWorld()
          Returns the World associated with this turtle.
 int getXPos()
          Returns the x-coordinate of the turtle.
 int getYPos()
          Returns the y-coordinate of the turtle.
 boolean isPathEnabled()
          Returns true if path drawing is enabled and false if it's disabled.
 boolean isVisible()
          Returns true if the turtle is visible and false if it's invisible.
 void move(int step)
          Moves the turtle along its current direction by an integer step-size.
 void moveTo(int x, int y)
          Repositions the turtle to the given x- and y-coordinates.
 void setColor(int red, int green, int blue)
          Sets the color of the turtle to the specified RGB color.
 void setDirection(int direction)
          Sets the direction of the tutle in degrees.
 void setRadius(double radius)
          Set the scaling factor corresponding to the specified radius.
 void setSize(double size)
          Sets the scaling factor for the turtle where 1.0 is the default size and 2.0 is double the default size and 0.5 is half the default size.
 void setVisible(boolean visible)
          Sets the visibility status of the turtle.
 String toString()
          Generates a compact string representation of some of the turtle's attributes.
 void turn(int degrees)
          Turns the turtle clockwise by the given integer number of degrees.
 void turnTo(int x, int y)
          Turns the turtle to face the point specified by the x- and y-coordinate.
 void turnTo(Turtle t)
          Turns the turtle to face the position of another turtle.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

RADIUS

public static final double RADIUS
See Also:
Constant Field Values
Constructor Detail

Turtle

public Turtle(World w)
Constructs a turtle and places it at the center of the world.

Parameters:
w - The world which the turtle will reside in.

Turtle

public Turtle(World w,
              int x,
              int y)
Constructs a turtle and places it at the specified position in the world.

Parameters:
w - The world which the turtle will reside in.
x - X-coordinate of the point where the turtle will start at.
y - Y-coordinate of the point where the turtle will start at.
Method Detail

getWorld

public World getWorld()
Returns the World associated with this turtle.

Returns:
The world.

getXPos

public int getXPos()
Returns the x-coordinate of the turtle.

Returns:
X-coordinate as an integer.

getYPos

public int getYPos()
Returns the y-coordinate of the turtle.

Returns:
Y-coordinate as an integer.

getDirection

public int getDirection()
Returns the direction of the turtle in degrees. The value is guaranteed to be in domain [0-359].

Returns:
The direction in degrees.

setDirection

public void setDirection(int direction)
Sets the direction of the tutle in degrees. The value is converted to the [0-359] range.

Parameters:
direction - The direction in degrees.

getSize

public double getSize()
Returns the scaling factor for the turtle where 1.0 is the default size and 2.0 is double the default size and 0.5 is half the default size.

Returns:
Scaling factor of the turtle.

setSize

public void setSize(double size)
Sets the scaling factor for the turtle where 1.0 is the default size and 2.0 is double the default size and 0.5 is half the default size.

Parameters:
size - Scaling factor of the turtle.

getRadius

public double getRadius()
Returns the radius corresponding to the current scaling factor of the turtle.

Returns:
The radius in pixels as a double.

setRadius

public void setRadius(double radius)
Set the scaling factor corresponding to the specified radius.

Parameters:
radius - The new radius to resize the turtle to.

getColor

public Color getColor()
Returns the main Color of the turtle (used to draw its body).

Returns:
The color of the body.

getLimbColor

public Color getLimbColor()
Returns the Color of the turtle's limbs (head and legs).

Returns:
The color of the limbs.

setColor

public void setColor(int red,
                     int green,
                     int blue)
Sets the color of the turtle to the specified RGB color.

Parameters:
red - The red component of the color. [0-255]
green - The green component of the color. [0-255]
blue - The blue component of the color. [0-255]

isVisible

public boolean isVisible()
Returns true if the turtle is visible and false if it's invisible.

Returns:
Visibility flag as a boolean.

setVisible

public void setVisible(boolean visible)
Sets the visibility status of the turtle.

Parameters:
visible - Visibility status. (false hides the turtle and true shows it.)

isPathEnabled

public boolean isPathEnabled()
Returns true if path drawing is enabled and false if it's disabled.

Returns:
Path drawing flag as a boolean.

enablePath

public void enablePath()
Enables drawing of the path when the turtle moves.


disablePath

public void disablePath()
Disables drawing of the path when the turtle moves.


distanceTo

public double distanceTo(int x,
                         int y)
Calculates the Euclidean distance between a turtle and a given other point.

Parameters:
x - The x-coordinate of the other point.
y - The y-coordinate of the other point.
Returns:
The Euclidean distance as a double.

toString

public String toString()
Generates a compact string representation of some of the turtle's attributes.

Example: Turtle(x: 57, y: 173, direction: 180, size: 2.00000,red: 128, green: 57, blue: 34).

Overrides:
toString in class Object
Returns:
The string representation.

moveTo

public void moveTo(int x,
                   int y)
Repositions the turtle to the given x- and y-coordinates. moveTo is the basic movement method which all other movement methods should call.

When moveTo is called, the following actions are performed:

Parameters:
x - X-coordinate the turtle will move to.
y - Y-coordinate the turtle will move to.

turn

public void turn(int degrees)
Turns the turtle clockwise by the given integer number of degrees. All int-values are valid and the value will wrap around correctly.

Parameters:
degrees - The number of degrees to turn. (Clockwise if positive and counter-clockwise if negative.)

turnTo

public void turnTo(int x,
                   int y)
Turns the turtle to face the point specified by the x- and y-coordinate.

Parameters:
x - X-coordinate of the point which to turn towards.
y - Y-coordinate of the point which to turn towards.

turnTo

public void turnTo(Turtle t)
Turns the turtle to face the position of another turtle.

Parameters:
t - The turtle to turn towards.

move

public void move(int step)
Moves the turtle along its current direction by an integer step-size.

Parameters:
step - The delta to move the turtle by. (Moves forward if positive and reverse if negative.)