acm.graphics
Class GTurtle

java.lang.Object
  extended by acm.graphics.GObject
      extended by acm.graphics.GTurtle

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.


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
 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.
 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.
void main(String[] args)
Makes it possible for a GTurtle to be run under the control of a GraphicsProgram 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.
 
Inherited Method Summary
ColorgetColor()
Returns the color used to display this object.
GPointgetLocation()
Returns the location of this object as a GPoint.
GDimensiongetSize()
Returns the size of the bounding box for this object.
doublegetX()
Returns the x-coordinate of the object.
doublegetX()
Returns the x-coordinate of the object.
doublegetY()
Returns the y-coordinate of the object.
doublegetY()
Returns the y-coordinate of the object.
booleanisVisible()
Checks to see whether the trace of the turtle is visible.
voidsendBackward()
Moves this object one step toward the back in the z dimension.
voidsendForward()
Moves this object one step toward the front in the z dimension.
voidsendToBack()
Moves this object to the back of the display in the z dimension.
voidsendToFront()
Moves this object to the front of the display in the z dimension.
voidsetVisible(boolean visible)
Sets whether the trace of the turtle is visible.
 

Constructor Detail

public GTurtle()

Creates a new GTurtle object.

 
Usage: GTurtle turtle = new GTurtle(); 
 


public GTurtle(double x, double y)

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

 
Usage: GTurtle turtle = new GTurtle(x, y); 
Parameters: 
 The initial x coordinate of the turtle
 The initial y coordinate of the turtle
 

Method Detail

public void erasePath()

Erases the entire path drawn by the turtle but does not change its position.

 
Usage: turtle.erasePath(); 
 


public final void forward()

Moves the turtle forward by its own size.

 
Usage: turtle.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.

 
Usage: turtle.forward(distance); 
Parameter: 
distance  The distance to move in the current direction
 


public GRectangle getBounds()

Returns the bounding box for the entire figure traced by the turtle.

 
Usage: bounds = getBounds(); 
Returns: A GRectangle representing the bounding box
 

Specified by:
getBounds in class GObject

public double getDirection()

Returns the direction specified by setDirection.

 
Usage: double dir = turtle.getDirection(); 
Returns: The direction in which the turtle is moving
 


public double getSpeed()

Returns the current speed of the turtle.

 
Usage: double speed = turtle.getSpeed(); 
Returns: The current speed of the turtle (0 is slowest, 1 is fastest)
 


public int getTurtleSize()

Returns the size of the turtle.

 
Usage: int size = turtle.getSize(); 
Returns: The size of the turtle image in pixels
 


public final void hideTurtle()

Hides the turtle.

 
Usage: turtle.hideTurtle(); 
 


public boolean isPenDown()

Returns whether the pen is down, as described in setPenDown.

 
Usage: if (turtle.isPenDown()) . . . 
Returns: true if the pen is down, false otherwise
 


public boolean isTurtleVisible()

Returns whether the turtle is visible.

 
Usage: if (turtle.isTurtleVisible()) . . . 
Returns: true if the turtle is visible, otherwise false
 


public final void left(double angle)

Changes the direction of the turtle so that it moves the specified number of degrees to the left.

 
Usage: turtle.left(angle); 
Parameter: 
angle  The angle to turn, measured in degrees
 


public static void main(String[] args)

Makes it possible for a GTurtle to be run under the control of a GraphicsProgram object.

 
Usage: main(args); 
Parameter: 
args  An array of string arguments
 


public final void penDown()

Lowers the pen.

 
Usage: turtle.penDown(); 
 


public final void penUp()

Raises the pen.

 
Usage: turtle.penUp(); 
 


public final void right(double angle)

Changes the direction of the turtle so that it moves the specified number of degrees to the right.

 
Usage: turtle.right(angle); 
Parameter: 
angle  The angle to turn, measured in degrees
 


public void setColor(Color color)

Sets the color of the turtle's pen.

 
Usage: turtle.setColor(color); 
Parameter: 
color  The new color for the pen
 


public void setDirection(double dir)

Sets the direction in which the turtle is moving.

 
Usage: turtle.setDirection(dir); 
Parameter: 
dir  The direction in which the turtle is moving
 


public void setLocation(double x, double y)

Moves the turtle to the point (x, y) without drawing a line.

 
Usage: turtle.setLocation(x, y); 
Parameters: 
 The x-coordinate of the new position
 The y-coordinate of the new position
 


public void setSize(int size)

Sets the size of the turtle.

 
Usage: turtle.setSize(size); 
Parameter: 
size  The size of the turtle image in pixels
 


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.

 
Usage: turtle.setSpeed(speed); 
Parameter: 
speed  The speed of the turtle (0 is slowest, 1 is fastest)
 


public final void showTurtle()

Makes the turtle visible.

 
Usage: turtle.showTurtle();