Turtle Graphics with Java

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

Methods

Complex programs can be split into shorter and more simple program parts with the help of methods. In addition to this, methods enable the programmer to reuse certain program sections without retyping the whole code. This program technique can be practices with the help of simple examples.

The first example uses the method square to enable the Turtle to draw a square.

Run this example (Tu9.java)  
// Tu9.java

import ch.aplu.turtle.*;

class Tu9
{
  Turtle joe = new Turtle();

  Tu9()
  {
    joe.hideTurtle();
    for (int k = 0; k < 10; k++)
    {
      square();
      joe.left(36);
    }
  }

  void square()
  {
    for (int i = 0; i < 4; i++)
    {
      joe.forward(100);
      joe.right(90);
    }
  }

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

Test this example in the Online-Editor

Explaining the program code:

void square() A method is defined by the key word void, the method's name and the parameter brackets. If the method does not need any parameter the brackets are left empty.
square() The method square() is called and executed.

 

Methods with parameters

A method can have one or more parameter. In the following example Tu10.java the method square() receives the parameter Color c. This causes the Turtle to draw the 36 squares with different, randomly chosen colors.

Run this example (Tu10.java)  
// Tu10.java
import ch.aplu.turtle.*;
import java.awt.Color;

class Tu10
{
  Turtle joe = new Turtle();

  Tu10()
  {
    joe.hideTurtle();
    for(int i = 0; i < 36; i++)
    {
      int r = (int)(Math.random()*255);
      int g = (int)(Math.random()*255);
      int b = (int)(Math.random()*255);
      Color c = new Color(r, g, b);
      square(c);
      joe.right(10);
    }
  }

  void square(Color c) // parameter
  {
    joe.setPenColor(c);
    for (int i = 0; i < 4; i++)
    {
      joe.forward(100);
      joe.right(90);
    }
  }

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

Test this example in the Online-Editor

In the example Tu10a.java the method square has the parameter side:

Run this example (Tu10a.java)  
// Tu10a.java
import ch.aplu.turtle.*;

class Tu10a
{
  Turtle joe = new Turtle();

  Tu10a()
  {
    joe.hideTurtle();
    double side 180;
    
    while (side > 5)
    {
      square(side);
      joe.right(10);
      side = side 0.90;
    }
  }

  void square(double s) //method with parameter
  {
    for (int i = 0; i < 4; i++)
    {
      joe.forward(s);
      joe.right(90);
    }
  }

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

Test this example in the Online-Editor

Explaining the program code:

int  r  = (int)(Math.random() * 255) returns a random number between 0 and 255
Color  c  = new Color(r, g, b) Color c is defined with three randomly chosen red-, green and blue-components
square(c) This calls and executes the method square with the parameter c
void square(Color  c) The method square with the parameter Color c
double  side  = 180
void square (double  s)
The parameter in the methods brackets does not need the have the same name as the variable
side  =  side  * 0 . 90 The length of the side is decreased by 90%


Further examples with two alternatives:

Even though the use of methods without any parameter seems easier, it can be of great importance when dealing with more complex problems, such as recursions.

Using methods without parameters:

Run this example (Tu11.java)  
//Tu11.java
import ch.aplu.turtle.*;

class Tu11
{
  Turtle joe = new Turtle(); //instance variable

  Tu11()
  {
    joe.hideTurtle();
    for (int x = -140; x < 200; x = x + 75)
    {
      joe.setPos(x, 0);
      flower();
    }
  }

  void triangle( )
  {
    for (int i = 0; i < 4; i++)
    {
      joe.forward(20);
      joe.right(120);
    }
  }

  void flower()
  {
    for (int j = 0; j < 6; j++)
    {
      triangle();
      joe.right(180);
    }
  }

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

Test this example in the Online-Editor

Using methods with parameters:

Run this examples (Tu11a.java)  
//Tu11a.java
import ch.aplu.turtle.*;

class Tu11a
{
  Tu11a()
  {
    Turtle john = new Turtle();
    john.hideTurtle();

    for (int x = -140; x < 200; x = x + 75)
    {
      john.setPos(x, 0);
      flower(john, 20);
    }
  }

  void triangle(Turtle t, double s)
  {
    for (int i = 0; i < 4; i++)
    {
      t.forward(s);
      t.right(120);
    }
  }

  void flower(Turtle t, double s)
  {
    for (int j = 0; j < 6; j++)
    {
      triangle(t, s);
      t.right(180);
    }
  }

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

Test this example in the Online-Editor

Explaining the program code:
joe.setPos(x, 0) ; Sets the Turtle to the specified coordinates
for (int  x  = - 140 ; x  < 200 ; x  =  x  + 75) The first flower is drawn at the coordinates (-140, 0), the second at (-65, 0), etc.

Exercises