Aegidius
 Plüss   Aplulogo
 
in examples
Use the asterisk (*) character for wildcard searches
Searches on more than one word will be treated "as a phrase"
 
serverstatus
 www.aplu.ch
 www.java-online.eu
     Print Text 
© 2018, V10.0
 
  Jython
 
 

 

The Java package ch.aplu.turtle provides a professional and stable version of OOP-based turtle graphics since more than 6 years. The library is used in many introductory programming courses and achieved teachers satisfaction. Using Jython, turtles can be created and controlled interactively now, providing a pleasant subject for the "first hour of programming". It's like LOGO with the advantage that multiple turtles may be created, so turtles are really "objects" in the common sense of the word. Because the turtle movements (forward/backward steps and rotations) are animated and the turtle shapes are cute (not a simple triangle), introducing programming concepts like interations, selections, functions and recursion is a pleasure.

Depending on your didactical intensions you want to emphasize object orienting programming from the very beginning or start your course with global variables. (In Java you don't have the choice because global variables are not supported). If you opt for the second concept, TigerJython lets you create a global turtle by importing gturtle and calling makeTurtle(). All turtle methods are available in Python's global namespace. Consult the JavaDoc for the Turtle methods.

Programs now become extremely simple, just like in C, Basic or Logo. This example shows the most fundamental concept of informatics: "A computer program is a sequence of instructions that are executed step-by-step".

# TuEx1.py

from gturtle import *

makeTurtle()

fd(100)
rt(90)
fd(100)
rt(90)
fd(100)
rt(90)
fd(100)
rt(90)
Tut01

Select TuEx1.py (Ctrl+C to copy, Ctrl+V to paste)
Execute the program locally using WebStart.

Programming is based on repeating the same code automatically by using a "repeat" programming structure. In TigerJython you can just say "repeat a number of times the following actions". To draw the square we say: "repeat 4 times [go forward and turn right 90 degrees]", translated easily into code:

# TuEx1a.py

from gturtle import *

makeTurtle()

repeat 4:
   fd(100)
   rt(90)
Tut01

Select TuEx1a.py
(Ctrl+C to copy, Ctrl+V to paste)
Execute the program locally using WebStart.

The next example is the famous recursively defined binary tree, already used in programming courses in the sixties of the last century. Enjoy the clean and simple syntax of Python!

# TuEx2.py		

from gturtle import *

makeTurtle()

def tree(s):
   if s < 8:
     return
   fd(s)
   lt(45)
   tree(s/2)
   rt(90)
   tree(s/2)
   lt(45)
   back(s)

setY(-100)
tree(128)
Tut02

Select TuEx2.py (Ctrl+C to copy, Ctrl+V to paste)
Execute the program locally using WebStart.

On the other hand, if you want to start with OOP from the beginning, but without yet defining your own classes, you may create several named Turtle instances and invoke methods of the Turtle class.

# TuEx3.py

from gturtle import *

def drawLine(t):
   t.forward(100)
   t.right(160)

frame = TurtleFrame()
joe = Turtle(frame)
lea = Turtle(frame, "red")
sara = Turtle(frame, "green")

joe.setPos(-90, -50)
lea.setPos(0, -50)
sara.setPos(90, -50)
sara.setPenColor("green")
lea.setPenColor("red")
for i in range(9):
   drawLine(joe)
   drawLine(lea)
   drawLine(sara)
Tut03
Select TuEx3.py (Ctrl+C to copy, Ctrl+V to paste)
Execute the program locally using WebStart.

Since the ch.aplu.turtle package is (almost) thread-safe, you may easily make all three turtles work at the same time using a separate thread for each of them:

# TuEx4.py

from gturtle import *
from thread import start_new_thread

def go(t):
  start_new_thread(draw, (t, ))

def draw(t):
   for i in range(9):
      drawLine(t)

def drawLine(t):
   t.forward(100)
   t.right(160)

frame = TurtleFrame()
joe = Turtle(frame)
lea = Turtle(frame, "red")
sara = Turtle(frame, "green")

joe.setPos(-90, -50)
lea.setPos(0, -50)
sara.setPos(90, -50)
sara.setPenColor("green")
lea.setPenColor("red")
go(joe)
go(lea)
go(sara)
Tut01
Select TuEx4.py (Ctrl+C to copy, Ctrl+V to paste)
Execute the program locally using WebStart.

 

In the next step we construct a modern event driven turtle program where any number of turtle instances are created by a mouse click event. The callback onMouseHit() is registered simply by adding a named parameter mouseHit in the TurtleFrame constructor. This is one of the most elegant features of Python/Jython where functions are first-class objects that can be passed as parameters. The onMouseHit() callback runs in its own thread, so there is no need to create an extra thread.

# TuEx5.py

from gturtle import *

def onMouseHit(x, y):   
   t = Turtle(frame)
   t.setPos(x, y)
   for i in range(9):
      t.forward(100)
      t.right(160)

frame = TurtleFrame(mouseHit = onMouseHit)
Tut01
Select TuEx5.py (Ctrl+C to copy, Ctrl+V to paste)
Execute the program locally using WebStart.