|
1 | 1 | package org.teachingkidsprogramming.section07events; |
2 | 2 |
|
3 | | -import java.util.ArrayList; |
4 | | - |
5 | | -import org.teachingextensions.logo.MultiTurtlePanel; |
6 | | -import org.teachingextensions.logo.Turtle; |
7 | | -import org.teachingextensions.windows.MouseLeftClickListener; |
8 | | - |
9 | | -public class ManyAnimals implements MouseLeftClickListener |
| 3 | +public class ManyAnimals |
10 | 4 | { |
11 | 5 | public ManyAnimals() |
12 | 6 | { |
13 | 7 | showSomeTurtles(); |
14 | 8 | } |
15 | 9 | //create container for Turtles HINT: Use ArrayList |
16 | | - public ArrayList<Turtle> turtles = new ArrayList<Turtle>(); |
17 | 10 | //create a window for many turtles HINT: Use MultiTurtlePanel |
18 | | - public MultiTurtlePanel mt = new MultiTurtlePanel(); |
19 | 11 | private void showSomeTurtles() |
20 | 12 | { |
21 | 13 | //show the panel |
22 | | - mt.showPanel(); |
23 | | - //listen for left mouse button click |
24 | | - //mt.addMouseLeftClickListener(this); |
25 | 14 | //set the size to 100 |
26 | | - int size = 100; |
27 | | - //add three turtles HINT: Use a For loop |
28 | | - for (int i = 1; i <= 3; i++) |
29 | | - { |
30 | | - //create a turtle |
31 | | - Turtle turtle = new Turtle(); |
32 | | - //add the turtles to the container for turtles |
33 | | - turtles.add(turtle); |
34 | | - } |
35 | | - //add all turtles to the window HINT: Use a FOR loop |
36 | | - for (Turtle turtle : turtles) |
37 | | - { |
38 | | - //NOTE: must call addTurtle BEFORE calling other methods |
39 | | - //add all turtles to the window |
40 | | - mt.addTurtle(turtle); |
41 | | - } |
| 15 | + //add three turtles HINT: FOR loop which 'does an action' |
| 16 | + //create a turtle |
| 17 | + //add the turtles to the container for turtles |
| 18 | + //add all turtles to the window HINT: Use a foreach loop |
| 19 | + //NOTE: must call addTurtle BEFORE calling other methods |
| 20 | + //add all turtles to the window |
42 | 21 | //teleport all turtles on the window HINT: Use a FOR loop and ZERO |
43 | | - for (int i = 0; i < 3; i++) |
44 | | - { |
45 | | - //set the X position to i*100 + 350 |
46 | | - turtles.get(i).setX(i * 100 + 350); |
47 | | - //set the Y position to i*100 + 100 |
48 | | - turtles.get(i).setY(i * 100 + 100); |
49 | | - } |
50 | | - //set some values for all turtles HINT: Use a FOR loop |
51 | | - for (Turtle turtle : turtles) |
52 | | - { |
53 | | - //set the speed of all turtle's to 7 |
54 | | - turtle.setSpeed(7); |
55 | | - //have every turtle draw a star of the current size |
56 | | - turtle.drawStar(size); |
57 | | - } |
58 | | - } |
59 | | - @Override |
60 | | - //threading error - Virtual Proctor contention |
61 | | - public void onLeftMouseClick(int x, int y) |
62 | | - { |
63 | | - int size = 200; |
64 | | - //create a new Turtle |
65 | | - Turtle turtle = new Turtle(); |
66 | | - //add another turtle to the turtles |
67 | | - turtles.add(turtle); |
68 | | - // the turtle to the window |
69 | | - mt.addTurtle(turtle); |
70 | | - turtle.show(); |
71 | | - // teleport the new turtle to the x and y of the mouse click |
72 | | - turtle.setX(x); |
73 | | - turtle.setY(y); |
74 | | - // have this turtle draw a star |
75 | | - turtle.drawStar(size); |
| 22 | + //set the X position to i*100 + 350 |
| 23 | + //set the Y position to i*100 + 100 |
| 24 | + //set some values for all turtles HINT: Use a foreach loop |
| 25 | + //set the speed of all turtle's to 7 |
| 26 | + //have every turtle draw a star of the current size |
76 | 27 | } |
77 | 28 | public static void main(String[] args) |
78 | 29 | { |
|
0 commit comments