com.oxfordmathcenter.breadboards
Class Turtle

java.lang.Object
  extended by acm.graphics.GObject
      extended by acm.graphics.GCompound
          extended by com.oxfordmathcenter.breadboards.Turtle
All Implemented Interfaces:
acm.graphics.GContainer, acm.graphics.GScalable, java.io.Serializable, java.lang.Cloneable

public class Turtle
extends acm.graphics.GCompound

Logo-inspired turtles for use with GBreadboard class which can move forward or reverse, turn left or right, change size, be visible or invisible, raise or lower a pen to trace it's path, change body or pen color, and change speed.

Author:
paul oser
See Also:
Serialized Form

Field Summary
 
Fields inherited from interface acm.graphics.GContainer
BACK_TO_FRONT, FRONT_TO_BACK
 
Constructor Summary
Turtle(double x, double y, OneButtonBreadboard gBreadboard)
          Constructs a new turtle positioned at (x,y) in the gBreadboard's drawing area
Turtle(OneButtonBreadboard gBreadboard)
          Constructs a new turtle in the center of gBreadboard's drawing area
 
Method Summary
 void erasePath()
          Removes all lines from the turtle's path
 void forward()
          Moves the turtle forward by its own size (i.e., the diameter of its body)
 void forward(double distance)
          Moves the turtle some number of units forward or reverse
 acm.graphics.GRectangle getBounds()
          Gets bounding rectangle for entire path traced by turtle.
 double getDefaultTurningAngle()
          Returns angle turtle should turn (in degrees) if no angle is specified in a call to right() or left()
 double getDirection()
          This method returns the turtle's heading as an angle (in radians)
 double getSpeed()
          Gets the speed of the turtle
 double getTurtleSize()
          Returns the size of the turtle (the diameter of its body)
 void hideTurtle()
          Makes the turtle invisible
 boolean isAtMaxSpeed()
          Returns whether or not the turtle is at maximum speed
 boolean isPenDown()
          Determines whether the tail is up or down.
 boolean isTurtleVisible()
          Returns whether the turtle is visible
 void left()
          Alters the turtle's direction by the default number of degrees, counter-clockwise about turtle's center
 void left(double degrees)
          Alters the turtle's direction by some number of degrees, counter-clockwise about turtle's center
static void main(java.lang.String[] args)
          Makes it possible for a Turtle to be run under the control of a GraphicsProgram object.
 void penDown()
          Sets the turtle's tail (which can be thought of as holding a pen) down, which means the turtle will draw its path
 void penUp()
          Sets the turtle's tail (which can be thought of as holding a pen) up, which means the turtle will not draw its path
 void right()
          Alters the turtle's direction by the default number of degrees, clockwise about turtle's center
 void right(double degrees)
          Alters the turtle's direction by some number of degrees, clockwise about turtle's center
 void setColor(java.awt.Color color)
          Set the color of the turtle's body and head
 void setDefaultTurningAngle(double angle)
          Sets angle turtle should turn (in degrees) if no angle is specified in a call to right() or left()
 void setDirection(double radians)
          Sets the heading for the turtle
 void setLocation(double x, double y)
          Moves/teleports the turtle to the point (x,y) without drawing a line.
 void setPenColor(java.awt.Color color)
          Set the color of the pen in the turtle's "tail"
 void setSize(int size)
          Sets the size of the turtle (i.e., the diameter of the body)
 void setSpeed(double speed)
          Set the speed of the turtle
 void showTurtle()
          Makes the turtle visible (the turtle itself is initially visible).
 java.lang.String toString()
          This method returns a text-description of the turtle, noting its position and direction
 void update()
          This method ensures the turtles position, orientation, color, and other attributes are reflected on the screen
 
Methods inherited from class acm.graphics.GCompound
add, add, add, contains, getCanvasPoint, getCanvasPoint, getElement, getElementAt, getElementAt, getElementCount, getLocalPoint, getLocalPoint, iterator, iterator, markAsComplete, paint, remove, removeAll, scale, scale
 
Methods inherited from class acm.graphics.GObject
addActionListener, addMouseListener, addMouseMotionListener, contains, fireActionEvent, fireActionEvent, getColor, getHeight, getLocation, getParent, getSize, getWidth, getX, getY, isVisible, move, movePolar, pause, removeActionListener, removeMouseListener, removeMouseMotionListener, sendBackward, sendForward, sendToBack, sendToFront, setLocation, setParent, setVisible
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Turtle

public Turtle(OneButtonBreadboard gBreadboard)
Constructs a new turtle in the center of gBreadboard's drawing area


Turtle

public Turtle(double x,
              double y,
              OneButtonBreadboard gBreadboard)
Constructs a new turtle positioned at (x,y) in the gBreadboard's drawing area

Parameters:
x - x-coordinate of center of turtle's body
y - y-coordinate of center of turtle's body
Method Detail

update

public void update()
This method ensures the turtles position, orientation, color, and other attributes are reflected on the screen


isAtMaxSpeed

public boolean isAtMaxSpeed()
Returns whether or not the turtle is at maximum speed

Returns:
true if turtle speed is essentially 0, false otherwise

forward

public void forward(double distance)
Moves the turtle some number of units forward or reverse

Parameters:
distance - number of units to move forward (if negative, turtle moves in reverse instead)

forward

public void forward()
Moves the turtle forward by its own size (i.e., the diameter of its body)


getBounds

public acm.graphics.GRectangle getBounds()
Gets bounding rectangle for entire path traced by turtle.

Overrides:
getBounds in class acm.graphics.GCompound
Returns:
the bounding rectangle for the entire path traced by the turtle.

left

public void left(double degrees)
Alters the turtle's direction by some number of degrees, counter-clockwise about turtle's center

Parameters:
degrees - number of degrees to turn

right

public void right(double degrees)
Alters the turtle's direction by some number of degrees, clockwise about turtle's center

Parameters:
degrees - number of degrees to turn

left

public void left()
Alters the turtle's direction by the default number of degrees, counter-clockwise about turtle's center


right

public void right()
Alters the turtle's direction by the default number of degrees, clockwise about turtle's center


setColor

public void setColor(java.awt.Color color)
Set the color of the turtle's body and head

Overrides:
setColor in class acm.graphics.GObject

setPenColor

public void setPenColor(java.awt.Color color)
Set the color of the pen in the turtle's "tail"

Parameters:
color - color of the pen in the turtle's "tail"

getDirection

public double getDirection()
This method returns the turtle's heading as an angle (in radians)

Returns:
the angle (in radians) that corresponds to the turtle's heading. 0 means the turtle is heading to the right, while pi/2 indicates the turtle is heading upwards.

setDirection

public void setDirection(double radians)
Sets the heading for the turtle

Parameters:
degrees - direction the heading of the turtle. When 0, the turtle heads to the right, while pi/2 heads the turtle upwards.

isPenDown

public boolean isPenDown()
Determines whether the tail is up or down. Recall, when the tail is down turtle draws it path, while when the tail is up nothing is drawn.

Returns:
true if tail is down, false otherwise

penDown

public void penDown()
Sets the turtle's tail (which can be thought of as holding a pen) down, which means the turtle will draw its path


penUp

public void penUp()
Sets the turtle's tail (which can be thought of as holding a pen) up, which means the turtle will not draw its path


toString

public java.lang.String toString()
This method returns a text-description of the turtle, noting its position and direction

Overrides:
toString in class acm.graphics.GObject
Returns:
a string similar in form to "Turtle at (50,100) with heading 3.1459"

setSpeed

public void setSpeed(double speed)
Set the speed of the turtle

Parameters:
speed - if speed is 0, the turtle will move slowly enough to see all of the movements; if speed is 1, the turtle will move and draw as fast as possible (possibly faster than the screen can update)

getSpeed

public double getSpeed()
Gets the speed of the turtle

Returns:
0 for slow enough to be seen, 1 for as fast as possible.

erasePath

public void erasePath()
Removes all lines from the turtle's path


hideTurtle

public void hideTurtle()
Makes the turtle invisible


showTurtle

public void showTurtle()
Makes the turtle visible (the turtle itself is initially visible).


setLocation

public void setLocation(double x,
                        double y)
Moves/teleports the turtle to the point (x,y) without drawing a line.

Overrides:
setLocation in class acm.graphics.GObject

getTurtleSize

public double getTurtleSize()
Returns the size of the turtle (the diameter of its body)

Returns:
size of the turtle (the diameter of its body)

setSize

public void setSize(int size)
Sets the size of the turtle (i.e., the diameter of the body)

Parameters:
size - the diameter of the turtle body

isTurtleVisible

public boolean isTurtleVisible()
Returns whether the turtle is visible

Returns:
true if visible, false otherwise

getDefaultTurningAngle

public double getDefaultTurningAngle()
Returns angle turtle should turn (in degrees) if no angle is specified in a call to right() or left()

Returns:
default turning angle in degrees

setDefaultTurningAngle

public void setDefaultTurningAngle(double angle)
Sets angle turtle should turn (in degrees) if no angle is specified in a call to right() or left()

Parameters:
angle - default turning angle in degrees

main

public static void main(java.lang.String[] args)
Makes it possible for a Turtle to be run under the control of a GraphicsProgram object.

Parameters:
args - An array of string arguments