|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectgalapagos.Turtle
public class Turtle
This Turtle draws geometric shapes a DrawingCanvas. This turtle will run in a separate Thread so multiple turtles can be drawing on a single canvas. Date Modified: February, 2009 Made the class up-to-date Java 5.0 and cleaned up the code a bit.
Field Summary | |
---|---|
static int |
CREATE_DEFAULT_WINDOW
Constant for creating a default drawing window |
private double |
currentOrientation
This turtle's current orientation. |
private Position |
currentPosition
This turtle's current position in the logical coordinate |
private int |
currentSpeed
This turtle's current speed |
private static java.awt.Color |
DEFAULT_COLOR
Default color used in drawing |
private static double |
DEFAULT_PENSIZE
Default pen size used in drawing |
private static int |
DEFAULT_SPEED
Default speed of this turtle; unit is logical units/sec |
private boolean |
inMotion
The motion flag: true makes turtle moving immediately after receiving command; otherwise the turtle is in the pause mode and will not move until told to move |
private java.util.Vector<TurtleCommand> |
instructionBuffer
This turtle's instruction buffer to store user commands. |
private java.util.Vector<TurtleCommand> |
memory
This turtle's memory to remember the past commands. |
private DrawingCanvas |
myCanvas
The DrawingCanvas which this turtle is drawing its trajectory. |
private java.awt.Graphics |
myGraphics
The Graphics object returned from the connected DrawingCanvas. |
private TurtleShape |
myShape
The shape of this turtle to draw on the canvas. |
static int |
NO_DEFAULT_WINDOW
Constant for not creating any default drawing window |
private static int |
PEN_DOWN
Constant for pen down state. |
private static int |
PEN_UP
Constant for pen up state |
private java.awt.Color |
penColor
This turtle's pen color |
private double |
penSize
The pen size of this turtle in logical units |
private int |
penState
The position (down or up) of this turtle's pen |
private TurtleDrawingWindow |
playGround
Default drawing window for this turtle. |
private double |
redrawCurrentOrientation
The orientation of this turtle during the redrawing phase |
private Position |
redrawCurrentPosition
The position of this turtle during the redrawing phase. |
private boolean |
showBody
The visibility flag: true makes turtle visible; otherwise invisible. |
private static int |
STEP_INCR
Number of logical units this turtle moves in one drawing |
Constructor Summary | |
---|---|
Turtle()
A default constructor that creates an instance of the Turtle class with a default drawing window. |
|
Turtle(int choice)
Constructs a standard turtle with or without default drawing window assigned. |
Method Summary | |
---|---|
void |
backup(double length)
Moves this turtle backward for a specified length. |
void |
bodyColor(java.awt.Color color)
Changes the body color of this turtle. |
void |
bodyShape(java.awt.Point[] point)
Sets the shape of this turtle to the give shape expressed in an array of Point objects. |
(package private) void |
changeOrientation(double orientation)
Sets the orientation of this turtle. |
(package private) void |
changePenColor(java.awt.Color color)
Sets the pen color to the specified color. |
(package private) void |
changePenSize(float size)
Sets the pen size to the specified size. |
(package private) void |
changePenState(int penState)
Sets the pen position of this turtle, true for pen down and false for pen up. |
(package private) void |
changePosition(Position newLocation)
Sets the current position of this turtle. |
(package private) void |
changeRedrawOrientation(double orientation)
Changes the orientation of the turtle when redrawing the commands in the 'memory' Vector |
(package private) void |
changeRedrawPosition(Position position)
Changes the position of the turtle while commands in the 'memory' vector are being executed. |
(package private) void |
changeSpeed(int speed)
Changes the speed of this turtle to the designated speed. |
(package private) void |
changeVisibility(boolean status)
Change the visibility of the Turtle body. |
(package private) void |
draw(double length)
Draws this turtle's trajectory for a specified length. |
(package private) void |
drawShape(double orientation)
Draw the turtle shape facing the given orientation |
(package private) void |
drawText(java.lang.String text)
Draws the specified text at the current position of the turtle. |
(package private) void |
drawTextMemory(java.lang.String text,
Position position)
Draws text specified by the DrawTextCommand command in the memory |
void |
forward(double length)
Moves this turtle forward for a given length. |
void |
heading(double degree)
Sets this turtle's heading for a specified degree. |
void |
hide()
Hides this turtle, that is, the turtle body is not displayed.. |
void |
init()
Initializes this turtle. |
private void |
initMemory()
Clears this turtle's memory. |
void |
jumpTo(double x,
double y)
Jumps to the given (x, y). |
void |
move(double length)
Moves this turtle's for a specified length. |
void |
moveTo(double targetX,
double targetY)
Moves this turtle to the specified target (x, y) position. |
void |
pause()
Stops this turtle from moving. |
void |
penColor(java.awt.Color color)
Sets the color of the turtle's pen. |
void |
penDown()
Sets the pen state to down. |
void |
penSize(int penSize)
Sets the size of this turtle's pen. |
void |
penUp()
Sets the pen state to up. |
(package private) void |
plot(Position target,
int penState,
java.awt.Color penColor,
double penSize)
If the pen is down, this method calls the DrawingCanvas plot method to draw a line between redrawCurrentPosition to target in the specified color and pen size. |
void |
print(java.lang.String message)
Prints the specified message at the current position. |
void |
redraw(java.awt.Graphics g)
Required method to implement the DrawingController interface. |
private void |
reset()
Resets this turtle to it's initial state |
(package private) void |
rotate(double degree)
Turns this turtle's orientation for a specified degree in counter clockwise direction. |
void |
run()
Required method to implement the Runnable interface. |
void |
setCanvas(DrawingCanvas canvas)
Required method to implement the DrawingController interface. |
void |
show()
Shows this turtle, that is, the turtle body is displayed. |
void |
speed(int speed)
Changes the speed of this turtle |
void |
start()
Starts moving this turtle in its own thread. |
private void |
startRunning()
Starts this turtle running in its own thread. |
void |
turn(double degree)
Turns the turtle |
(package private) void |
turnAndMove(Position target)
Implements the MoveTo command by using the changeOrientation and draw methods. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final int CREATE_DEFAULT_WINDOW
public static final int NO_DEFAULT_WINDOW
private static final int PEN_DOWN
private static final int PEN_UP
private static final int DEFAULT_SPEED
private static final double DEFAULT_PENSIZE
private static final java.awt.Color DEFAULT_COLOR
private static final int STEP_INCR
private int penState
private double penSize
private java.awt.Color penColor
private Position currentPosition
private double currentOrientation
private int currentSpeed
private java.awt.Graphics myGraphics
private DrawingCanvas myCanvas
private Position redrawCurrentPosition
private double redrawCurrentOrientation
private java.util.Vector<TurtleCommand> memory
private java.util.Vector<TurtleCommand> instructionBuffer
private TurtleShape myShape
private boolean showBody
private boolean inMotion
private TurtleDrawingWindow playGround
Constructor Detail |
---|
public Turtle()
public Turtle(int choice)
choice
- zero means no default window; non-zero will create a
default windowMethod Detail |
---|
public void backup(double length)
length
- the length to move backwardpublic void bodyColor(java.awt.Color color)
color
- a new body colorpublic void bodyShape(java.awt.Point[] point)
point
- an array of Point objects representing a polygonpublic void forward(double length)
length
- the length to move forwardpublic void heading(double degree)
degree
- the new orientation of this turtlepublic void hide()
public void init()
public void jumpTo(double x, double y)
x
- the x coordinate of the target positiony
- the y coordinate of the target positionpublic void move(double length)
public void moveTo(double targetX, double targetY)
targetX
- the x coordinate of the target pointtargetY
- the y coordinate of the target pointpublic void pause()
public void penColor(java.awt.Color color)
color
- a new pen color for subsequent drawingpublic void penDown()
public void penSize(int penSize)
penSize
- the new pen sizepublic void penUp()
public void print(java.lang.String message)
message
- the text to displaypublic void redraw(java.awt.Graphics g)
redraw
in interface DrawingController
g
- the Graphics object where this turtle can drawpublic void run()
run
in interface java.lang.Runnable
public void setCanvas(DrawingCanvas canvas)
setCanvas
in interface DrawingController
canvas
- the object that called this methodpublic void show()
public void speed(int speed)
speed
- the new speed of this turtlepublic void start()
public void turn(double degree)
void changeOrientation(double orientation)
orientation
- the direction of this turtle's headingvoid changePenColor(java.awt.Color color)
color
- the new colorvoid changePenSize(float size)
size
- the new pen sizevoid changePenState(int penState)
penState
- either Turtle.PEN_DOWN or Turtle.PEN_UPvoid changePosition(Position newLocation)
newLocation
- the new location of this turtlevoid changeRedrawOrientation(double orientation)
orientation
- the new orientation of this turtlevoid changeRedrawPosition(Position position)
position
- the new positionvoid changeSpeed(int speed)
speed
- the new moving speed of this turtlevoid changeVisibility(boolean status)
status
- true to show Turtle body; false to hide the bodyvoid draw(double length)
void drawShape(double orientation)
void drawText(java.lang.String text)
text
- void drawTextMemory(java.lang.String text, Position position)
text
- the text to displayposition
- the position on the canvas to display the textvoid plot(Position target, int penState, java.awt.Color penColor, double penSize)
target
- the next starting position for redrawing.penState
- the state of the pen (UP or DOWN).penColor
- the pen color.penSize
- the thickness of the pen.void rotate(double degree)
degree
- the amount of degree this turtle rotates from
the current orientationvoid turnAndMove(Position target)
target
- a target position this turtle moves to
from its current position.private void initMemory()
private void reset()
private void startRunning()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |