Turtlegraphics for Smartphones and Tablets

Bern University of Teacher Education  
HomeStart Online EditorPrintAndroid GamesJava-Online

Use colours

It is easy to use colours with the Android-Turtlegraphic. Colours may be changed into red, for example, as follows:

  • Turtle-colour: new Turtle(RED) or setTurtleColor(RED)
  • Background-colour: clean(RED) or clean(RED) if all turtles shall disappear
  • pen colour: setPenColor(RED)

The colours BLACK, BLUE, CYAN, DKGRAY, GREEN, GRAY, LTGRAY, MAGENTA, RED, WHITE, YELLOW and TRANSPARENT can be used immediately. To use different colours, Android-class has to be imported (import android.graphics.Color). A random colour is defined by its RGB-value (e.g.. setPenColor(Color.rgb(20, 80, 0)). When filling areas, the current pen colour is used.

In the following example we use the colours for drawing the swiss flag.

Edit source with online-compiler

Install App on smartphone or tablet

QR-Code

download sources (Tu5.zip)

// Tu5.java

package app.tu5;

import turtle.*;

public class Tu5 extends Playground
{
  void main()
  {
    st();
    setSpeed(10);
    clean(RED);
    setPos(-50, 50);
    for (int = 0; k < 4; k++)
    {
      for (int = 0; i < 3; i++)
      {
        fd(90);
        rt(90);
      }
      lt(180);
    }
    fill(0, 0);
  }
}
 

Explanations to the program code:

setSpeed(10) Sets the turtle's speed to 10. The speed can be between 0 and 10. The standard value is 5. setSpeed(MAXSPEED) is the fastest speed.
clean(RED) Der background turns red
fill() The closed area, where the turtle is, is filled with the pen colour. In our case with the standard-colour white.


Filled-figures

In the first example we have already used the method fill(). Thereby the area is fille dwith the so called Flood-Fill Algorithm. Is is presumed that the turtle is in a closed area that has the background colour. The area will be filled with the current pen colour. That means that the figur is additionally filled. In many cases it makes sense that the turtle is filling the area while moving. Herefore three different filling-methods can be used:

  • fillToPoint(x, y)
    The point is set before the turtle starts drawing. Then with each turtle movement the area between the turtle trace and point is filled. In our example, the yellow star is filled with this method.
  • fillToHorizontal(y)
    The horizontal line is set through the y-coordinate. Then the area between the turtle trace and the horizontal line is continuously filled. In our example the green area is filled by this way.
  • fillToVertical(x)
    The verical line is set through the x-coordinate. Then the area between the turtle trace and the vertical line is continuously filled. In our case the red area is filled by this way.

Edit source with online-compiler

QR-Code

Install App on smartphone or tablet

download sources (Tu5a.zip)

// Tu5a.java

package app.tu5a;

import turtle.*;

public class Tu5a extends Playground
{
  void main()
  {
    st();
    setSpeed(8);
    setPenColor(YELLOW);
    fillToPoint(-75, 75);
    setPos(-100, 100);
    for (int = 0; i < 6; i++)
      fd(50).rt(140).fd(50).lt(80);  
    
    setPenColor(GREEN);
    fillToHorizontal(-150);
    setPos(-100-100);
    for (int = 0; i < 5; i++)
      rt(30).fd(50);
    
    setPenColor(RED);
    fillToVertical(100);
    setPos(150, 150);
    setHeading(225);
    fd(180);
  }
}
 

Explanations to the program code:

setPenColor(YELLOW) Sets the pencolour and therefore also the filling-colour to yellow
fillToPoint(-75, 75) The figure is filled from the point of the set coordinates. In our case the point is at the border of the star figure.
fillToHorizontal(-150) The area between the turtle trace and the horizontal line with y = -150, is filled
fillToVertical(100) The area between the turtle trace and the vertical line with x = 100, is filled