Turtlegraphics for Smartphones and Tablets

Bern University of Teacher Education  
HomeStart Online EditorPrintAndroid GamesJava-Online

Simple drawing (Sequence)


The simples programs are made of a sequence of instruction that are executed in a fixed order. The name of the turtle window is Playground. A turtle, that is in the middle at the beginning and that can be displayed with showTurtle() respectively. st(), belongs to the Playground. The drawing occurs independently of the windows size in a coordinate system with the x-coordinates -200 to 200 from left to right and y-coordinates -200 to 200 bottom-up. The coordinates are of double type, meaning that also decimal numbers can be used. The drawing methods forward(), right(), left() can also entered shortly with the two letters fd(), rt(), lt(). Especially when working with a smartphone or tablet, this method is advantagous.

In the first example, two steps of a staircase are drawn.

Edit source with online-compiler

Install App on smartphone or tablet

QR-Code

download sources (Tu1.zip)


// Tu1.java

package app.tu1;

import turtle.*;

public class Tu1 extends Playground
{
  public void main()
  {
    st();
    fd(50);
    rt(90);
    fd(50);
    lt(90);
    fd(50);
    rt(90);
    fd(50);
    lt(90);    
  }
}
 


Explanations to the program code:
package app.tu1 An Android-Application has to receive a Package-name with at least one point. It serves as identification of our app for smartphone or table
import turtle.*
Imports the class library JTurtleLib
public 

Makes the class "visible", so that it can be executed with the Android-operation-system

class Tu1

Defines a program unit. Tu1 is the class name

extends Playground

Creates a playground (400x400 steps) and gives acces to all methods

void main()
{
}

By using the methodmain() Android-operation-system starts the program's cycle

showTurtle() oder kurz st() Shows the global, at the beginning hidden turtle, that appears in the middle of the playground
forward (100) oder kurz fd(100) The turtle moves 100 steps forward
right(90) oder kurz rt(90) The turtle turns in a 90° angle to the right
left(90) oder kurz lt(90) The turtle turns in a 90° angle to the left

 

Object-oriented approach


In an object-oriented program, the turtle is representated through an isntance of the class Turtle. By using Turtle john = new Turtle(), a turtle named John is created. In order to let a certain method act out by a turtle, this method with the point-separator is added to the name of the turtle. Multiple turtles can be created on one playground.
Since the programs with a global playground-turtle are syntactically easier, a global turtle is used in the introduction of the program structures.

Edit source with online-compiler

Install App on smartphone or tablet

QR-Code

downlaod sources (Tu1a.zip)


// Tu1a.java

package app.tu1a;

import turtle.*;

public class Tu1a extends Playground
{
  public void main()
  {
    Turtle john = new Turtle();
    Turtle luka = new Turtle();    
    john.fd(50);
    john.rt(90);
    john.fd(50);
    luka.lt(90).fd(50).lt(90).fd(50);  
  }
}
 


Explanations to the program code:
Turtle john = new Turtle(); Creates an object of the class Turtle
john.fd(50) The method fd(50) is executed by the turtle john
luka.lt(90).fd(50).lt(90).fd(50);

The methods can be written in succession with a full-stop separator but without vertical spacing

Syntax: