acm.graphics
Class GTurtle

java.lang.Object
  extended by acm.graphics.GObject
      extended by acm.graphics.GTurtle
All Implemented Interfaces:
Serializable, Cloneable

public class GTurtle
extends GObject

The GTurtle class simulates a turtle moving on a canvas. This class is similar to the GPen class, but keeps track of both a position and a direction.

See Also:
Serialized Form

Constructor Summary
GTurtle()
          Creates a new GTurtle object.
GTurtle(double x, double y)
          Creates a new GTurtle positioned at the point (x, y).
 
Method Summary
 boolean contains(double x, double y)
          Contains is defined to be false for the GTurtle object to avoid having the trace intercept mouse clicks.
protected  Image createTurtleImage(double dir)
          This method creates (or retrieves) an image of the turtle in the specified direction.
protected  void drawTurtle(Graphics g)
          This method draws a representation of the turtle at the current location.
 void erasePath()
          Erases the entire path drawn by the turtle but does not change its position.
 void forward()
          Moves the turtle forward by its own size.
 void forward(double distance)
          Moves the turtle forward in its current direction by the specified distance.
 GRectangle getBounds()
          Returns the bounding box for the entire figure traced by the turtle.
 double getDirection()
          Returns the direction specified by setDirection.
 double getSpeed()
          Returns the current speed of the turtle.
protected  Rectangle getTurtleBounds()
          This method returns the bounds that the turtle occupies.
 int getTurtleSize()
          Returns the size of the turtle.
 void hideTurtle()
          Hides the turtle.
 boolean isPenDown()
          Returns whether the pen is down, as described in setPenDown.
 boolean isTurtleVisible()
          Returns whether the turtle is visible.
 void left(double angle)
          Changes the direction of the turtle so that it moves the specified number of degrees to the left.
static void main(String[] args)
          Makes it possible for a GTurtle to be run under the control of a GraphicsProgram object.
 void move(double dx, double dy)
          Moves the turtle by the specified displacements.
 void paint(Graphics g)
          Implements the paint operation for this graphical object.
 void penDown()
          Lowers the pen.
 void penUp()
          Raises the pen.
 void right(double angle)
          Changes the direction of the turtle so that it moves the specified number of degrees to the right.
 void setColor(Color color)
          Sets the color of the turtle's pen.
 void setDirection(double dir)
          Sets the direction in which the turtle is moving.
 void setLocation(double x, double y)
          Moves the turtle to the point (x, y) without drawing a line.
 void setSize(int size)
          Sets the size of the turtle.
 void setSpeed(double speed)
          Sets the speed of the turtle, which must be a number between 0 (slowest) and 1 (fastest).
 void showTurtle()
          Makes the turtle visible.
protected  void start()
          Starts a GraphicsProgram containing this object.
protected  void start(String[] args)
          Starts a GraphicsProgram containing this object, passing it the specified arguments.
 
Methods inherited from class acm.graphics.GObject
addActionListener, addMouseListener, addMouseMotionListener, areMouseListenersEnabled, colorName, contains, fireActionEvent, fireActionEvent, fireMouseListeners, getColor, getComponent, getHeight, getLocation, getObjectColor, getParent, getSize, getWidth, getX, getY, isVisible, movePolar, paintObject, paramString, pause, removeActionListener, removeMouseListener, removeMouseMotionListener, repaint, sendBackward, sendForward, sendToBack, sendToFront, setLocation, setParent, setVisible, toString, updateEnabledList
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

GTurtle

public GTurtle()
Creates a new GTurtle object.


GTurtle

public GTurtle(double x,
               double y)
Creates a new GTurtle positioned at the point (x, y).

Parameters:
x - The initial x coordinate of the turtle
y - The initial y coordinate of the turtle
Method Detail

contains

public boolean contains(double x,
                        double y)
Contains is defined to be false for the GTurtle object to avoid having the trace intercept mouse clicks.

Overrides:
contains in class GObject
Parameters:
x - The x-coordinate of the point being tested
y - The y-coordinate of the point being tested
Returns:
true if the point (xy) is inside the object, and false otherwise

createTurtleImage

protected Image createTurtleImage(double dir)
This method creates (or retrieves) an image of the turtle in the specified direction. Subclasses can change this method to create different images, but the images must be square.

Parameters:
dir - The direction in which the turtle is heading
Returns:
The image used to draw the turtle

drawTurtle

protected void drawTurtle(Graphics g)
This method draws a representation of the turtle at the current location. Subclasses can override this method to draw fancier turtles.

Parameters:
g - The graphics context in which to draw the turtle

erasePath

public void erasePath()
Erases the entire path drawn by the turtle but does not change its position.


forward

public final void forward()
Moves the turtle forward by its own size.


forward

public void forward(double distance)
Moves the turtle forward in its current direction by the specified distance. The turtle draws a line if its pen is down, but otherwise simply moves.

Parameters:
distance - The distance to move in the current direction

getBounds

public GRectangle getBounds()
Returns the bounding box for the entire figure traced by the turtle.

Specified by:
getBounds in class GObject
Returns:
A GRectangle representing the bounding box

getDirection

public double getDirection()
Returns the direction specified by setDirection.

Returns:
The direction in which the turtle is moving

getSpeed

public double getSpeed()
Returns the current speed of the turtle.

Returns:
The current speed of the turtle (0 is slowest, 1 is fastest)

getTurtleBounds

protected Rectangle getTurtleBounds()
This method returns the bounds that the turtle occupies.


getTurtleSize

public int getTurtleSize()
Returns the size of the turtle.

Returns:
The size of the turtle image in pixels

hideTurtle

public final void hideTurtle()
Hides the turtle.


isPenDown

public boolean isPenDown()
Returns whether the pen is down, as described in setPenDown.

Returns:
true if the pen is down, false otherwise

isTurtleVisible

public boolean isTurtleVisible()
Returns whether the turtle is visible.

Returns:
true if the turtle is visible, otherwise false

left

public final void left(double angle)
Changes the direction of the turtle so that it moves the specified number of degrees to the left.

Parameters:
angle - The angle to turn, measured in degrees

main

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

Parameters:
args - An array of string arguments

move

public void move(double dx,
                 double dy)
Moves the turtle by the specified displacements. The turtle draws a line if its pen is down, but otherwise simply moves.

Overrides:
move in class GObject
Parameters:
dx - The distance to move the object in the x direction (positive is rightward)
dy - The distance to move the object in the y direction (positive is downward)

paint

public void paint(Graphics g)
Implements the paint operation for this graphical object. This method is not called directly by clients.

Specified by:
paint in class GObject
Parameters:
g - The graphics context into which the painting is done

penDown

public final void penDown()
Lowers the pen.


penUp

public final void penUp()
Raises the pen.


right

public final void right(double angle)
Changes the direction of the turtle so that it moves the specified number of degrees to the right.

Parameters:
angle - The angle to turn, measured in degrees

setColor

public void setColor(Color color)
Sets the color of the turtle's pen.

Overrides:
setColor in class GObject
Parameters:
color - The new color for the pen

setDirection

public void setDirection(double dir)
Sets the direction in which the turtle is moving.

Parameters:
dir - The direction in which the turtle is moving

setLocation

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

Overrides:
setLocation in class GObject
Parameters:
x - The x-coordinate of the new position
y - The y-coordinate of the new position

setSize

public void setSize(int size)
Sets the size of the turtle.

Parameters:
size - The size of the turtle image in pixels

setSpeed

public void setSpeed(double speed)
Sets the speed of the turtle, which must be a number between 0 (slowest) and 1 (fastest). Setting speed to a value less than one makes the turtle move slowly, thereby making it easy to see exactly how a figure is being drawn.

Parameters:
speed - The speed of the turtle (0 is slowest, 1 is fastest)

showTurtle

public final void showTurtle()
Makes the turtle visible.


start

protected void start()
Starts a GraphicsProgram containing this object.

Overrides:
start in class GObject

start

protected void start(String[] args)
Starts a GraphicsProgram containing this object, passing it the specified arguments.

Overrides:
start in class GObject
Parameters:
args - The array of arguments