With the constructor Turtle() more than one Turtle-object can be generated.
Turtle joe = new Turtle(); generates the first Turtle in a window. If afterwards a second Turtle is generated with Turtle luka = new Turtle(); it will run in a second window and not in the same as the first one. It is possible to generate up to 20 Turtles in different windows.
// Tu15.java import ch.aplu.turtle.*; import java.awt.Color; public class Tu15 { public Tu15() { Turtle joe = new Turtle(); Turtle luka = new Turtle(joe); //erzeugt luka im joe's-Fenster luka.setColor(Color.red); luka.setPenColor(Color.green); joe.setPos(-100, -100); luka.setPos(50,-100); for (int i = 0; i < 5; i++) { square(joe); joe.forward(40); square(luka); luka.forward(40); } } void square(Turtle t) { for (int i = 0; i < 4; i++) { t.forward(40); t.right(90); } } public static void main(String[] args) { new Tu15(); } } |
With Turtle luka = new Turtle(joe); a second Turtle is generated in the window of the first one. So luka is now inside joe's window. In this example a constructor with a parameter is used: Turtle(Turtle other Turtle). This allows both Turtles to run in one window.
// Tu16.java import ch.aplu.turtle.*; import java.awt.Color; public class Tu16 { public Tu16() { Turtle joe = new Turtle(); Turtle lea = new Turtle(joe, Color.red); Turtle sara = new Turtle(joe, Color.green); joe.setPos(-135, -50); lea.setPos(-10, -50); sara.setPos(115, -50); sara.setPenColor(Color.green); lea.setPenColor(Color.red); for (int i = 0; i < 9; i++) { segment(joe); segment(lea); segment(sara); } } void segment(Turtle t) { t.forward(140); t.right(160); } public static void main(String[] args) { new Tu16(); } } |