Turtlegraphics with Java
HomeAufgabenDruckenJava-Online

Use Colors


The colors can be changed using the following methods:
  • Turtle Color: setColor(Color.red)
  • Pen Color: setPenColor(Color.red)
  • Fill Color: setFillColor(Color.red)

To be able to use colors in these methods, the Java class Color must be imported.
import  java.awt.Color;

Predefined color values: Color.black, Color.blue, Color.cyan, Color.dkgray, Color.green, Color.gray, Color.ltgray, Color.magenta, Color.pink, Color.red, Color.white und Color.yellow.

However, you can also define any colour tone by specifying the red, green and blue components as an integer between 0 and 255. E.g:
Color c = new Color(120, 200, 80);
joe.setColor(c);

 


In the first example, the Turtle first draws a figure. Then the closed area outside this figure is coloured red. Two methods can be used for this filling (so-called flood fill):
fill() fills the closed area in which the Turtle is located. Make sure that the turtle is not on the edge line.
fill(x, y) fills the closed area in which the point with the coordinates (x, y) is located.

// Tu4a.java

import ch.aplu.turtle.*;
import java.awt.Color;

public class  Tu4a
{
  Turtle joe = new Turtle();

  public Tu4a()
  {
    joe.setPos(-5050);
    joe.setColor(Color.red);
    joe.setPenColor(Color.red)
    for (int k = 0; k < 4; k++)
    {
      for (int i = 0; i < 3; i++)
      {
        joe.forward(100);
        joe.right(90);
      }
      joe.left(180);
    }
    joe.setFillColor(Color.red);
    joe.fill(-6060);
    joe.hideTurtle();
  }

  public static void main(String[] args)
  {
    new Tu4a();
  }
}
 

Explanations of the programme code:

import  java.awt.Color This import is always necessary when working with colours.
setColor (Color.red) Sets the turtle color to red
setPenColor (Color.red) Sets the pen color to red
setFillColor (Color.red) Sets the fill color to red
fill ( - 60 ,  60 ) The area in which the point with the coordinates is located is filled
hideTurtle() The Turtle is invisible

 


In the second example, the areas are coloured continuously while drawing. The following methods are available for this

  • fillToPoint(x, y)
    A rubber band is attached to point (x, y), so to speak, which pulls the turtle along. All points over which the rubber band moves are coloured continuously
  • fillToHorizontal(y)
    Continuously fills the areas between the turtle track and the horizontal line (y = constant)
  • fillToVertical(x)
    Continuously fills the areas between the turtle track and the vertical line (x = constant)

The fill colour is the same as the current pen colour.

 

// Tu4b.java

import ch.aplu.turtle.*;
import java.awt.Color;

public class  Tu4b
{
  Turtle = new Turtle();
  
  public Tu4b()
  {
    t.speed(-1);
    t.setPenColor(Color.yellow);
    t.setPos(-100, 100);
    t.fillToPoint(-75, 75);
    for (int = 0; i < 6; i++)
      t.fd(60).rt(140).fd(60).lt(80);  
    
    t.setPenColor(Color.green);
    t.rt(30);
    t.setPos(-180-150);
    t.fillToHorizontal(-100);    
    for (int = 0; i < 3; i++)
      t.fd(120).rt(120).fd(120).lt(120);
    
    t.setPenColor(Color.red);
    t.fillToVertical(100);
    t.home();
    t.rt(45);
    t.fd(250);
  }
  
  public static void main(String[] args)
  {
    new Tu4b();
  }
}
 

Explanations of the programme code:

speed(-1) speed(-1) causes drawing without animation. Turtle is visible, but draws quickly

fillToPoint(-75, 75)
for (int i = 0; i < 6; i++)
      t.fd(60).rt(140).fd(60).lt(80);  

This method must be called before drawing a figure. The point (-75, 75) ends within the star drawn below
fillToHorizontal(-100) Trapezoidal areas between the drawn figure and the horizontal line are filled with y = -100
fillToVertical(100) Trapezoidal areas between the drawn figure and a vertical straight line with x = 100 are filled. It does not matter whether the turtle is to the left or right of the straight line.
home() Turtle is set to the position (0, 0) with the direction upwards

 

In the third example, the wrap() method is used when drawing filled figures. This causes the turtle to reappear in the window on the opposite side after leaving the turtle window and to continue drawing. Filling continues at the same point.

 
// Tu4c.java

import ch.aplu.turtle.*;
import java.awt.Color;

public class Tu4c
{
  Turtle = new Turtle();

  public Tu4c()
  {
    t.wrap();
    t.speed(-1);
    t.setColor(Color.red);
    t.fillToPoint(100, 0);
    t.setPenColor(Color.red);
    t.setPos(-50, 0);
    for (int = 0; i < 120; i++) 
      t.fd(8).rt(3);
  }

  public static void main(String[] args)
  {
    new Tu4c();
  }
}
 

Explanations of the programme code:

wrap() The Turtle is set to wrap mode