public class AnimatedTurtle extends Turtle implements Runnable
speed(long)
)Turtle.UnderTheShell
Modifier and Type | Field and Description |
---|---|
static long |
DEFAULT_FRAME_DELAY
10 milliseconds
|
static long |
TURN_SPEED_CUTOFF
25 milliseconds
|
DEFAULT_HEADING_IN_DEGREES,
DEFAULT_HIDDEN, DEFAULT_PEN_COLOR,
DEFAULT_PEN_DOWN, DEFAULT_PEN_WIDTH,
EAST, NORTH, SOUTH, UNDER_THE_SHELL, WEST
Constructor and Description |
---|
AnimatedTurtle()
Construct an animated turtle with
DEFAULT_FRAME_DELAY |
AnimatedTurtle(long frameDelay)
Construct an animated turtle with a custom frame delay
|
AnimatedTurtle(long frameDelay,
Terrarium terrarium)
Construct an animated turtle with a custom frame delay in a specific terrarium
|
AnimatedTurtle(Terrarium terrarium)
Construct an animated turtle with
DEFAULT_FRAME_DELAY in a specific terrarium |
Modifier and Type | Method and Description |
---|---|
void |
draw(Graphics2D context,
Terrarium.UnderTheSurface key)
Draw the turtle
|
void |
head(double heading)
Turn the turtle to a particular heading
|
void |
hide()
Hide the turtle
|
void |
home()
Reset the turtle to its home position
|
void |
move(double steps)
Move the turtle in the direction of its current heading
|
void |
moveTo(double x,
double y)
Move the turtle to a particular location
|
void |
penColor(Color color)
Set the color of the turtle's pen
|
void |
penDown()
Lower the turtle's pen, causing it to leave a trail
|
void |
penUp()
Lift the turtle's pen up, causing it not to leave a track
|
void |
penWidth(double width)
Set the width of the turtle's pen
|
void |
run()
Thread execution
|
void |
show()
Show the turtle (if it was hidden)
|
void |
sp(long frameDelay)
Alias for
speed(long) |
void |
speed(long frameDelay)
Set the speed of the turtle's animation
|
void |
teleport(double x,
double y)
Move the turtle instantaneously to a particular location
|
void |
turn(double angle)
Turn the turtle from its current heading
|
back, bk, drawIcon,
fd, forward, getHeadingInDegrees,
getHeadingInRadians,
getPenColor, getPenStroke, getPenWidth, getTerrarium, getX, getY, hd, ht, isHidden, isPenDown, left, lt, pc, pd, pu, pw, right, rt, setTerrarium,
st, to, tp
public static final long DEFAULT_FRAME_DELAY
public static final long TURN_SPEED_CUTOFF
25 milliseconds
This is an internal measure, sued to determine how and when to animate turtle turns (frame delays longer than this cutoff will have the turns animated in larger steps
public AnimatedTurtle()
DEFAULT_FRAME_DELAY
public AnimatedTurtle(long frameDelay)
frameDelay
- between frames of animation, measured in millisecondspublic AnimatedTurtle(Terrarium terrarium)
DEFAULT_FRAME_DELAY
in a specific terrariumterrarium
- to house the turtlepublic AnimatedTurtle(long frameDelay, Terrarium terrarium)
frameDelay
- between frames of animation, measured in millisecondsterrarium
- to house the turtlepublic void move(double steps)
Turtle
Move the turtle in the direction of its current heading
A positive value for steps
is interpreted as forward movement and a negative value as backward
movement
public void turn(double angle)
Turtle
Turn the turtle from its current heading
A positive angle is interpreted as a right turn and a negative angle is a left turn. This is mildly
surprising if you know about the unit circle, but is because the Y-axis of the window increases from top to bottom,
which results in a mirrored unit circle around the X-axis, with 90° at the Turtle.SOUTH
and270° at the
Turtle.NORTH
public void head(double heading)
Turtle
Turn the turtle to a particular heading
Note that, because the Y-axis of the window increases from top to bottom, the usual angles of the unit circle
have been mirrored around the X-axis, with 90° at the Turtle.SOUTH
and 270%deg; at the Turtle.NORTH
.
Convenience constants have been provided for the cardinal directions.
public void penUp()
Turtle
public void penDown()
Turtle
public void penColor(Color color)
Turtle
Set the color of the turtle's pen
Color values are given as Color
values. Color
has a number of helpful constants like
Color.GREEN
or Color.GREEN
. Custom colors can also be constructed. Refer to the
java.awt.Color
API documentation more details (including how to create transparent colors!)
public void penWidth(double width)
Turtle
public void hide()
Turtle
Hide the turtle
Hiding the turtle causes it to become invisible — it can still be moved and create tracks, but the turtle itself is not visible
public void show()
Turtle
public void teleport(double x, double y)
Turtle
Move the turtle instantaneously to a particular location
The turtle moves directly to the window coordinates (x
, y
). Note that the origin of
the wind ow is in the top, left corner and that, while the X-axis increases from left to right, the Y-axis
increases from to to bottom.
No track is left by a teleportation.
public void moveTo(double x, double y)
Turtle
Move the turtle to a particular location
The turtle moves directly to the window coordinates (x
, y
). Note that the origin of
the wind ow is in the top, left corner and that, while the X-axis increases from left to right, the Y-axis
increases from to to bottom.
If the turtle's pen is currently down, the move will create a track from the old location to the new location.
public void home()
Turtle
Reset the turtle to its home position
The turtle's home position is at the center of the window, heading Turtle.EAST
public void sp(long frameDelay)
speed(long)
frameDelay
- in millisecondspublic void speed(long frameDelay)
Set the speed of the turtle's animation
The frame delay is the time between individual frames of animation. Shorter frame delays
(below TURN_SPEED_CUTOFF
) will cause turn animations to be animated in more detail to allow them to remain
visible to the naked eye
frameDelay
- in millisecondspublic void run()
Thread execution
This method is the control loop that allows animation to be updated in a separate control loop for each turtle. This method should not be called by students (although calling it manually should do nothing).
public void draw(Graphics2D context, Terrarium.UnderTheSurface key)
Turtle
Draw the turtle
May only be called by Terrarium
and its subclasses, enforced by Terrarium.UnderTheSurface
Copyright © 2018. All rights reserved.