Most cases of repetitions only need a repetition counter or loop counter. The next example displays how the while-structure can be replaced by a simple for-structure, or for-loop.
// Tu3.java import ch.aplu.turtle.*; public class Tu3 { Turtle joe = new Turtle(); public Tu3() { for (int i = 0; i < 9; i++) { joe.forward(160); joe.right(160); } } public static void main(String[] args) { new Tu3(); } } |
![]() |
// Tu4.java import ch.aplu.turtle.*; public class Tu4 { Turtle joe = new Turtle(); public Tu4() { joe.speed(500); for (int k = 0; k < 10; k++) { for (int i = 0; i < 4; i++) { joe.forward(80); joe.right(90); } joe.left(36); } } public static void main(String[] args) { new Tu4(); } } |
// Tu4a.java import ch.aplu.turtle.*; import java.awt.Color; public class Tu4a { Turtle joe = new Turtle(); public Tu4a() { joe.setPos(-50, 50); 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(-60, 60); joe.hideTurtle(); } public static void main(String[] args) { new Tu4a(); } } |
Explaining the program code:
import java.awt.Color ; | This import is necessary if working with colors. |
joe.setColor (Color.red) ; | The Turtle itself is colored red. |
joe. setPenColor (Color.red) ; | The Turtle draws red lines. |
joe. setFillColor (Color.red) ; | The filling color is set to red. |
joe. fill ( - 60 , 60 ) ; | The area in which the coordinates lie is being filled. |
joe.hideTurtle(); | The Turtle is hidden (invisible). |
It is also possible to define any other color by choosing the amount (between 0 and 255) of red, green and blue. E.g.:
Color c = new Color(120, 200, 80);
joe.setColor(c);