|
20 | 20 | * allows you to change the color of the background and more... |
21 | 21 | */ |
22 | 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 | | - { |
| 23 | +public class ProgramWindow extends JPanel { |
| 24 | + protected final ArrayList<Paintable> painters = new ArrayList<Paintable>(); |
| 25 | + private JFrame frame; |
| 26 | + |
| 27 | + public ProgramWindow(String title) { |
29 | 28 | this(); |
30 | | - frame = new JFrame(title); |
31 | | - getFrame().getContentPane().add(this); |
| 29 | + this.frame = new JFrame(title); |
| 30 | + this.frame.getContentPane().add(this); |
32 | 31 | ProgramWindow.createStandardFrame(getFrame()); |
33 | 32 | } |
| 33 | + |
| 34 | + public ProgramWindow() { |
| 35 | + setPreferredSize(new Dimension(627, 442)); |
| 36 | + setColor(PenColors.Whites.White); |
| 37 | + } |
| 38 | + |
| 39 | + public static void createStandardFrame(JFrame frame) { |
| 40 | + WindowUtils.testFrame(frame, new VirtualProctorWeb(), new FrameCloser()); |
| 41 | + } |
| 42 | + |
| 43 | + public static Graphics2D configureGraphics2D(Graphics g) { |
| 44 | + Graphics2D g2d = (Graphics2D) g.create(); |
| 45 | + g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); |
| 46 | + g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY); |
| 47 | + g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE); |
| 48 | + g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE); |
| 49 | + return g2d; |
| 50 | + } |
| 51 | + |
34 | 52 | /** |
35 | 53 | * Adds a button instance to a window |
36 | 54 | * <p><b>Example:</b> {@code programWindow.addButton(myButton)}</p> |
37 | 55 | * |
38 | 56 | * @param button |
39 | 57 | * A button instance |
40 | 58 | */ |
41 | | - public void addButton(JButton button) |
42 | | - { |
| 59 | + public void addButton(JButton button) { |
43 | 60 | this.add(button); |
44 | 61 | } |
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 | | - } |
| 62 | + |
54 | 63 | @Override |
55 | | - public void paint(Graphics g) |
56 | | - { |
| 64 | + public void paint(Graphics g) { |
57 | 65 | super.paint(g); |
| 66 | + if (this.painters == null) { |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + ArrayList<Paintable> toPaint = new ArrayList<>(this.painters); |
| 71 | + |
58 | 72 | Graphics2D g2d = configureGraphics2D(g); |
59 | | - for (Paintable p : additional) |
60 | | - { |
| 73 | + for (Paintable p : toPaint) { |
61 | 74 | p.paint(g2d, this); |
62 | 75 | } |
63 | | - g2d.dispose(); |
64 | 76 | } |
65 | | - public void setColor(Color backgroundColor) |
66 | | - { |
| 77 | + |
| 78 | + public void setColor(Color backgroundColor) { |
67 | 79 | setBackground(backgroundColor); |
68 | 80 | } |
69 | | - public void addPaintable(Paintable additional) |
70 | | - { |
71 | | - this.additional.add(additional); |
72 | | - repaint(); |
73 | | - } |
74 | | - public void removePaintable() |
75 | | - { |
76 | | - additional.clear(); |
77 | | - repaint(); |
| 81 | + |
| 82 | + public ProgramWindow clearWindow() { |
| 83 | + this.painters.clear(); |
| 84 | + return this; |
78 | 85 | } |
| 86 | + |
79 | 87 | /** |
80 | 88 | * Adds a right mouse click listener instance to a window |
81 | 89 | * <p><b>Example:</b> {@code programWindow.addMouseRightClickListener(myRightClickListener)}</p> |
82 | 90 | * |
83 | 91 | * @param listener |
84 | 92 | * A listener instance |
85 | 93 | */ |
86 | | - public void addMouseRightClickListener(MouseRightClickListener listener) |
87 | | - { |
| 94 | + public void addMouseRightClickListener(MouseRightClickListener listener) { |
88 | 95 | addMouseListener(new RightClickMouseAdapter(listener)); |
89 | 96 | } |
| 97 | + |
90 | 98 | /** |
91 | 99 | * Adds a left mouse click listener instance to a window |
92 | 100 | * <p><b>Example:</b> {@code programWindow.addMouseLeftClickListener(myLeftClickListener)}</p> |
93 | 101 | * |
94 | 102 | * @param listener |
95 | 103 | * A listener instance |
96 | 104 | */ |
97 | | - public void addMouseLeftClickListener(MouseLeftClickListener listener) |
98 | | - { |
| 105 | + public void addMouseLeftClickListener(MouseLeftClickListener listener) { |
99 | 106 | addMouseListener(new LeftClickMouseAdapter(listener)); |
100 | 107 | } |
| 108 | + |
101 | 109 | /** |
102 | 110 | * Adds a background image to a window |
103 | 111 | * <p><b>Example:</b> {@code programWindow.setBackgroundImage("http://www.coolpicture.com/sunshine.jpg")}</p> |
104 | 112 | * |
105 | 113 | * @param url |
106 | 114 | * A URL path to an image |
107 | 115 | */ |
108 | | - public void setBackgroundImage(String url) |
109 | | - { |
110 | | - addPaintable(new ImageBackground(url)); |
| 116 | + public void setBackgroundImage(String url) { |
| 117 | + add(new ImageBackground(url)); |
111 | 118 | } |
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 | | - { |
| 119 | + |
| 120 | + public JFrame getFrame() { |
128 | 121 | return frame; |
129 | 122 | } |
130 | | - public void setWindowVisible(boolean b) |
131 | | - { |
| 123 | + |
| 124 | + public void setWindowVisible(boolean b) { |
132 | 125 | this.frame.setVisible(b); |
133 | 126 | this.setVisible(b); |
134 | 127 | } |
| 128 | + |
| 129 | + public ProgramWindow add(Paintable painter) { |
| 130 | + if (!this.painters.contains(painter)) { |
| 131 | + this.painters.add(painter); |
| 132 | + } |
| 133 | + |
| 134 | + return this; |
| 135 | + } |
| 136 | + |
| 137 | + public ProgramWindow remove(Paintable painter) { |
| 138 | + if (this.painters.contains(painter)) { |
| 139 | + this.painters.remove(painter); |
| 140 | + } |
| 141 | + |
| 142 | + return this; |
| 143 | + } |
135 | 144 | } |
0 commit comments