Turtle Graphics

Turtle Graphics

The turtle graphics found in TigerJython is based on the Java-libraries of Aegidius Plüss (aplu). It provides some sophisticated functions so that you can easily create rich and beautiful graphics.

These pages here give you an overview of the gturtle-library. However, this is not a complete list. A more complete documentation can be found within the TigerJython application under Help -> APLU Documentation.

Also check out the Examples.

Reference

Initialization and Basic Example

To use the turtle graphics you have to first load the gturtle-module and then create a new turtle with its window using makeTurtle(). Here is a simple example, drawing a red square:

from gturtle import *
makeTurtle()

setPenColor("red")
right(45)
repeat 4:
    forward(120)
    left(90)

Basic Movements

forward(distance)
Go distance pixels forward in the current direction of the turtle.
back(distance)
Go distance pixels backward. This is relative to the turtle's current position and orientation. The turtle will not retrace its previous steps backward.
left(angle), right(angle)
Turn the turtle angle degrees to the left or right, respecively.
leftArc(radius, angle), rightArc(radius, angle)
Let the turtle walk on the line of a circle to the left or right, respecively with the given radius. With an angle of 360° you get a full circle.

Controlling the Pen

dot(diameter)
Paint a dot with the given diameter using the current pen color. The turtle does not move.
penUp(), penDown()
With a raised pen the turtle will not draw its usual trace/line. Hence, using penUp() the turtle can be moved around without drawing anything.
penWidth(width)
Set the pen's width in pixels.

Filling Regions

fill(), fill(x, y)
The turtle must be inside a closed shape. fill() will then cause it to fill the entire enclosed region with the current fill color. Alternatively, you can specify the point where the floodfill should start. A common mistake when using fill() is to have forgotten using penUp() before moving the turtle into the inside of a shape.
fillToPoint(), fillOff()
With fillToPoint() the turtle will connect every point with its current position. This way the turtle does not draw outlines but rather paint an entire area using the current pen color. Use fillOff() to stop the turtle connecting the points.
startPath(), fillPath()
After startPath() the turtle will record its own movements and start constructing a path/shape. With fillPath() it then closes the recorded path/shape and fills it with the current fill color.

The Turtle's State

The turtle has various properties such as its position (x, y) within the window, its heading and the colors of itself and its pen.

getX(), getY()
Get the turtle's x- or y-coordinate, respecively.
getPixelColorStr()
Return the color of the pixel right below the turtle. The color is returned as a string.
heading(), heading(angle)
heading() always returns the turtle's current heading. You can use the same function to give the turtle a new heading in degrees, where 0° is to the top (north), 90° to the right (east), 180° to the bottom (south) and 270° to the left (west).
hideTurtle()
Hide the turtle altogether and thereby switch off all animation. The pcitures will hence be drawn very quickly.
setColor(color)
Set the color of the turtle itself. This will have no effect upon the colors used for the pen or filling.
setFillColor(color)
Set the color used for filling reagions.
setPenColor(color)
Set the color used for drawing the traces/lines.
setPos(x, y)
Set the turtle to the coordinates (x, y) without drawing any trace/line. The turtle's heading is not affected by setPos.
showTurtle()
Show the previously hidden turtle and switch animvation back on.
speed(s)
Set the speed with which the turtle walks. Use s=-1 to have maximum speed or hideTurtle() to completely switch off animation.
towards(x, y)
Turns the turtle so that it is headed towards the point (x, y).

Mouse control

The turtle can react to mouse input. In order to do so, you need to define a callback-function which is to be called whenever a mouse event occurs. You then register your function within makeTurtle.

The following example reacts to mouse clicks by painting a dot at the position of the click. Note while the name onClick is arbitrary, your function must take two parameters for the x- and y-coordinates, respectively.

from gturtle import *

# Paint a dot wherever you click:
def onClick(x, y):
    setPos(x, y)
    dot(10)

makeTurtle(mouseHit = onClick)

Other callbacks allow your program to react to mouse movement. Those callbacks take a single parameter which will be an instance of Java's MouseEvent-class. Use the functions toTurtleX and toTurtleY to transform the mouse coordinates to turtle coordinates as shown in the following example.

from gturtle import *

def onMove(e):
    x = toTurtleX(e.getX())
    y = toTurtleY(e.getY())
    setPos(x, y)

def onDrag(e):
    x = toTurtleX(e.getX())
    y = toTurtleY(e.getY())
    moveTo(x, y)

makeTurtle(mouseMoved = onMove, mouseDragged = onDrag)
hideTurtle()

When reacting to mouse input like this, don't forget to hide the turtle because the turtle's animation is to slow to follow the mouse.