aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristopher Baines <cbaines8@gmail.com>2011-09-08 07:32:05 +0100
committerChristopher Baines <cbaines8@gmail.com>2011-09-08 07:32:05 +0100
commitc7d181a016ab908e47e8994bb62003f8aa61bbfd (patch)
tree5b025a6355ed6edf96444847bb650dbd98215f75
parent15fa1dd190d8e211a790372156bf4d76c569c3a6 (diff)
downloadpunchingbag-c7d181a016ab908e47e8994bb62003f8aa61bbfd.tar
punchingbag-c7d181a016ab908e47e8994bb62003f8aa61bbfd.tar.gz
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.
-rw-r--r--PunchingBag/src/PunchingBag.java31
-rw-r--r--PunchingBag/src/PunchingBagGUI.java52
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<Effect> 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<Effect> 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;