Skip to content

Commit 48c6067

Browse files
committed
refactored ConnectTheDots
1 parent c6cb6d1 commit 48c6067

2 files changed

Lines changed: 71 additions & 90 deletions

File tree

Lines changed: 34 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package org.teachingkidsprogramming.recipes.completed.section08events;
22

3-
import java.awt.Color;
4-
53
import org.teachingextensions.logo.Tortoise;
64
import org.teachingextensions.logo.utils.ColorUtils.ColorWheel;
75
import org.teachingextensions.logo.utils.ColorUtils.PenColors;
@@ -14,79 +12,62 @@ public class ConnectTheDots implements MouseRightClickListener, MouseLeftClickLi
1412
{
1513
public static void main(String[] args)
1614
{
17-
//Create new a 'Connect the Dots' object.
1815
new ConnectTheDots();
1916
}
2017
public ConnectTheDots()
2118
{
22-
Tortoise.show();
19+
setUpTheWindow();
20+
prepareColorPalette();
21+
// Listen for left clicks on the window for the tortoise
22+
Tortoise.getBackgroundWindow().addMouseLeftClickListener(this);
2323
// Listen for right clicks on the window for the tortoise
2424
Tortoise.getBackgroundWindow().addMouseRightClickListener(this);
25-
// Listen for left clicks on the window for the tortoise
26-
Tortoise.getBackgroundWindow().addMouseLeftClickListener(this);
27-
//Make the Tortoise go as fast as possible.
28-
Tortoise.setSpeed(10);
29-
// clearTheScreen (recipe below)
30-
clearTheScreen();
31-
// prepareColorPalette (recipe below)
32-
prepareColorPalette();
3325
}
34-
private static void prepareColorPalette()
26+
@Override
27+
public void onLeftMouseClick(int x, int y)
3528
{
36-
// ------------- Recipe for prepareColorPalette
37-
//
38-
// Add red to the color wheel
39-
ColorWheel.addColor(PenColors.Reds.Red);
40-
// Add green to the color wheel
41-
ColorWheel.addColor(PenColors.Greens.Green);
42-
// Add blue to the color wheel
43-
ColorWheel.addColor(PenColors.Blues.Blue);
44-
// Add purple to the color wheel
45-
ColorWheel.addColor(PenColors.Purples.Purple);
46-
// Add pink to the color wheel
47-
ColorWheel.addColor(PenColors.Pinks.Pink);
48-
// Add teal to the color wheel
49-
ColorWheel.addColor(PenColors.Greens.Teal);
29+
// addDot at x and y (recipe below)
30+
addDot(x, y);
31+
// Uncomment to write the text "Right click to clear the window" on the screen at position 100, 100
32+
new Text("Right click to clear the window").setTopLeft(100, 100).addTo(Tortoise.getBackgroundWindow());
5033
}
5134
private void addDot(int x, int y)
5235
{
53-
// ------------- Recipe for addDot
54-
// addACircle (recipe below)
55-
addCircle(x, y);
56-
// Move the tortoise to the current position of the mouse # 8
36+
// createCircle at x and y (recipe below)
37+
createCircle(x, y);
38+
// Move the tortoise to the current position of the mouse (x,y)
5739
Tortoise.moveTo(x, y);
5840
}
59-
private void addCircle(int x, int y)
41+
private void createCircle(int x, int y)
6042
{
61-
// ------------- Recipe for addACircle
62-
// The width of the circle is 15
63-
int radius = 7;
64-
// Change the color for the next shape to the next color from the color wheel
65-
Color color = ColorWheel.getNextColor();
66-
// Create a circle
67-
Circle circle = new Circle(radius, color);
68-
// Change the circle to be 40% opaque
69-
circle.setTransparency(60);
70-
// Move the center of the circle to the current position of the mouse
43+
// Create a new circle with a radius size of 11 using the next color on the Color wheel
44+
Circle circle = new Circle(11, ColorWheel.getNextColor());
45+
// Change the circle to be 60% opaque
46+
circle.setTransparency(40);
47+
// Move the center of the circle to the current position of the mouse (x,y)
7148
circle.setCenter(x, y);
49+
// Add the circle to the window HINT: Use Tortoise to get the window
7250
circle.addTo(Tortoise.getBackgroundWindow());
7351
}
74-
private static void clearTheScreen()
52+
@Override
53+
public void onRightMouseClick(int x, int y)
7554
{
76-
// ------------- Recipe for clearTheScreen
77-
// Clear the Program Window
55+
// Clear everything from the window HINT: Use Tortoise
7856
Tortoise.clear();
79-
// Write "Right click to clearWindow" on the screen at position 100, 100
80-
new Text("Right click to clearWindow").setTopLeft(100, 100).addTo(Tortoise.getBackgroundWindow());
8157
}
82-
@Override
83-
public void onRightMouseClick(int x, int y)
58+
private static void prepareColorPalette()
8459
{
85-
clearTheScreen();
60+
ColorWheel.addColor(PenColors.Reds.Red);
61+
ColorWheel.addColor(PenColors.Greens.Green);
62+
ColorWheel.addColor(PenColors.Blues.Blue);
63+
ColorWheel.addColor(PenColors.Purples.Purple);
64+
ColorWheel.addColor(PenColors.Pinks.Pink);
65+
ColorWheel.addColor(PenColors.Greens.Teal);
8666
}
87-
@Override
88-
public void onLeftMouseClick(int x, int y)
67+
private void setUpTheWindow()
8968
{
90-
addDot(x, y);
69+
Tortoise.show();
70+
Tortoise.setSpeed(10);
71+
Tortoise.hide();
9172
}
9273
}
Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,59 @@
11
package org.teachingkidsprogramming.section08events;
22

33
import org.teachingextensions.logo.Tortoise;
4+
import org.teachingextensions.logo.utils.ColorUtils.ColorWheel;
5+
import org.teachingextensions.logo.utils.ColorUtils.PenColors;
46
import org.teachingextensions.logo.utils.EventUtils.MouseLeftClickListener;
57
import org.teachingextensions.logo.utils.EventUtils.MouseRightClickListener;
68

79
public class ConnectTheDots implements MouseRightClickListener, MouseLeftClickListener
810
{
911
public static void main(String[] args)
1012
{
11-
// Create a new 'Connect The Dots' object. --#1.1
13+
new ConnectTheDots();
1214
}
1315
public ConnectTheDots()
1416
{
15-
Tortoise.show();
17+
setUpTheWindow();
18+
prepareColorPalette();
19+
// Listen for left clicks on the window for the tortoise --#1
1620
// Listen for right clicks on the window for the tortoise --#20.2
17-
// Listen for left clicks on the window for the tortoise --#1.2
18-
// Make the Tortoise go as fast as possible. --#4
19-
// clearTheScreen (recipe below) --#19.1
20-
//
21-
// ------------- Recipe for clearTheScreen --#19.2
22-
// Clear the Tortoise --#20.1
23-
// Write "Right click to clearWindow" on the screen at position 100, 100 --#18
24-
// ------------- End of clearTheScreen Recipe --#19.3
25-
//
26-
// prepareColorPalette (recipe below) --#17.1
27-
// ------------- Recipe for prepareColorPalette --#17.2
28-
// Add red to the color wheel --#6
29-
// Add green to the color wheel --#12
30-
// Add blue to the color wheel --#13
31-
// Add purple to the color wheel --#14
32-
// Add pink to the color wheel --#15
33-
// Add teal to the color wheel --#16
34-
// ------------- End of prepareColorPalette Recipe --#17.3
3521
}
3622
@Override
37-
public void onRightMouseClick(int x, int y)
23+
public void onLeftMouseClick(int x, int y)
3824
{
39-
// clearTheScreen (recipe above) --#20.3
25+
// addDot at x and y (recipe below) --#5
26+
// ------------- Recipe for addDot --#6
27+
// createCircle at x and y (recipe below) --#2
28+
// ------------- Recipe for createCircle --#3.0 (everything in this recipe)
29+
// Create a new circle with a radius of 11 using the next color on the color wheel
30+
// Change the circle to be 60% opaque
31+
// Move the circle so that it's center is at the current position of the mouse (x,y)
32+
// Place the circle on the tortoise's window
33+
// ------------- End of createCircle Recipe --#3.1
34+
// Move the tortoise to the current position of the mouse (x,y) --#4
35+
// ------------- End of addDot Recipe
36+
// Uncomment to write the text "Right click to clear the window" on the screen at position 100, 100 --#8
37+
// new Text("Right click to clear the window").setTopLeft(100, 100).addTo(Tortoise.getBackgroundWindow());
4038
}
4139
@Override
42-
public void onLeftMouseClick(int x, int y)
40+
public void onRightMouseClick(int x, int y)
4341
{
44-
// addDot (recipe below) --#11.1
45-
//
46-
// ------------- Recipe for addDot --#11.2
47-
// addACircle (recipe below) --#10.1
48-
//
49-
// ------------- Recipe for addACircle --#10.2
50-
// Create a circle with a radius of 7 which is the same color as the next color on the color wheel --#5
51-
// Change the circle to be 40% opaque --#9
52-
// Move the circle so that it's center is at the current position of the mouse --#8
53-
// Place the circle on the tortoise's window. --#7
54-
// ------------- End of addACircle Recipe --#10.3
55-
//
56-
// Move the tortoise to the current position of the mouse --#2
57-
// ------------- End of addDot Recipe --#11.3
42+
// Clear everything from the window HINT: Use Tortoise --#7
43+
}
44+
private static void prepareColorPalette()
45+
{
46+
ColorWheel.addColor(PenColors.Reds.Red);
47+
ColorWheel.addColor(PenColors.Greens.Green);
48+
ColorWheel.addColor(PenColors.Blues.Blue);
49+
ColorWheel.addColor(PenColors.Purples.Purple);
50+
ColorWheel.addColor(PenColors.Pinks.Pink);
51+
ColorWheel.addColor(PenColors.Greens.Teal);
52+
}
53+
private void setUpTheWindow()
54+
{
55+
Tortoise.show();
56+
Tortoise.setSpeed(10);
57+
Tortoise.hide();
5858
}
5959
}

0 commit comments

Comments
 (0)