aboutsummaryrefslogtreecommitdiff
path: root/PunchingBag/src
diff options
context:
space:
mode:
Diffstat (limited to 'PunchingBag/src')
-rw-r--r--PunchingBag/src/PunchingBag.java88
-rw-r--r--PunchingBag/src/PunchingBagGUI.java15
2 files changed, 53 insertions, 50 deletions
diff --git a/PunchingBag/src/PunchingBag.java b/PunchingBag/src/PunchingBag.java
index d09d8e6..b36f562 100644
--- a/PunchingBag/src/PunchingBag.java
+++ b/PunchingBag/src/PunchingBag.java
@@ -45,7 +45,7 @@ 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];
}
@@ -64,10 +64,11 @@ public class PunchingBag implements Runnable {
}
void CircleExpand(int x, int y, int intensity) {
- runningEffects.add(new CircleExpand(x,y,100));
+ runningEffects.add(new CircleExpand(x, y, 100));
}
- abstract class Effect implements Runnable {
+ abstract class Effect {
+ long lastRefresh = 0;
boolean stop = false;
int refreshRate = 60; // Times per second to refresh
@@ -75,18 +76,6 @@ public class PunchingBag implements Runnable {
stop = true;
}
- public void run() {
- while (!stop) {
- draw();
- try {
- Thread.sleep(1000 / 60);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- runningEffects.remove(this);
- }
-
abstract public void draw();
}
@@ -102,7 +91,6 @@ public class PunchingBag implements Runnable {
this.x = x;
this.y = y;
this.intensity = intensity;
- new Thread(this).start();
}
public void draw() {
@@ -115,10 +103,11 @@ public class PunchingBag implements Runnable {
} else {
colour = Colour.Green;
}
-
+
intensity -= 5;
- currentRadius += 0.1 + (currentRadius / 10);
+ currentRadius += 0.1 + 0.1 + (currentRadius / 20);
+ // currentRadius += 0.01;
// longhand: currentRadius = currentRadius + 0.1 + (currentRadius /
// 10);
@@ -211,16 +200,17 @@ public class PunchingBag implements Runnable {
}
/* Clears the led grid and stops all running effects */
- 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++) {
+ 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);
+ // Arrays.fill(leds,Colour.None);
}
public void setLEDGridIntensity(byte grid, byte intensity) {
@@ -230,7 +220,7 @@ 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;
@@ -238,36 +228,56 @@ public class PunchingBag implements Runnable {
public void run() {
while (true) {
+ synchronized (runningEffects) { // Should prevent
+ // ConcurrentModificationException's
+ // (havent managed to produce one
+ // though
+ for (Iterator<Effect> iter = runningEffects.iterator(); iter
+ .hasNext();) {
+ Effect ef = (Effect) iter.next();
+ if (ef.stop) {
+ iter.remove();
+ } else if ((ef.lastRefresh + (1000 / ef.refreshRate)) <= System
+ .currentTimeMillis()) {
+ ef.draw();
+ ef.lastRefresh = System.currentTimeMillis();
+ }
+ }
+ }
+
if (ledsChanged) {
- LEDListener[] LEDListeners = ledListenerList.getListeners(LEDListener.class);
- for (int i=0; i<ledListenerList.getListenerCount(); i++) {
+ 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);
+ 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++) {
+ 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);
+ Thread.sleep(1000 / 60);
} catch (InterruptedException e) {
e.printStackTrace();
}
diff --git a/PunchingBag/src/PunchingBagGUI.java b/PunchingBag/src/PunchingBagGUI.java
index f3997a8..1db9d68 100644
--- a/PunchingBag/src/PunchingBagGUI.java
+++ b/PunchingBag/src/PunchingBagGUI.java
@@ -56,15 +56,6 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener,
bag.addButtonListener(this);
bag.addLEDChangeListener(this);
-
- draw();
- }
-
- void draw() throws InterruptedException {
- bagSimFrame.paintComponents(bagSimFrame.getGraphics());
- // bagSimFrame.paintAll(bagSimFrame.getGraphics());
- Thread.sleep(50);
- draw();
}
public void runInteractively(Contact contact) {
@@ -133,10 +124,12 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener,
} 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) {
+ } 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);
}
}
}