Skip to content

Commit 7ba6342

Browse files
committed
created new recipe TortoiseMobile stub w/ @mballin
added buttons and stub for new recipe - must fix threading/rendering issue on ProgramWindow
1 parent cc6d874 commit 7ba6342

8 files changed

Lines changed: 124 additions & 9 deletions

File tree

src/main/java/org/teachingextensions/logo/ImageBackground.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,26 @@
1010

1111
import org.teachingextensions.approvals.lite.util.ObjectUtils;
1212

13-
public class ImageBackground implements Paintable {
13+
public class ImageBackground implements Paintable
14+
{
1415
private BufferedImage image;
15-
16-
public ImageBackground(String uri) {
17-
try {
16+
public ImageBackground(String uri)
17+
{
18+
try
19+
{
1820
URL url = new URL(uri);
1921
image = ImageIO.read(url);
20-
} catch (Throwable e) {
22+
}
23+
catch (Throwable e)
24+
{
2125
throw ObjectUtils.throwAsError(e);
2226
}
2327
}
24-
2528
@Override
26-
public void paint(Graphics2D g, JPanel caller) {
29+
public void paint(Graphics2D g, JPanel caller)
30+
{
2731
Image img = image;
28-
g.drawImage(img, 0, 0, caller.getWidth(), caller.getHeight(), 0, 0,
29-
img.getWidth(null), img.getHeight(null), null);
32+
g.drawImage(img, 0, 0, caller.getWidth(), caller.getHeight(), 0, 0, img.getWidth(null), img.getHeight(null),
33+
null);
3034
}
3135
}

src/main/java/org/teachingextensions/windows/ProgramWindow.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.awt.RenderingHints;
88
import java.util.ArrayList;
99

10+
import javax.swing.JButton;
1011
import javax.swing.JFrame;
1112
import javax.swing.JPanel;
1213

@@ -32,6 +33,10 @@ public ProgramWindow(String title)
3233
frame.getContentPane().add(this);
3334
ProgramWindow.createStandardFrame(frame);
3435
}
36+
public static void addButton(JPanel panel, JButton button)
37+
{
38+
panel.add(button);
39+
}
3540
public ProgramWindow()
3641
{
3742
setPreferredSize(new Dimension(627, 442));
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package org.teachingkidsprogramming.recipes.completed.section07events;
2+
3+
import java.awt.Color;
4+
import java.awt.event.ActionEvent;
5+
import java.awt.event.ActionListener;
6+
7+
import javax.swing.ImageIcon;
8+
import javax.swing.JButton;
9+
10+
import org.teachingextensions.logo.ColorWheel;
11+
import org.teachingextensions.logo.PenColors;
12+
import org.teachingextensions.logo.Tortoise;
13+
import org.teachingextensions.logo.shapes.Circle;
14+
import org.teachingextensions.windows.MouseRightClickListener;
15+
import org.teachingextensions.windows.ProgramWindow;
16+
17+
//***********************In Progress***************************//
18+
public class TortoiseMobile implements MouseRightClickListener
19+
{
20+
public static void main(String[] args)
21+
{
22+
new TortoiseMobile();
23+
}
24+
public TortoiseMobile()
25+
{
26+
Tortoise.getBackgroundWindow().addMouseRightClickListener(this);
27+
Tortoise.setSpeed(10);
28+
clearTheScreen();
29+
prepareColorPalette();
30+
ImageIcon arrowIconL = new ImageIcon(
31+
"../TeachingKidsProgramming.Source.Java/src/main/resources/icons/arrow-left.png");
32+
JButton leftButton = new JButton(arrowIconL);
33+
// leftButton.setLocation(300, 100);
34+
ProgramWindow.addButton(Tortoise.getBackgroundWindow(), leftButton);
35+
ImageIcon arrowIconU = new ImageIcon(
36+
"../TeachingKidsProgramming.Source.Java/src/main/resources/icons/arrow-up.png");
37+
JButton upButton = new JButton(arrowIconU);
38+
// rightButton.setLocation(300, 100);
39+
ProgramWindow.addButton(Tortoise.getBackgroundWindow(), upButton);
40+
ImageIcon arrowIconR = new ImageIcon(
41+
"../TeachingKidsProgramming.Source.Java/src/main/resources/icons/arrow-right.png");
42+
JButton rightButton = new JButton(arrowIconR);
43+
// rightButton.setLocation(300, 100);
44+
ProgramWindow.addButton(Tortoise.getBackgroundWindow(), rightButton);
45+
rightButton.addActionListener(new ActionListener()
46+
{
47+
@Override
48+
public void actionPerformed(ActionEvent e)
49+
{
50+
Tortoise.turn(90);
51+
}
52+
});
53+
leftButton.addActionListener(new ActionListener()
54+
{
55+
@Override
56+
public void actionPerformed(ActionEvent e)
57+
{
58+
Tortoise.turn(-90);
59+
}
60+
});
61+
upButton.addActionListener(new ActionListener()
62+
{
63+
@Override
64+
public void actionPerformed(ActionEvent e)
65+
{
66+
Tortoise.move(25);
67+
}
68+
});
69+
Tortoise.getBackgroundWindow().setVisible(false);
70+
Tortoise.getBackgroundWindow().setVisible(true);
71+
// String backGround = "/TeachingKidsProgramming.Source.Java/src/main/resources/maze.png";
72+
// Tortoise.getBackgroundWindow().setBackgroundImage(backGround);
73+
}
74+
private static void prepareColorPalette()
75+
{
76+
ColorWheel.addColor(PenColors.Reds.Red);
77+
ColorWheel.addColor(PenColors.Greens.Green);
78+
ColorWheel.addColor(PenColors.Blues.Blue);
79+
ColorWheel.addColor(PenColors.Purples.Purple);
80+
ColorWheel.addColor(PenColors.Pinks.Pink);
81+
ColorWheel.addColor(PenColors.Greens.Teal);
82+
}
83+
private void addDot(int x, int y)
84+
{
85+
addCircle(x, y);
86+
Tortoise.moveTo(x, y);
87+
}
88+
private void addCircle(int x, int y)
89+
{
90+
int radius = 7;
91+
Color color = ColorWheel.getNextColor();
92+
Circle circle = new Circle(radius, color);
93+
circle.setTransparency(60);
94+
circle.setCenter(x, y);
95+
circle.addTo(Tortoise.getBackgroundWindow());
96+
}
97+
private static void clearTheScreen()
98+
{
99+
Tortoise.clear();
100+
}
101+
@Override
102+
public void onRightMouseClick(int x, int y)
103+
{
104+
clearTheScreen();
105+
}
106+
}
2.15 KB
Loading
2.09 KB
Loading
1.97 KB
Loading
2.04 KB
Loading

src/main/resources/maze.png

19.3 KB
Loading

0 commit comments

Comments
 (0)