Turtle is a selfcontained class that will allow students to make beautiful turtle graphics with ease.
Constructor Summary
Turtle()
Makes a default turtle.
Turtle(double x, double y)
Makes a default turtle at the specified position.
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Constructor Detail
public Turtle()
Makes a default turtle.
public Turtle(double x, double y)
Makes a default turtle at the specified position.
Parameters:
x - x coordinate
y - y coordinate
Method Detail
public static void exit()
public static void startApplet(javax.swing.JApplet applet)
This is an experimental method that should allow you to make turtle applets in the future. For now, it doesn't work because the key and mouse bindings require reflection and applets think that allowing reflection would open a security hole. Theoretically in the init method of the applet you need to place Turtle.startApplet(this);. This is not currently working.
Parameters:
applet -
public Turtle clone()
This creates a cloned copy of a turtle.
Overrides:
clone in class java.lang.Object
Returns:
a cloned copy of a turtle
public boolean contains(int x, int y)
Determines if a turtle is covering a screen position
Parameters:
x - x screen coordinate
y - y screen coordinate
Returns:
true if this turtle is at the indicated screen position.
public double getSpeed()
Gets the speed of the animation.
Returns:
milliseconds it takes to do one action
public long speed(double delay)
Sets the speed of the animation.
Parameters:
delay - milliseconds it takes to do one action
Returns:
state change timestamp
public long forward(double distance)
Moves the turtle forward by the distance.
Parameters:
distance - distance to move forward
Returns:
state change timestamp
public long backward(double distance)
Moves the turtle backward by the distance.
Parameters:
distance - distance to move backward
Returns:
state change timestamp
public long left(double angle)
Turns the turtle left by the number of indicated degrees.
Parameters:
angle - angle in degrees
Returns:
state change timestamp
public long right(double angle)
Turns the turtle right by the number of indicated degrees.
Parameters:
angle - angle in degrees
Returns:
state change timestamp
public double getDirection()
Gets the direction the turtle is facing neglecting tilt.
Returns:
state change timestamp
public long setDirection(double direction)
Sets the direction the turtle is facing neglecting tilt.
Parameters:
direction - angle counter-clockwise from east
Returns:
state change timestamp
public long home()
Moves the turtle to (0,0) and facing east.
Returns:
state change timestamp
public long hide()
Hides the turtle but it can still draw.
Returns:
state change timestamp
public long show()
Makes the turtle visible.
Returns:
state change timestamp
public long face(double x, double y)
Sets the direction in such a way that it faces (x,y)
Parameters:
x - x coordinate of target location
y - y coordinate of target location
Returns:
state change timestamp
public double towards(double x, double y)
Gets direction towards (x,y)
Parameters:
x - x coordinate of target location
y - y coordinate of target location
Returns:
angle in degrees where 0<=angle<360
public double distance(double x, double y)
Gets the distance to another position.
Parameters:
x - x coordinate of target location
y - y coordinate of target location
Returns:
distance between turtle's current location and another position
public double getX()
Gets the x coordinate of the turtle.
Returns:
x coordinate
public double getY()
Gets the y coordinate of the turtle.
Returns:
y coordinate
public long setPosition(double x, double y, double direction)
Sets the position and direction of a turtle.
Parameters:
x - x coordinate
y - y coordinate
direction - angle counter-clockwise from east in degrees
Returns:
state change timestamp
public long setPosition(double x, double y)
Sets the position and direction of a turtle.
Parameters:
x - x coordinate
y - y coordinate
Returns:
state change timestamp
public long tilt(double angle)
Adds an additional angle to rotation of the turtle's shape when rendering. This is useful when you need to face a different direction than the direction you are moving in.
Parameters:
angle - angle in degrees
Returns:
state change timestamp
public long setTilt(double angle)
Sets the angle to rotate the turtle's shape when rendering. This is useful when you need to face a different direction than the direction you are moving in.
Parameters:
angle - angle in degrees
Returns:
state change timestamp
public double getTilt()
Gets the rotation of the turtle's shape away from the turtle's direction.
Returns:
tilt in degrees (positive in counter-clockwise)
public long width(double penWidth)
Sets the width of the turtles path
Parameters:
penWidth - width of the turtles path
Returns:
state change timestamp
public long outlineWidth(double width)
Sets the width of the turtle's outline.
Parameters:
width - width of the turtle's outline
Returns:
state change timestamp
public long up()
Picks the turtle's tail up so it won't draw on the screen as it moves.
Returns:
state change timestamp
public long down()
Puts the turtle's tail down so it will draw on the screen as it moves.
Returns:
state change timestamp
public long penColor(java.lang.String penColor)
Sets the turtle's path color.
Parameters:
penColor - Color of the turtle's path.
Returns:
state change timestamp
public long penColor(java.awt.Color penColor)
Sets the turtle's path color.
Parameters:
penColor - Color of the turtle's path.
Returns:
state change timestamp
public long outlineColor(java.lang.String outlineColor)
Sets the turtle's outlineColor color.
Parameters:
outlineColor - Color of the turtle's outlineColor.
Returns:
state change timestamp
public long outlineColor(java.awt.Color outlineColor)
Sets the turtle's outlineColor color.
Parameters:
outlineColor - Color of the turtle's outlineColor.
Returns:
state change timestamp
public long fillColor(java.lang.String fillColor)
Sets the turtle's fill color.
Parameters:
fillColor - Color of the turtle's fill.
Returns:
state change timestamp
public long fillColor(java.awt.Color fillColor)
Sets the turtle's fill color.
Parameters:
fillColor - Color of the turtle's fill.
Returns:
state change timestamp
public long shape(java.lang.String shape)
Sets the shape of the turtle using the built in shapes (turtle,square, rectangle,triangle,arrow,circle) or to a image.
Parameters:
shape - shapename or filename of image
Returns:
state change timestamp
public long shapeSize(int width, int height)
public long stamp()
Put a copy of the current turtle shape on the canvas.
Returns:
state change timestamp
public long dot()
Put a dot 3 times the size of the penWidth on the canvas.
Returns:
state change timestamp
public long dot(java.lang.String color)
Put a dot 3 times the size of the penWidth on the canvas.
Parameters:
color - color of dot
Returns:
state change timestamp
public long dot(java.awt.Color color)
Put a dot 3 times the size of the penWidth on the canvas.
Parameters:
color - color of dot
Returns:
state change timestamp
public long dot(java.lang.String color, double dotSize)
Put a dot on the canvas.
Parameters:
color - color of dot
dotSize - diameter of the dot
Returns:
state change timestamp
public long dot(java.awt.Color color, double dotSize)
Put a dot on the canvas.
Parameters:
color - color of dot
dotSize - diameter of the dot
Returns:
state change timestamp
public long write(java.lang.String text, java.lang.String fontName, int fontSize, int justification, double xOffset, double yOffset)
public void undo(int steps)
Undo turtle state changes.
Parameters:
steps - the number of state changes to remove
public void undo()
Undo the last turtle state change.
public void redo(int steps)
Redo turtle state changes.
Parameters:
steps - the number of state changes to restore
public void redo()
Redo turtle state changes.
public void clear()
Clears all the drawing that a turtle has done but all the turtle settings remain the same. (color, location, direction, shape)
public static void refreshMode(int mode)
This specifies when the screen gets refreshed. 0(default)=Animated (The turtle will slide from one state to another without being jerky.) 1=State Change (The turtle will refresh immediately to the last state. Jerky motion.) 2=On Demand (The turtle will refresh only when you call update())
Parameters:
mode - refresh mode
public static void backgroundMode(int mode)
This specifies how the background is drawn. 0=The image if present is stretched to fill the screen. 1=The image is centered on the middle of the screen and will not scale/pan 2=The image is tiled and will not scale/pan 3=The image is centered on (0,0) and will scale/pan 4(default)=The image is tiled and will scale/pan
Parameters:
mode - background mode
public static void bgcolor(java.lang.String color)
Sets the background color.
Parameters:
color - Color of the background.
public static void bgcolor(java.awt.Color color)
Sets the background color.
Parameters:
color - Color of the background.
public static void bgpic(java.lang.String filename)
Set the background image.
Parameters:
filename - filename for a background image
public boolean onKey(java.lang.String methodName, java.lang.String keyText)
Links a method to a key.
Parameters:
methodName - method to be executed when the key is pressed
keyText - key that triggers the method
Returns:
public boolean onKey(java.lang.String methodName, java.lang.String keyText, boolean append)
Links a method to a key.
Parameters:
methodName - method to be executed when the key is pressed
keyText - key that triggers the method
append - true if you want to have multiple methods per key
Returns:
public boolean onKey(java.lang.String methodName, java.lang.String keyText, boolean append, boolean repeat)
Links a method to a key.
Parameters:
methodName - method to be executed when the key is pressed
keyText - key that triggers the method
append - true if you want to have multiple methods per key
repeat - true if you want call the method every screen refresh
Returns:
public static void zoom(double minx, double miny, double maxx, double maxy)
Fits the indicated box in the center of the screen as large as possible.
Parameters:
minx - left x coordinate of box
miny - bottom y coordinate of box
maxx - right x coordinate of box
maxy - top y coordinate of box
public static void zoomFit()
Fits everything on the screen.
public static void update()
Force redraw when the refreshMode is set to on demand.
public static void setCanvasSize(int width, int height)
Changes the size of the canvas effectively changing the size of the window.
Parameters:
width - width of the canvas
height - height of the canvas
public static void save(java.lang.String filename)
Saves the visible canvas to an image.
Parameters:
filename - image filename
public static void main(java.lang.String[] a)
Demo program
Parameters:
a - commandline args
public static java.lang.String[] keysDown()
Get the pressed keys.
Returns:
a list of pressed keys
public static boolean isKeyDown(java.lang.String key)
Test if a key is pressed or not.
Parameters:
key - key you are testing
Returns:
true if the key is pressed
public static int mouseX()
Get the mouse x coordinate using the screens coordinate system.
Returns:
x coordinate
public static int mouseY()
Get the mouse y coordinate using the screens coordinate system.
Returns:
y coordinate
public static boolean mouseButton()
Check to see if a mouse button is down.
Returns:
true if a button is down
public static boolean mouseButton1()
Check to see if the first mouse button is down.
Returns:
true if button 1 is down
public static boolean mouseButton2()
Check to see if the second mouse button is down.
Returns:
true if button 2 is down
public static boolean mouseButton3()
Check to see if the third mouse button is down.
Returns:
true if button 3 is down
public static double canvasX(double screenX)
Converts screen coordinates to canvas coordinates.
Parameters:
screenX - screen x coordinate
Returns:
canvas x coordinate
public static double canvasY(double screenY)
Converts screen coordinates to canvas coordinates.
Parameters:
screenY - screen y coordinate
Returns:
canvas y coordinate