|
1 | 1 | package org.teachingextensions.WindowUtils; |
2 | 2 |
|
| 3 | +import java.awt.Color; |
| 4 | +import java.awt.Cursor; |
| 5 | +import java.awt.Dimension; |
| 6 | +import java.awt.event.MouseListener; |
| 7 | +import java.awt.image.BufferedImage; |
| 8 | + |
| 9 | +import javax.swing.JButton; |
| 10 | +import javax.swing.JFrame; |
| 11 | + |
3 | 12 | import org.teachingextensions.approvals.lite.util.FrameCloser; |
4 | 13 | import org.teachingextensions.approvals.lite.util.WindowUtils; |
5 | 14 | import org.teachingextensions.approvals.lite.writers.ComponentApprovalWriter; |
|
14 | 23 | import org.teachingextensions.logo.utils.LineAndShapeUtils.Paintable; |
15 | 24 | import org.teachingextensions.virtualproctor.VirtualProctorWeb; |
16 | 25 |
|
17 | | -import javax.swing.*; |
18 | | -import java.awt.*; |
19 | | -import java.awt.event.MouseListener; |
20 | | -import java.awt.image.BufferedImage; |
21 | | - |
22 | 26 | /** |
23 | 27 | * <img src="http://ftpmirror.your.org/pub/wikimedia/images/wikibooks/de/2/2c/JPanel_Add_JButton_PAGE_END.JPG" style="text-align: left" alt="A window image" height="50" width="75" > Program Window |
24 | 28 | * allows you to change the color of the background and more... |
25 | 29 | */ |
26 | | -public class ProgramWindow { |
| 30 | +public class ProgramWindow |
| 31 | +{ |
27 | 32 | private final LazyCanvas canvas = new LazyCanvas(); |
28 | | - private JFrame frame; |
29 | | - |
30 | | - public ProgramWindow(String title) { |
| 33 | + private JFrame frame; |
| 34 | + public ProgramWindow(String title) |
| 35 | + { |
31 | 36 | this(); |
32 | 37 | this.frame = new JFrame(title); |
33 | 38 | this.canvas.addTo(this.frame); |
34 | 39 | ProgramWindow.createStandardFrame(getFrame()); |
35 | 40 | } |
36 | | - |
37 | | - public ProgramWindow() { |
| 41 | + public ProgramWindow() |
| 42 | + { |
38 | 43 | this.canvas.setPreferredSize(new Dimension(627, 442)); |
39 | 44 | setColor(PenColors.Whites.White); |
40 | 45 | } |
41 | | - |
42 | | - public static void createStandardFrame(JFrame frame) { |
| 46 | + public static void createStandardFrame(JFrame frame) |
| 47 | + { |
43 | 48 | WindowUtils.testFrame(frame, new VirtualProctorWeb(), new FrameCloser()); |
44 | 49 | } |
45 | | - |
46 | | - |
47 | 50 | /** |
48 | 51 | * Adds a button instance to a window |
49 | 52 | * <p><b>Example:</b> {@code programWindow.addButton(myButton)}</p> |
50 | 53 | * |
51 | 54 | * @param button |
52 | 55 | * A button instance |
53 | 56 | */ |
54 | | - public void addButton(JButton button) { |
| 57 | + public void addButton(JButton button) |
| 58 | + { |
55 | 59 | this.canvas.add(button); |
56 | 60 | } |
57 | | - |
58 | | - |
59 | | - public void setColor(Color backgroundColor) { |
| 61 | + public void setColor(Color backgroundColor) |
| 62 | + { |
60 | 63 | this.canvas.setBackground(backgroundColor); |
61 | 64 | } |
62 | | - |
63 | | - public ProgramWindow clearWindow() { |
| 65 | + public ProgramWindow clearWindow() |
| 66 | + { |
64 | 67 | this.canvas.clear(); |
65 | 68 | return this; |
66 | 69 | } |
67 | | - |
68 | 70 | /** |
69 | 71 | * Adds a right mouse click listener instance to a window |
70 | 72 | * <p><b>Example:</b> {@code programWindow.addMouseRightClickListener(myRightClickListener)}</p> |
71 | 73 | * |
72 | 74 | * @param listener |
73 | 75 | * A listener instance |
74 | 76 | */ |
75 | | - public void addMouseRightClickListener(MouseRightClickListener listener) { |
| 77 | + public void addMouseRightClickListener(MouseRightClickListener listener) |
| 78 | + { |
76 | 79 | this.canvas.addMouseListener(new RightClickMouseAdapter(listener)); |
77 | 80 | } |
78 | | - |
79 | 81 | /** |
80 | 82 | * Adds a left mouse click listener instance to a window |
81 | 83 | * <p><b>Example:</b> {@code programWindow.addMouseLeftClickListener(myLeftClickListener)}</p> |
82 | 84 | * |
83 | 85 | * @param listener |
84 | 86 | * A listener instance |
85 | 87 | */ |
86 | | - public void addMouseLeftClickListener(MouseLeftClickListener listener) { |
| 88 | + public void addMouseLeftClickListener(MouseLeftClickListener listener) |
| 89 | + { |
87 | 90 | this.canvas.addMouseListener(new LeftClickMouseAdapter(listener)); |
88 | 91 | } |
89 | | - |
90 | 92 | /** |
91 | 93 | * Adds a background image to a window |
92 | 94 | * <p><b>Example:</b> {@code programWindow.setBackgroundImage("http://www.coolpicture.com/sunshine.jpg")}</p> |
93 | 95 | * |
94 | 96 | * @param url |
95 | 97 | * A URL path to an image |
96 | 98 | */ |
97 | | - public void setBackgroundImage(String url) { |
| 99 | + public void setBackgroundImage(String url) |
| 100 | + { |
98 | 101 | add(new ImageBackground(url)); |
99 | 102 | } |
100 | | - |
101 | | - public JFrame getFrame() { |
| 103 | + public JFrame getFrame() |
| 104 | + { |
102 | 105 | return frame; |
103 | 106 | } |
104 | | - |
105 | | - public void setWindowVisible(boolean b) { |
| 107 | + public void setWindowVisible(boolean b) |
| 108 | + { |
106 | 109 | this.frame.setVisible(b); |
107 | 110 | this.canvas.getValue().setVisible(b); |
108 | 111 | } |
109 | | - |
110 | 112 | @Deprecated |
111 | | - public ProgramWindow add(Paintable painter) { |
| 113 | + public ProgramWindow add(Paintable painter) |
| 114 | + { |
112 | 115 | this.canvas.getValue().add(painter); |
113 | 116 | return this; |
114 | 117 | } |
115 | | - |
116 | 118 | @Deprecated |
117 | | - public ProgramWindow remove(Paintable painter) { |
| 119 | + public ProgramWindow remove(Paintable painter) |
| 120 | + { |
118 | 121 | this.canvas.getValue().remove(painter); |
119 | 122 | return this; |
120 | 123 | } |
121 | | - |
122 | | - |
123 | | - public final BufferedImage getWindowImage() { |
| 124 | + public final BufferedImage getWindowImage() |
| 125 | + { |
124 | 126 | return ComponentApprovalWriter.drawComponent(this.canvas.getValue()); |
125 | 127 | } |
126 | | - |
127 | | - public ProgramWindow addTo(TurtleFrame frame) { |
| 128 | + public ProgramWindow addTo(TurtleFrame frame) |
| 129 | + { |
128 | 130 | frame.addContent(this.canvas.getValue()); |
129 | 131 | return this; |
130 | 132 | } |
131 | | - |
132 | | - public ProgramWindow repaint() { |
| 133 | + public ProgramWindow repaint() |
| 134 | + { |
133 | 135 | this.canvas.getValue().repaint(); |
134 | 136 | return this; |
135 | 137 | } |
136 | | - |
137 | | - public ProgramWindow setVisible(boolean visible) { |
| 138 | + public ProgramWindow setVisible(boolean visible) |
| 139 | + { |
138 | 140 | this.canvas.getValue().setVisible(visible); |
139 | 141 | return this; |
140 | 142 | } |
141 | | - |
142 | | - public ProgramWindow setCursor(int cursor) { |
| 143 | + public ProgramWindow setCursor(int cursor) |
| 144 | + { |
143 | 145 | this.setCursor(Cursor.getPredefinedCursor(cursor)); |
144 | 146 | return this; |
145 | 147 | } |
146 | | - |
147 | | - public ProgramWindow setCursor(Cursor predefinedCursor) { |
| 148 | + public ProgramWindow setCursor(Cursor predefinedCursor) |
| 149 | + { |
148 | 150 | this.canvas.getValue().setCursor(predefinedCursor); |
149 | 151 | return this; |
150 | 152 | } |
151 | | - |
152 | | - public ProgramWindow setBackground(Color color) { |
| 153 | + public ProgramWindow setBackground(Color color) |
| 154 | + { |
153 | 155 | this.canvas.setBackground(color); |
154 | 156 | return this; |
155 | 157 | } |
156 | | - |
157 | | - public CanvasPanel getCanvas() { |
| 158 | + public CanvasPanel getCanvas() |
| 159 | + { |
158 | 160 | return this.canvas.getValue(); |
159 | 161 | } |
160 | | - |
161 | | - private class LazyCanvas { |
| 162 | + private class LazyCanvas |
| 163 | + { |
162 | 164 | private CanvasPanel panel; |
163 | | - |
164 | | - public LazyCanvas addTo(JFrame frame) { |
| 165 | + public LazyCanvas addTo(JFrame frame) |
| 166 | + { |
165 | 167 | frame.getContentPane().add(this.getValue()); |
166 | 168 | return this; |
167 | 169 | } |
168 | | - |
169 | | - public CanvasPanel getValue() { |
170 | | - if (this.panel == null){ |
| 170 | + public CanvasPanel getValue() |
| 171 | + { |
| 172 | + if (this.panel == null) |
| 173 | + { |
171 | 174 | this.panel = new CanvasPanel(); |
172 | 175 | } |
173 | | - |
174 | 176 | return panel; |
175 | 177 | } |
176 | | - |
177 | | - public LazyCanvas setPreferredSize(Dimension dimension) { |
| 178 | + public LazyCanvas setPreferredSize(Dimension dimension) |
| 179 | + { |
178 | 180 | this.getValue().setPreferredSize(dimension); |
179 | 181 | return this; |
180 | 182 | } |
181 | | - |
182 | | - public LazyCanvas add(JButton button) { |
| 183 | + public LazyCanvas add(JButton button) |
| 184 | + { |
183 | 185 | this.getValue().add(button); |
184 | 186 | return this; |
185 | 187 | } |
186 | | - |
187 | | - public LazyCanvas setBackground(Color color) { |
| 188 | + public LazyCanvas setBackground(Color color) |
| 189 | + { |
188 | 190 | this.getValue().setBackground(color); |
189 | 191 | return this; |
190 | 192 | } |
191 | | - |
192 | | - public LazyCanvas clear() { |
| 193 | + public LazyCanvas clear() |
| 194 | + { |
193 | 195 | this.getValue().clear(); |
194 | 196 | return this; |
195 | 197 | } |
196 | | - |
197 | | - public LazyCanvas addMouseListener(MouseListener adapter) { |
| 198 | + public LazyCanvas addMouseListener(MouseListener adapter) |
| 199 | + { |
198 | 200 | this.getValue().addMouseListener(adapter); |
199 | 201 | return this; |
200 | 202 | } |
| 203 | + @SuppressWarnings("unused") |
| 204 | + public LazyCanvas setImageBackground(ImageBackground imageBackground) |
| 205 | + { |
| 206 | + this.getValue().setBackground(imageBackground); |
| 207 | + return this; |
| 208 | + } |
201 | 209 | } |
202 | 210 | } |
203 | | - |
|
0 commit comments