Turtlegraphics for Smartphones and Tablets

Bern University of Teacher Education  
HomeStart Online EditorPrintAndroid GamesJava-Online

Threads


Today's operating systems are so efficient that multiple programs can run at the same time (multitasking). Since mostly only one processor is available, this happens by a fast switching among the processes. Also parts of an Android-program can be divided into multiple processes. This is called a thread.

Without the usage of threads two turtles cannot draw at the same time. In the example Tu13 two turtles are drawing alternatively a step of a staircase. ( App Tu13 without threads).

With the usage of threads two turtles are able to draw at the same time. Herefore we declare one of turtle's derived class PainterTurtle, which implements the method run(). This turtle is executed in every instance of PainterTurtle in an own thread.

Edit source with online-compiler

Install App on smartphone, tablet or emulator

QR-Code

download sources (Tu21.zip)

// Ex21.java

package app.tu21;

import turtle.*;

public class Tu21 extends Playground
{
  public void main()
  {
    Turtle joe = new PainterTurtle(-100-100, RED,  0);
    Turtle lisa = new PainterTurtle(-100, 100, GREEN, 90);
  }

  class PainterTurtle extends Turtle implements Runnable
  {
    PainterTurtle(double x, double y, int c,  double dir)
    {
      super( x, y, c);
      setHeading(dir);
      setPenColor(c);
      new Thread(this).start();
    }

    public void run()
    {
      for (int i = 0; i < 10; i++)
        step();
    }

    private void step()
    {
      fd(20);
      rt(90);
      fd(20);
      lt(90);
    }
  }
}
 

Explanatations to the program code:

PainterTurtle(double x, double y, int c, double dir)

The starting position, the colour and the starting direction is given to the constructor

super( x, y, c);

The constructor of Turtle is initialised with the starting position and the colour

new Thread(this).start();

A new thread is created and started

Turtle joe = new PainterTurtle(-100, -100, RED, 0); In main() a new PainterTurtle is created. This one starts to draw the staircase "autonomously"

 

Example 2: 20 turtles draw in the same time gleichzeitig, each in its own thread.

Edit source with online-compiler

Install App on smartphone, tablet or emulator

QR-Code

download sources (Tu22.zip)

// Tu22.java

package app.tu22;

import turtle.*;

public class Tu22 extends Playground
{
  public void main()
  {
    for (int = 0; i < 20; i++)
      new PainterTurtle();
  }

  class PainterTurtle extends Turtle implements Runnable
  {
    PainterTurtle()
    {
      new Thread(this).start();
      setHeading(360 * Math.random());
    }

    public void run()
    {
      fd(50 + 100 * Math.random());
      lt(45);
      for (int = 0; i < 10; i++)
      {
        fd(50);
        bk(50);
        rt(20 * Math.random());
      }
    }
  }
}
 

Explanatations to the program code:

new Thread(this).start()

New thread is created and started

setHeading(360 * Math.random());

The turtle's starting direction is set coincidentally

for (int i = 0; i < 15; i++)
      new PainterTurtle()
In main() 15 PainterTurtle are created. Each of them moves autonomously in its own thread.

 

The turtles draw each a tower at the same time. A similar exercise without threads canbe found under the menu item Multiple Turtles.

Edit source with online-compiler

Install App on smartphone, tablet or emulator

QR-Code

download sources (Tu23.zip)

// Ex23.java

package app.tu23;

import turtle.*;

public class Tu23 extends Playground
{
  public void main()
  {
    Turtle joe = new PainterTurtle(-100-100, RED);
    Turtle lisa = new PainterTurtle(-15-100, YELLOW);
    Turtle sara = new PainterTurtle(70, -100, GREEN);
  }

  class PainterTurtle extends Turtle implements Runnable
  {
    PainterTurtle(double x, double y, int c)
    {
      super(x, y, c);
      setPenColor(c);
      new Thread(this).start();
    }

    public void run()
    {
      for (int = 0; i < 6; i++)
        square();
    }

    private void square()
    {
      for (int = 0; k < 4; k++)
      {
        fd(30);
        rt(90);
      }
      fd(30);
    }
  }
}
 

Explanation to the program code:

PainterTurtle(double x, double y, int c)

The starting position and the colour is given to the constructor

super( x, y, c);

The constructor of Turtle is initialised with the starting position and the colour

new Thread(this).start();

A new thread is created and started. For each instance of he class PainterTurtle a thread is created

Turtle joe = new PainterTurtle(-100, -100, RED); In main() three PainterTurtles are created. These begin to draw "autonomously"