From c7d181a016ab908e47e8994bb62003f8aa61bbfd Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Thu, 8 Sep 2011 07:32:05 +0100 Subject: Added clear button to the LEDControlPanel. TODO for Adam, make the ripple effect start square around the button, then expand, instead of expanding from a single point to the top left. --- PunchingBag/src/PunchingBagGUI.java | 52 ++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 21 deletions(-) (limited to 'PunchingBag/src/PunchingBagGUI.java') diff --git a/PunchingBag/src/PunchingBagGUI.java b/PunchingBag/src/PunchingBagGUI.java index 60cacaa..5f1b571 100644 --- a/PunchingBag/src/PunchingBagGUI.java +++ b/PunchingBag/src/PunchingBagGUI.java @@ -22,8 +22,7 @@ import javax.swing.JList; import javax.swing.JPanel; import javax.swing.ListCellRenderer; -public class PunchingBagGUI implements MouseListener, MouseMotionListener, - ButtonListener, LEDListener { +public class PunchingBagGUI implements MouseListener, MouseMotionListener, ButtonListener, LEDListener { public static enum Mode { Menu, Interactive, Game @@ -70,11 +69,11 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener, } class LEDControlPanel extends JPanel implements ActionListener { - String[] colours = {" ", "R", "Y", "G" }; + String[] colours = { " ", "R", "Y", "G" }; public LEDControlPanel() { this.setLayout(new GridBagLayout()); - + GridBagConstraints c = new GridBagConstraints(); c.fill = GridBagConstraints.BOTH; c.anchor = GridBagConstraints.CENTER; @@ -85,30 +84,44 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener, 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); + // rygCombo.setRenderer(renderer); this.add(rygCombo, c); } } + + c.gridx = 0; + c.gridy = 20; + c.fill = GridBagConstraints.HORIZONTAL; + c.anchor = GridBagConstraints.CENTER; + c.gridwidth = 9; + JButton clear = new JButton("Clear"); + clear.setActionCommand("C"); + clear.addActionListener(this); + this.add(clear, 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; + if (a.getActionCommand() == "C") { + bag.clearLEDs(); + } else { + 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); } - 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 { @@ -116,9 +129,7 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener, super(); } - public Component getListCellRendererComponent(JList list, - Object value, int index, boolean isSelected, - boolean cellHasFocus) { + 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.) @@ -139,8 +150,7 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener, } } - class PunchingBagSim extends JPanel implements ButtonListener, LEDListener, - MouseListener { + class PunchingBagSim extends JPanel implements ButtonListener, LEDListener, MouseListener { int buttonMinSize = 10; int buttonMaxSize = 50; @@ -249,7 +259,7 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener, } - class Button extends JPanel implements MouseListener { + class Button extends JComponent implements MouseListener { final int x; final int y; -- cgit v1.2.3