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/PunchingBag.java | 31 ++++++++++++---------- PunchingBag/src/PunchingBagGUI.java | 52 ++++++++++++++++++++++--------------- 2 files changed, 49 insertions(+), 34 deletions(-) diff --git a/PunchingBag/src/PunchingBag.java b/PunchingBag/src/PunchingBag.java index 7833eaf..7f7b044 100644 --- a/PunchingBag/src/PunchingBag.java +++ b/PunchingBag/src/PunchingBag.java @@ -78,9 +78,9 @@ public class PunchingBag implements Runnable { return false; } } - + public void setLED(int x, int y, Colour colour) { - runningEffects.add(new Point(x,y,colour)); + runningEffects.add(new Point(x, y, colour)); } void circleExpand(int x, int y, int intensity) { @@ -99,21 +99,20 @@ public class PunchingBag implements Runnable { abstract public void draw(); } - + class Point extends Effect { final int x; final int y; final Colour colour; - + public Point(int x, int y, Colour colour) { this.x = x; this.y = y; this.colour = colour; } - public void draw() { - setLEDInternal(x,y,colour); + setLEDInternal(x, y, colour); } } @@ -252,18 +251,24 @@ public class PunchingBag implements Runnable { */ } - /* Clears the led grid and stops all running effects */ + /* Clears the led grid */ private void clearLEDGrid() { - /* - * for (Iterator iter = runningEffects.iterator(); - * iter.hasNext();) { ((Effect) iter.next()).stop(); } - */ for (int x = 0; x < ledWidth; x++) { for (int y = 0; y < ledHeight; y++) { leds[x][y] = Colour.None; } } - // Arrays.fill(leds,Colour.None); + } + + private void clearEffects() { + for (Iterator iter = runningEffects.iterator(); iter.hasNext();) { + ((Effect) iter.next()).stop(); + } + } + + public void clearLEDs() { + clearLEDGrid(); + clearEffects(); } public void setLEDGridIntensity(byte grid, byte intensity) { @@ -294,7 +299,7 @@ public class PunchingBag implements Runnable { if (ef.stop) { iter.remove(); } else {// if ((ef.lastRefresh + (1000 / - // ef.refreshRate)) <= + // ef.refreshRate)) <= // Systems.currentTimeMillis()) { ef.draw(); ef.lastRefresh = System.currentTimeMillis(); 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