|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object acm.graphics.GObject acm.graphics.GTurtle
public class GTurtle
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 | |
---|---|
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 |
---|
public GTurtle()
GTurtle
object.
public GTurtle(double x, double y)
GTurtle
positioned at the point
(x
, y
).
x
- The initial x coordinate of the turtley
- The initial y coordinate of the turtleMethod Detail |
---|
public boolean contains(double x, double y)
GTurtle
object to avoid having the
trace intercept mouse clicks.
contains
in class GObject
x
- The x-coordinate of the point being testedy
- The y-coordinate of the point being tested
true
if the point (x
, y
) is inside
the object, and false
otherwiseprotected Image createTurtleImage(double dir)
dir
- The direction in which the turtle is heading
protected void drawTurtle(Graphics g)
g
- The graphics context in which to draw the turtlepublic void erasePath()
public final void forward()
public void forward(double distance)
distance
- The distance to move in the current directionpublic GRectangle getBounds()
getBounds
in class GObject
GRectangle
representing the bounding boxpublic double getDirection()
setDirection
.
public double getSpeed()
protected Rectangle getTurtleBounds()
public int getTurtleSize()
public final void hideTurtle()
public boolean isPenDown()
setPenDown
.
true
if the pen is down, false
otherwisepublic boolean isTurtleVisible()
true
if the turtle is visible, otherwise false
public final void left(double angle)
angle
- The angle to turn, measured in degreespublic static void main(String[] args)
GTurtle
to be run under the control
of a GraphicsProgram
object.
args
- An array of string argumentspublic void move(double dx, double dy)
move
in class GObject
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)public void paint(Graphics g)
paint
operation for this graphical object. This method
is not called directly by clients.
paint
in class GObject
g
- The graphics context into which the painting is donepublic final void penDown()
public final void penUp()
public final void right(double angle)
angle
- The angle to turn, measured in degreespublic void setColor(Color color)
setColor
in class GObject
color
- The new color for the penpublic void setDirection(double dir)
dir
- The direction in which the turtle is movingpublic void setLocation(double x, double y)
x
, y
) without drawing a line.
setLocation
in class GObject
x
- The x-coordinate of the new positiony
- The y-coordinate of the new positionpublic void setSize(int size)
size
- The size of the turtle image in pixelspublic void setSpeed(double speed)
speed
- The speed of the turtle (0 is slowest, 1 is fastest)public final void showTurtle()
protected void start()
GraphicsProgram
containing this object.
start
in class GObject
protected void start(String[] args)
GraphicsProgram
containing this object, passing
it the specified arguments.
start
in class GObject
args
- The array of arguments
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |