Skip to content

Commit 5a5c9c0

Browse files
committed
fixed window render bug w/@jamesrcounts
1 parent dc24fbe commit 5a5c9c0

40 files changed

Lines changed: 205 additions & 144 deletions

src/main/java/org/teachingextensions/approvals/lite/util/WindowUtils.java

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,53 @@
66
import javax.swing.JFrame;
77
import javax.swing.JOptionPane;
88

9-
public class WindowUtils {
10-
public static void centerWindow(java.awt.Window window) {
11-
Dimension d = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
12-
Dimension w = window.getSize();
13-
int dx = (int) w.getWidth();
14-
int dy = (int) w.getHeight();
15-
int x = (int) ((d.getWidth() - dx) / 2);
16-
int y = (int) ((d.getHeight() - dy) / 2);
17-
MySystem.variable(" size (" + x + "," + y + "," + dx + "," + dy + ")");
18-
window.setBounds(x, y, dx, dy + 1);
9+
public class WindowUtils
10+
{
11+
public static void centerWindow(java.awt.Window window)
12+
{
13+
Dimension d = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
14+
Dimension w = window.getSize();
15+
int dx = (int) w.getWidth();
16+
int dy = (int) w.getHeight();
17+
int x = (int) ((d.getWidth() - dx) / 2);
18+
int y = (int) ((d.getHeight() - dy) / 2);
19+
MySystem.variable(" size (" + x + "," + y + "," + dx + "," + dy + ")");
20+
window.setBounds(x, y, dx, dy + 1);
21+
}
22+
public static void testFrame(JFrame frame)
23+
{
24+
testFrame(frame, true);
25+
}
26+
public static void copyToClipBoard(String code, boolean displayMessage)
27+
{
28+
java.awt.datatransfer.StringSelection selection = new java.awt.datatransfer.StringSelection(code);
29+
java.awt.Frame frame = new java.awt.Frame();
30+
frame.getToolkit().getSystemClipboard().setContents(selection, selection);
31+
if (displayMessage)
32+
{
33+
JOptionPane.showMessageDialog(null, "Code copied to Clipboard", "Finished", JOptionPane.INFORMATION_MESSAGE);
1934
}
20-
21-
public static void testFrame(JFrame frame) {
22-
testFrame(frame, true);
35+
frame.dispose();
36+
}
37+
public static void testFrame(JFrame frame, boolean closeOnExit)
38+
{
39+
if (closeOnExit)
40+
{
41+
testFrame(frame, new FrameCloser());
2342
}
24-
25-
public static void copyToClipBoard(String code, boolean displayMessage) {
26-
java.awt.datatransfer.StringSelection selection = new java.awt.datatransfer.StringSelection(code);
27-
java.awt.Frame frame = new java.awt.Frame();
28-
frame.getToolkit().getSystemClipboard().setContents(selection, selection);
29-
if (displayMessage) {
30-
JOptionPane.showMessageDialog(null, "Code copied to Clipboard", "Finished", JOptionPane.INFORMATION_MESSAGE);
31-
}
32-
frame.dispose();
43+
else
44+
{
45+
testFrame(frame);
3346
}
34-
35-
public static void testFrame(JFrame frame, boolean closeOnExit) {
36-
if (closeOnExit) {
37-
testFrame(frame, new FrameCloser());
38-
} else {
39-
testFrame(frame);
40-
}
41-
}
42-
43-
public static void testFrame(JFrame frame, WindowAdapter... array) {
44-
frame.pack();
45-
for (WindowAdapter closer : array) {
46-
frame.addWindowListener(closer);
47-
}
48-
WindowUtils.centerWindow(frame);
49-
frame.setVisible(true);
47+
}
48+
public static void testFrame(JFrame frame, WindowAdapter... array)
49+
{
50+
frame.pack();
51+
for (WindowAdapter closer : array)
52+
{
53+
frame.addWindowListener(closer);
5054
}
55+
WindowUtils.centerWindow(frame);
56+
//frame.setVisible(true);
57+
}
5158
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ public static void addColor(Color color)
3333
{
3434
wheel.add(color);
3535
}
36+
protected static void assertNonEmpty()
37+
{
38+
if (wheel.isEmpty())
39+
{
40+
String message = "I call shenanigans!!!\nThis ColorWheel is empty\nYou can NOT get a color from the ColorWheel before you've added anything to it.";
41+
throw new RuntimeException(message);
42+
}
43+
}
3644
/**
3745
* This method returns the next color of the ColorWheel. <br>
3846
* <b>Example:</b> {@code Color color = ColorWheel.getNextColor();}
@@ -41,6 +49,7 @@ public static void addColor(Color color)
4149
*/
4250
public static Color getNextColor()
4351
{
52+
assertNonEmpty();
4453
return wheel.next();
4554
}
4655
/**
@@ -51,6 +60,7 @@ public static Color getNextColor()
5160
*/
5261
public static Color getRandomColorFromWheel()
5362
{
63+
assertNonEmpty();
5464
return wheel.getRandomFrom();
5565
}
5666
public static void removeAllColors()

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public void addTurtle(Turtle turtle)
3939
{
4040
if (turtle == null) { return; }
4141
this.turtles.add(turtle);
42+
turtle.setFrame(this.getFrame());
4243
turtle.setPanel(this);
4344
}
4445
@Override

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,4 +364,9 @@ private static void makeTortoiseBody()
364364
Tortoise.turn(-25);
365365
Tortoise.move(65);
366366
}
367+
public static void setVisible(boolean b)
368+
{
369+
turtle().setFrameVisible(b);
370+
turtle().setPanelVisible(b);
371+
}
367372
}

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public class Turtle
2727
private double x = 640 / 2;
2828
private double y = 480 / 2;
2929
private double angleInDegrees = 0;
30+
private JFrame frame;
3031
private TurtlePanel panel;
3132
private int speed = 1;
3233
private List<LineSegment> trail = new ArrayList<LineSegment>();
@@ -74,7 +75,7 @@ private Component getPanel()
7475
panel = new TurtlePanel();
7576
if (speed != TEST_SPEED)
7677
{
77-
JFrame frame = new JFrame(title);
78+
frame = new JFrame(title);
7879
frame.getContentPane().add(panel);
7980
ProgramWindow.createStandardFrame(frame);
8081
}
@@ -233,7 +234,10 @@ public void setPenWidth(int width)
233234
public void show()
234235
{
235236
hidden = false;
236-
refreshPanel();
237+
Component p = getPanel();
238+
this.setFrameVisible(true);
239+
this.setPanelVisible(true);
240+
refreshPanel(p);
237241
}
238242
public TurtlePanel getBackgroundWindow()
239243
{
@@ -378,4 +382,16 @@ public void drawLightning(int length)
378382
this.move(length);
379383
}
380384
}
385+
public void setFrameVisible(boolean b)
386+
{
387+
frame.setVisible(b);
388+
}
389+
public void setPanelVisible(boolean b)
390+
{
391+
panel.setVisible(b);
392+
}
393+
public void setFrame(JFrame frame2)
394+
{
395+
this.frame = frame2;
396+
}
381397
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ public T next()
4949
}
5050
return list.get(index++);
5151
}
52-
private void assertNonEmpty()
52+
protected void assertNonEmpty()
5353
{
5454
if (list.isEmpty())
5555
{
56-
String message = "I call shenanigans!!!\nThis ColorWheel is empty\nYou can NOT get a color from the Wheel before you've added anything to it.";
56+
String message = "I call shenanigans!!!\nThis Wheel is empty\nYou can NOT get an object from the Wheel before you've added anything to it.";
5757
throw new RuntimeException(message);
5858
}
5959
}
@@ -68,4 +68,8 @@ public void empty()
6868
list.clear();
6969
index = 0;
7070
}
71+
public boolean isEmpty()
72+
{
73+
return list.isEmpty();
74+
}
7175
}

src/main/java/org/teachingextensions/virtualproctor/VirtualProctorWeb.java

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,8 @@
88
import java.io.File;
99
import java.io.IOException;
1010
import java.io.InputStream;
11-
import java.io.OutputStream;
1211
import java.net.URISyntaxException;
1312
import java.net.URL;
14-
import java.net.URLConnection;
15-
import java.net.URLEncoder;
16-
import java.net.UnknownHostException;
1713

1814
import javax.imageio.ImageIO;
1915

@@ -28,7 +24,6 @@
2824
import org.teachingextensions.approvals.lite.util.MySystem;
2925
import org.teachingextensions.approvals.lite.util.ThreadLauncher;
3026
import org.teachingextensions.approvals.lite.util.ThreadUtils;
31-
import org.teachingextensions.approvals.lite.util.io.FileUtils;
3227
import org.teachingextensions.approvals.lite.util.lambda.Action0;
3328

3429
public class VirtualProctorWeb extends WindowAdapter
@@ -52,7 +47,6 @@ public void sendImageToDisk(BufferedImage image) throws IOException
5247
{
5348
String filename = "C:\\temp\\VirtualProctor.png";
5449
ImageIO.write(image, "png", new File(filename));
55-
//TestUtils.displayFile(filename);
5650
}
5751
@Override
5852
public void windowClosed(WindowEvent e)
@@ -64,7 +58,6 @@ public void windowClosed(WindowEvent e)
6458
}
6559
public void sendImageToWeb(BufferedImage image)
6660
{
67-
//sendToWebLegacy(image);
6861
sendToWeb(image);
6962
}
7063
private void sendToWeb(BufferedImage image)
@@ -102,29 +95,4 @@ private void postImageToUrl(BufferedImage image, URL url) throws URISyntaxExcept
10295
MySystem.event("oh no, the internet ate your screenshot!");
10396
}
10497
}
105-
private void sendToWebLegacy(BufferedImage image)
106-
{
107-
try
108-
{
109-
String urlFormat = "http://virtualproctor-tkp.appspot.com/org.teachingkidsprogramming.virtualproctor.UploadImageRack?fileName=%s.png";
110-
String name = URLEncoder.encode(VirtualProctor.internals.getFullName(), "ISO-8859-1");
111-
URL url = new URL(String.format(urlFormat, name));
112-
URLConnection connection = url.openConnection();
113-
connection.setDoOutput(true);
114-
connection.setDoInput(true);
115-
OutputStream outputStream = connection.getOutputStream();
116-
ImageIO.write(image, "png", outputStream);
117-
outputStream.close();
118-
String content = FileUtils.readStream((InputStream) connection.getContent());
119-
MySystem.event(content);
120-
}
121-
catch (UnknownHostException e)
122-
{
123-
MySystem.event("No internet connection");
124-
}
125-
catch (Exception e)
126-
{
127-
MySystem.warning(e);
128-
}
129-
}
13098
}

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,18 @@
2525
@SuppressWarnings({"serial"})
2626
public class ProgramWindow extends JPanel
2727
{
28+
private JFrame frame;
2829
public ArrayList<Paintable> additional = new ArrayList<>();
2930
public ProgramWindow(String title)
3031
{
3132
this();
32-
JFrame frame = new JFrame(title);
33-
frame.getContentPane().add(this);
34-
ProgramWindow.createStandardFrame(frame);
33+
frame = new JFrame(title);
34+
getFrame().getContentPane().add(this);
35+
ProgramWindow.createStandardFrame(getFrame());
3536
}
36-
public static void addButton(JPanel panel, JButton button)
37+
public void addButton(JButton button)
3738
{
38-
panel.add(button);
39+
this.add(button);
3940
}
4041
public ProgramWindow()
4142
{
@@ -97,4 +98,13 @@ public void removePaintable(Paintable item)
9798
this.additional.remove(item);
9899
repaint();
99100
}
101+
public JFrame getFrame()
102+
{
103+
return frame;
104+
}
105+
public void setWindowVisible(boolean b)
106+
{
107+
this.frame.setVisible(b);
108+
this.setVisible(b);
109+
}
100110
}

src/main/java/org/teachingkidsprogramming/recipes/completed/section00demos/QuickShape.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public class QuickShape
99
{
1010
public static void main(String[] args) throws Exception
1111
{
12+
Tortoise.show();
1213
Tortoise.setX(150);
1314
//Tip: Use the Tortoise to draw shapes!
1415
//Draw a red square that is 50 pixels per side with a line that's 2 pixels thick

src/main/java/org/teachingkidsprogramming/recipes/completed/section00demos/QuickTortoise.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ public class QuickTortoise
66
{
77
public static void main(String[] args) throws Exception
88
{
9+
Tortoise.show();
910
Tortoise.drawTortoise();
1011
}
1112
}

0 commit comments

Comments
 (0)