aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristopher Baines <cbaines8@gmail.com>2011-09-04 23:13:39 +0100
committerChristopher Baines <cbaines8@gmail.com>2011-09-04 23:13:39 +0100
commitfcd9bda1916ee874b843ae257ba9ac1a75a63c07 (patch)
tree6ed157c1ea12d38b468eed9870d1ebd98f758574
parentb7768fde2cbbb4f053bc394c4591659a90bd0238 (diff)
downloadpunchingbag-fcd9bda1916ee874b843ae257ba9ac1a75a63c07.tar
punchingbag-fcd9bda1916ee874b843ae257ba9ac1a75a63c07.tar.gz
Finished off the listener implementation for the moment. Allowed the debug screen to accept button presses and show leds.
The Ripple still needs more work and more effects need implementing.
-rw-r--r--PunchingBag/src/Arduino.java1
-rw-r--r--PunchingBag/src/PunchingBag.java76
-rw-r--r--PunchingBag/src/PunchingBagGUI.java56
3 files changed, 94 insertions, 39 deletions
diff --git a/PunchingBag/src/Arduino.java b/PunchingBag/src/Arduino.java
index aeaa107..5e6e288 100644
--- a/PunchingBag/src/Arduino.java
+++ b/PunchingBag/src/Arduino.java
@@ -9,7 +9,6 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
-
import gnu.io.*;
public class Arduino {
diff --git a/PunchingBag/src/PunchingBag.java b/PunchingBag/src/PunchingBag.java
index 00a5866..d09d8e6 100644
--- a/PunchingBag/src/PunchingBag.java
+++ b/PunchingBag/src/PunchingBag.java
@@ -13,11 +13,14 @@ public class PunchingBag implements Runnable {
private byte[] rawLeds = new byte[6 * 8];
private Colour[][] leds = new Colour[ledWidth][ledHeight];
private byte[] ledGridIntensities = new byte[6];
- private boolean[][] buttons = new boolean[8][19];
+ final public int buttonHeight = 19;
+ final public int buttonWidth = 8;
+ private boolean[][] buttons = new boolean[buttonWidth][buttonHeight];
+ boolean buttonsChanged = false;
private ArrayList<Effect> runningEffects = new ArrayList<Effect>();
- enum Colour {
+ public enum Colour {
None, Red, Yellow, Green
};
@@ -42,6 +45,10 @@ public class PunchingBag implements Runnable {
public void addLEDChangeListener(LEDListener l) {
ledListenerList.add(LEDListener.class, l);
}
+
+ public Colour getLED(int x, int y) {
+ return leds[x][y];
+ }
public boolean setLED(int x, int y, Colour colour) {
if (x >= 0 && x < ledWidth && y >= 0 && y < ledHeight) {
@@ -56,12 +63,11 @@ public class PunchingBag implements Runnable {
}
}
- void CircleExpand(int x, int y, int delay, Colour colour) {
- // TODO: Adam correct CircleExpand constructor, or this method?
- // runningEffects.add(new CircleExpand(x,y,delay,colour));
+ void CircleExpand(int x, int y, int intensity) {
+ runningEffects.add(new CircleExpand(x,y,100));
}
- class Effect implements Runnable {
+ abstract class Effect implements Runnable {
boolean stop = false;
int refreshRate = 60; // Times per second to refresh
@@ -81,19 +87,18 @@ public class PunchingBag implements Runnable {
runningEffects.remove(this);
}
- private void draw() {
- }
+ abstract public void draw();
}
class CircleExpand extends Effect {
- final byte x;
- final byte y;
- byte intensity;
+ final int x;
+ final int y;
+ int intensity;
boolean drawnSomething = true;
float currentRadius = 0;
- public CircleExpand(byte x, byte y, byte intensity) {
+ public CircleExpand(int x, int y, int intensity) {
this.x = x;
this.y = y;
this.intensity = intensity;
@@ -110,12 +115,14 @@ public class PunchingBag implements Runnable {
} else {
colour = Colour.Green;
}
+
+ intensity -= 5;
currentRadius += 0.1 + (currentRadius / 10);
// longhand: currentRadius = currentRadius + 0.1 + (currentRadius /
// 10);
- if (!DrawEllipse(2, 4, (int) currentRadius, (int) currentRadius,
+ if (!DrawEllipse(x, y, (int) currentRadius, (int) currentRadius,
colour))
stop = true;
}
@@ -204,11 +211,16 @@ public class PunchingBag implements Runnable {
}
/* Clears the led grid and stops all running effects */
- public void clearLEDGrid() {
- for (Iterator<Effect> iter = runningEffects.iterator(); iter.hasNext();) {
+ 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, Boolean.FALSE);
+ //Arrays.fill(leds,Colour.None);
}
public void setLEDGridIntensity(byte grid, byte intensity) {
@@ -218,13 +230,41 @@ public class PunchingBag implements Runnable {
public void setLEDIntensities(byte intensity) {
Arrays.fill(ledGridIntensities, intensity);
}
+
+ public void buttonPressed(int x, int y) {
+ buttons[x][y] = true;
+ buttonsChanged = true;
+ }
public void run() {
while (true) {
if (ledsChanged) {
- //LEDListeners =
- //ledListenerList.
+ LEDListener[] LEDListeners = ledListenerList.getListeners(LEDListener.class);
+ for (int i=0; i<ledListenerList.getListenerCount(); i++) {
+ LEDListeners[i].LEDchanged();
+ }
+ }
+
+ if (buttonsChanged) {
+ ButtonListener[] buttonListeners = buttonListenerList.getListeners(ButtonListener.class);
+ for (int i=0; i<buttonListenerList.getListenerCount(); i++) {
+ for (int x=0; x<buttonWidth; x++) {
+ for (int y=0; y<buttonHeight; y++) {
+ if (buttons[x][y]) buttonListeners[i].buttonPressed(x, y);
+ }
+ }
+ }
+ }
+
+ // TODO: Hack?
+ for (int x=0; x<buttonWidth; x++) {
+ for (int y=0; y<buttonHeight; y++) {
+ buttons[x][y] = false;
+ }
}
+
+ // TODO: Hack?
+ clearLEDGrid();
try {
Thread.sleep(1000 / 120);
diff --git a/PunchingBag/src/PunchingBagGUI.java b/PunchingBag/src/PunchingBagGUI.java
index a999bad..f3997a8 100644
--- a/PunchingBag/src/PunchingBagGUI.java
+++ b/PunchingBag/src/PunchingBagGUI.java
@@ -1,16 +1,13 @@
import java.awt.Color;
import java.awt.Graphics;
-import java.awt.Rectangle;
-import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
-import java.awt.geom.Area;
import javax.swing.JFrame;
-import javax.swing.JPanel;
-public class PunchingBagGUI implements MouseListener, MouseMotionListener, ButtonListener, LEDListener {
+public class PunchingBagGUI implements MouseListener, MouseMotionListener,
+ ButtonListener, LEDListener {
public static enum Mode {
Menu, Interactive, Game
@@ -23,7 +20,7 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener, Butto
static JFrame frame;
static JFrame bagSimFrame;
-
+
static PunchingBag bag = new PunchingBag();
// Contact
@@ -34,10 +31,10 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener, Butto
final boolean useDebugScreen = true;
public static void main(String[] args) throws InterruptedException {
- //SlaveArduino.listPorts();
-
+ // SlaveArduino.listPorts();
+
new PunchingBagGUI();
-
+
}
PunchingBagGUI() throws InterruptedException {
@@ -56,25 +53,25 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener, Butto
System.out.println("Bag X: " + bagSimFrame.getX() + " Bag Y: "
+ bagSimFrame.getY());
-
+
bag.addButtonListener(this);
bag.addLEDChangeListener(this);
-
+
draw();
}
void draw() throws InterruptedException {
bagSimFrame.paintComponents(bagSimFrame.getGraphics());
// bagSimFrame.paintAll(bagSimFrame.getGraphics());
- Thread.sleep(16);
+ Thread.sleep(50);
draw();
}
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
@@ -92,7 +89,8 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener, Butto
public void mousePressed(MouseEvent arg0) {
if (currentMode == Mode.Interactive) {
- runInteractively(new Contact(System.currentTimeMillis(), (arg0.getX() - 4)/ 40 , (arg0.getY() - 30) / 40, 0));
+ runInteractively(new Contact(System.currentTimeMillis(), (arg0
+ .getX() - 4) / 40, (arg0.getY() - 30) / 40, 0));
}
}
@@ -113,19 +111,37 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener, Butto
public void contact(Contact c) {
// TODO Auto-generated method stub
-
+
}
@Override
public void buttonPressed(int x, int y) {
- // TODO Auto-generated method stub
-
+ System.out.println("Button Pressed: " + x + " " + y);
+ bag.CircleExpand(x, y, 16);
+
}
@Override
public void LEDchanged() {
- // TODO Auto-generated method stub
-
+ 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);
+ }
+ if (bag.getLED(x, y) == PunchingBag.Colour.Yellow) {
+ g.setColor(Color.yellow);
+ g.fillOval(40 * x, 40 * y, 6, 6);
+ }
+ }
+ }
+ }
+
}
}