aboutsummaryrefslogtreecommitdiff
path: root/PunchingBag/src/PunchingBagGUI.java
blob: 60cacaa2167c78769a727234563d8d5b6cee347a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;

public class PunchingBagGUI implements MouseListener, MouseMotionListener,
		ButtonListener, LEDListener {

	public static enum Mode {
		Menu, Interactive, Game
	};

	Mode currentMode = Mode.Interactive;

	static JFrame frame;

	// Contact
	public enum Direction {
		None, Up, Down, Left, Right
	};

	static PunchingBag bag = new PunchingBag();

	final boolean useDebugScreen = true;

	public static void main(String[] args) throws InterruptedException {

		new PunchingBagGUI();

	}

	PunchingBagGUI() throws InterruptedException {
		bag.buttonArduino.listPorts();

		frame = new JFrame("Punching Bag GUI");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// frame.setSize(600, 600);
		frame.setVisible(true);

		frame.addMouseListener(this);
		frame.addMouseMotionListener(this);

		bag.addButtonListener(this);
		bag.addLEDChangeListener(this);

		frame.add(new PunchingBagSim(), BorderLayout.WEST);

		frame.add(new LEDControlPanel(), BorderLayout.CENTER);

	}

	class LEDControlPanel extends JPanel implements ActionListener {
		String[] colours = {" ", "R", "Y", "G" };

		public LEDControlPanel() {
			this.setLayout(new GridBagLayout());
			
			GridBagConstraints c = new GridBagConstraints();
			c.fill = GridBagConstraints.BOTH;
			c.anchor = GridBagConstraints.CENTER;
			// c.weightx = 0.2;
			// c.weighty = 0.2;

			for (int y = 0; y < bag.ledHeight; y++) {
				for (int x = 0; x < bag.ledWidth; x++) {
					c.gridx = x;
					c.gridy = y;
					
					RYGComboBoxRenderer renderer = new RYGComboBoxRenderer();
					JComboBox rygCombo = new JComboBox(colours);
					rygCombo.setSelectedIndex(0);
					rygCombo.addActionListener(this);
					rygCombo.setActionCommand(String.valueOf(x) + "," + String.valueOf(y));
					//rygCombo.setRenderer(renderer);

					this.add(rygCombo, c);
				}
			}
		}

		public void actionPerformed(ActionEvent a) {
			PunchingBag.Colour colour = PunchingBag.Colour.None;
			if (((JComboBox)a.getSource()).getSelectedIndex() == 1) {
				colour = PunchingBag.Colour.Red;
			} else if (((JComboBox)a.getSource()).getSelectedIndex() == 2) {
				colour = PunchingBag.Colour.Yellow;
			} else if (((JComboBox)a.getSource()).getSelectedIndex() == 3) {
				colour = PunchingBag.Colour.Green;
			}
			System.out.println("Setting " + a.getActionCommand().split(",")[0] + " " + a.getActionCommand().split(",")[1]);
			bag.setLED(Integer.valueOf(a.getActionCommand().split(",")[0]),Integer.valueOf( a.getActionCommand().split(",")[1]), colour);
		}

		class RYGComboBoxRenderer extends JLabel implements ListCellRenderer {
			public RYGComboBoxRenderer() {
				super();
			}

			public Component getListCellRendererComponent(JList list,
					Object value, int index, boolean isSelected,
					boolean cellHasFocus) {

				// Get the selected index. (The index param isn't
				// always valid, so just use the value.)

				if (isSelected) {
					setBackground(list.getSelectionBackground());
					setForeground(list.getSelectionForeground());
				} else {
					setBackground(list.getBackground());
					setForeground(list.getForeground());
				}

				if (index != -1)
					setText(colours[index]);

				return this;
			}
		}
	}

	class PunchingBagSim extends JPanel implements ButtonListener, LEDListener,
			MouseListener {
		int buttonMinSize = 10;
		int buttonMaxSize = 50;

		int ledMinSize = 10;
		int ledMaxSize = 50;

		int buttonSize = 40;
		int ledSize = 8;

		public PunchingBagSim() {
			/*
			 * this.setMinimumSize(new Dimension((bag.buttonWidth *
			 * buttonMinSize) + (bag.ledWidth * ledMinSize), (bag.buttonHeight *
			 * buttonMinSize) + (bag.ledHeight * ledMaxSize)));
			 * this.setMaximumSize(new Dimension((bag.buttonWidth *
			 * buttonMaxSize) + (bag.ledWidth * ledMaxSize), (bag.buttonHeight *
			 * buttonMaxSize) + (bag.ledHeight * ledMaxSize)));
			 */
			bag.addButtonListener(this);
			bag.addLEDChangeListener(this);

			this.addMouseListener(this);

			GridBagLayout gbl = new GridBagLayout();
			this.setLayout(gbl);
			GridBagConstraints c = new GridBagConstraints();

			// c.insets = new Insets(1, 1, 1, 1);
			c.fill = GridBagConstraints.BOTH;
			c.anchor = GridBagConstraints.CENTER;
			// c.weightx = 0.2;
			// c.weighty = 0.2;
			for (int y = 0; y < bag.ledHeight; y++) {
				for (int x = 0; x < bag.ledWidth; x++) {
					c.gridx = x * 2;
					c.gridy = y * 2;

					this.add(new LED(x, y), c);
				}
			}

			// c.weightx = 0.8;
			// c.weighty = 0.8;
			for (int y = 0; y < bag.buttonHeight; y++) {
				for (int x = 0; x < bag.buttonWidth; x++) {
					c.gridx = (x * 2) + 1;
					c.gridy = (y * 2) + 1;

					this.add(new Button(x, y), c);
				}
			}

			// this.setMinimumSize(new Dimension(500,500));
			// this.setPreferredSize(new Dimension(500,500));

			// this.setBackground(Color.BLUE);

			frame.pack();
			frame.setSize(500, 500);

			this.setBorder(BorderFactory.createLineBorder(Color.black));
		}

		/*
		 * public Dimension getPreferredSize() { if () return new
		 * Dimension(super.,10); }
		 */

		class LED extends JComponent implements LEDListener {
			final int x;
			final int y;

			public LED(int x, int y) {
				this.x = x;
				this.y = y;
				/*
				 * this.setMinimumSize(new Dimension(50,50)); this.setSize(50,
				 * 50); this.setPreferredSize(new Dimension(50,50));
				 */
				bag.addLEDChangeListener(this);

			}

			public Dimension getPreferredSize() {
				return new Dimension(10, 10);
			}

			public void paintComponent(Graphics g) {
				if (bag.getLED(x, y) == PunchingBag.Colour.Red) {
					g.setColor(Color.red);
				} else if (bag.getLED(x, y) == PunchingBag.Colour.Green) {
					g.setColor(Color.green);
				} else if (bag.getLED(x, y) == PunchingBag.Colour.Yellow) {
					g.setColor(Color.yellow);
				} else {
					g.setColor(Color.white);
				}

				g.fillOval(0, 0, this.getWidth(), this.getHeight());
			}

			@Override
			public void LEDchanged() {
				this.repaint();
			}

		}

		class Button extends JPanel implements MouseListener {
			final int x;
			final int y;

			public Button(int x, int y) {
				this.x = x;
				this.y = y;
				setBorder(BorderFactory.createLineBorder(Color.black));
				this.addMouseListener(this);
			}

			public Dimension getPreferredSize() {
				return new Dimension(30, 30);
			}

			public void paintComponent(Graphics g) {
				super.paintComponent(g);

				g.setColor(Color.black);
				g.fill3DRect(0, 0, this.getWidth(), this.getHeight(), true);

			}

			public void mouseClicked(MouseEvent arg0) {
			}

			public void mouseEntered(MouseEvent arg0) {
			}

			public void mouseExited(MouseEvent arg0) {
			}

			public void mousePressed(MouseEvent arg0) {
				System.out.println("Button Pressed");
				bag.buttonPressed(x, y);
			}

			public void mouseReleased(MouseEvent arg0) {
			}

		}

		public void buttonPressed(int x, int y) {
			this.repaint();
		}

		public void LEDchanged() {
			this.repaint();

		}

		@Override
		public void mouseClicked(MouseEvent arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void mouseEntered(MouseEvent arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void mouseExited(MouseEvent arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void mousePressed(MouseEvent me) {
			if (currentMode == Mode.Interactive) {
				// bag.buttonPressed(me.getX()/(buttonSize+ledSize),
				// me.getY()/(buttonSize+ledSize));
			}
		}

		@Override
		public void mouseReleased(MouseEvent arg0) {
			// TODO Auto-generated method stub

		}
	}

	public void runInteractively(Contact contact) {
		System.out.println("Contact");
		bag.buttonPressed(contact.x, contact.y);
	}

	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void mousePressed(MouseEvent arg0) {
		/*
		 * if (currentMode == Mode.Interactive) { runInteractively(new
		 * Contact(System.currentTimeMillis(), (arg0 .getX() - 4) / 40,
		 * (arg0.getY() - 30) / 40, 0)); }
		 */
	}

	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void mouseDragged(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void mouseMoved(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void contact(Contact c) {
		// TODO Auto-generated method stub

	}

	@Override
	public void buttonPressed(int x, int y) {
		System.out.println("Button Pressed: " + x + " " + y);
		bag.circleExpand(x, y, 16);

	}

	@Override
	public void LEDchanged() {
		/*
		 * for (int x = 0; x < bag.ledWidth; x++) { for (int y = 0; y <
		 * bag.ledHeight; y++) { if (useDebugScreen) { Graphics g =
		 * bagSimFrame.getGraphics(); if (bag.getLED(x, y) ==
		 * PunchingBag.Colour.Red) { g.setColor(Color.red); g.fillOval(40 * x,
		 * 40 * y, 6, 6); } else if (bag.getLED(x, y) ==
		 * PunchingBag.Colour.Green) { g.setColor(Color.green); g.fillOval(40 *
		 * x, 40 * y, 6, 6); } else if (bag.getLED(x, y) ==
		 * PunchingBag.Colour.Yellow) { g.setColor(Color.yellow); g.fillOval(40
		 * * x, 40 * y, 6, 6); } else { g.setColor(Color.white); g.fillOval(40 *
		 * x, 40 * y, 6, 6); } } } }
		 */

	}

	static PunchingBag getPunchingBag() {
		return bag;
	}

}