Turtle Graphics with Java

Bern University of Teacher Education  
HomeStart Online-EditorPrintAndroid-TurtlegraphicsJava-Online

Turtle applets

Applets are Java programs which can be executed over the internet. Their execution are started from a HTML-file and run by a virtual machine of a browser. Applets are inherited from the class JApplet. The class TurtleArea generates a applet window with a Turtle inside.

The example TuApplet1.java generates a embedded window with a Turtle drawing a square.

Run this example (TuApplet1.java)

// TuApplet1.java

import javax.swing.*;
import ch.aplu.turtle.*;

public class TuApplet1 extends JApplet implements Runnable
{
  private Turtle t;

  public void init()
  {
    TurtleArea ta = new TurtleArea(this);
    t = new Turtle(ta);
  }

  public void start()
  {
    new Thread(this).start();
  }

  public void run()
  {
    t.showTurtle();
    for (int j = 0; j < 10; j++)
    {
      for (int i = 0; i < 4; i++)
      {
        t.forward(100);
        t.right(90);
      }
      t.left(36)
    }   
  }
}
 

This applet can be embedded in any HTML-page. A minimal HTML-page embedding an applet looks as follows:

<html>
<body>
<h2>Turtle Applet 1</h2>
<applet code="TuApplet1.class" archive="aplu.jar" width="400" height="400"></applet>
</body>
</html>
 
It is important that the archive file aplu.jar must be copied into the same directory on the web server as the applet class and the HTML-file (the file aplu.jar is only zipped to make it possible to download it).

Test this example in the Online-Editor

Remarks: The written and compiled file TuApplet1.class can be downloaded to the local computer by clicking on the link TuApplet1.class inside the message window. It can then be started with a self-written HTML-page.

Explaining the program code:

void start()
The callback method start() is executed automatically when accessing the HTML-page or when returning to it

showTurtle()

In contrast to the normal Turtle program the Turtle only becomes visible when calling the method showTurtle()

Thread

Threads are used to be able to execute multiple program parts simultaneously. In this example it is necessary to generate a new thread for drawing. If the thread inside the method start() is not generated the drawn graphics do not appear until the method start() has finished.
implements Runnable Since Java only knows simple inheritance, special class constructions can be implemented with the help of interfaces. The class TurtleApplet1 is implemented with the interface Runnable. More information about interfaces can be found at Literature/Links (A. Pluss: Java Exemplarisch).


The example TuApplet2.java draws a recursive graphics.

Run this example (TuApplet2.java)

// TuApplet2.java

import javax.swing.*;
import ch.aplu.turtle.*;

public class TuApplet2 extends JApplet implements Runnable
{
  private Turtle t;

  public void init()
  {
    TurtleArea ta = new TurtleArea(this);
    t = new Turtle(ta);
  }

  public void start()
  {
    new Thread(this).start();
  }

  public void run()
  {
    t.hideTurtle();
    flocke(100);
  }

  void flocke(int seite)
  {
     if (seite < 5)
       return;
     for (int i = 0; i < 6; i++)
     {
       t.forward(seite);
       flocke(seite/3);
       t.back(seite);
       t.right(60);
    }
  }
}
 

A minimal HTML-page to start the applet TuApplet2 looks as follow:

<html>
<body>
<h2>Turtle Applet 2</h2>
<applet code="TuApplet2.class"archive="aplu.jar" width="400" height="400"></applet>
</body>
</html>

 
Test this example in the Online-Editor


The example TuApplet3.java causes the Turtle to repeat drawing a figure until the browser window is closed. The break of the applet is achieved with the callback-method stop() which causes the run-method to stop with the help of boolean flags.

Run this example (TurtleApplet3.java)

// TuApplet3.java

import javax.swing.*;
import ch.aplu.turtle.*;

public class TuApplet3 extends JApplet implements Runnable
{
  private boolean isActive = true;
  private Turtle t;

  public void init()
  {
    TurtleArea ta = new TurtleArea(this);
    t = new Turtle(ta);
  }

  public void start()
  {
    new Thread(this).start();
  }

  public void stop()
  {
    isActive = false// Causes thread to stop
  }

  public void run()
  {
    t.showTurtle();
       
    while (isActive)
    {
      t.setPos(-100-100);
      spirale();
      t.sleep(500);
      t.clear();
    }
  }

  private void spirale()
  {
    double a = 200;
    while (> 5)
    {
      t.forward(a);
      t.right(90);
      a = a * 0.9;
    }
  }
}
 

A minimal HTML-page to start the applet TuApplet3 looks as follow:

<html>
<body>
<h2>Turtle Applet 3</h2>
<applet code="TuApplet3.class" archive="aplu.jar" width="400" height="400"></applet>
</body>
</html>
 

Test this example in the Online-Editor

Exercises