smithers.extras
Class TurtleGraphics
java.lang.Object
smithers.extras.TurtleGraphics
public class TurtleGraphics
- extends java.lang.Object
Draws simple vector graphics using a 'turtle' and saves to an SVG file. The
turtle has 2 unchanging parameters, angle and
distance The turtle operates by reading a string of characters
and performing the following actions:
Name | Default character | Action |
| Turn Left | + | Rotates the turtle left by
angle. |
| Turn Right | - | Rotates the turtle right by
angle. |
| Forwards | F | Moves the turtle forwards by distance,
leaving a line behind it. |
| Forwards Nodraw | f | Moves the turtle forwards by
distance. |
| Backwards | B | Moves the turtle backwards by
distance, leaving a line behind it. |
| Backwards Nodraw | b | Moves the turtle backwards by
distance. |
| Push | [ | Pushes the turtle's current position and
direction onto the stack. |
| Pop | ] | Retrieves the turtle's current position and
direction from the stack. |
|
Constructor Summary |
TurtleGraphics(double angle,
double distance)
Creates a new turtle with the specified angle and distance and the
default command characters. |
TurtleGraphics(double angle,
double distance,
char turnLeft,
char turnRight,
char forwards,
char forwardsNodraw,
char backwards,
char backwardsNodraw,
char push,
char pop)
Creates a new turtle according to specified options. |
|
Method Summary |
void |
doTurtle(java.lang.String code,
double x,
double y,
double dir,
double width,
double height,
java.lang.String fileName)
Draws the turtle graphics and saves an svg file. |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
TurtleGraphics
public TurtleGraphics(double angle,
double distance)
- Creates a new turtle with the specified angle and distance and the
default command characters.
- Parameters:
angle - the angle to turn bydistance - the distance to draw
TurtleGraphics
public TurtleGraphics(double angle,
double distance,
char turnLeft,
char turnRight,
char forwards,
char forwardsNodraw,
char backwards,
char backwardsNodraw,
char push,
char pop)
- Creates a new turtle according to specified options.
- Parameters:
angle - the angle to turn by (in radians)distance - the distance to drawturnLeft - the character to trigger turning leftturnRight - the character to trigger turning rightforwards - the character to trigger drawing forwardsforwardsNodraw - the character to trigger moving forwardsbackwards - the character to trigger drawing backwardsbackwardsNodraw - the character to trigger moving backwardspush - the character to store the current positionpop - the character to retrieve the last stored
position
doTurtle
public void doTurtle(java.lang.String code,
double x,
double y,
double dir,
double width,
double height,
java.lang.String fileName)
throws java.io.IOException
- Draws the turtle graphics and saves an svg file.
- Parameters:
code - the code to draw fromx - the initial x-coordinatey - the initial y-coordinatedir - the initial directionwidth - the width of the imageheight - the height of the imagefileName - the name of the file to save to
- Throws:
java.io.IOException