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