aboutsummaryrefslogtreecommitdiff
path: root/PunchingBag/src/PunchingBag.java
diff options
context:
space:
mode:
Diffstat (limited to 'PunchingBag/src/PunchingBag.java')
-rw-r--r--PunchingBag/src/PunchingBag.java62
1 files changed, 47 insertions, 15 deletions
diff --git a/PunchingBag/src/PunchingBag.java b/PunchingBag/src/PunchingBag.java
index 7f7b044..4bab465 100644
--- a/PunchingBag/src/PunchingBag.java
+++ b/PunchingBag/src/PunchingBag.java
@@ -1,5 +1,6 @@
import javax.swing.event.EventListenerList;
+import java.awt.Rectangle;
import java.awt.geom.Area;
import java.io.IOException;
@@ -15,7 +16,9 @@ public class PunchingBag implements Runnable {
final public int ledHeight = 20;
final public int ledWidth = 9;
private byte[] rawLeds = new byte[6 * 8];
- private Colour[][] leds = new Colour[ledWidth][ledHeight];
+ private Colour[][] leds = new Colour[ledWidth][ledHeight]; // NEVER ACCESS
+ // DIRECTLY (Use
+ // SetLedInternal)
private byte[] ledGridIntensities = new byte[6];
final public int buttonHeight = 19;
final public int buttonWidth = 8;
@@ -87,6 +90,10 @@ public class PunchingBag implements Runnable {
runningEffects.add(new CircleExpand(x, y, 100));
}
+ void noise(Rectangle rect, long time) {
+ runningEffects.add(new Noise(rect, System.currentTimeMillis() + time));
+ }
+
abstract class Effect {
long lastRefresh = 0;
boolean stop = false;
@@ -168,13 +175,44 @@ public class PunchingBag implements Runnable {
// longhand: currentRadius = currentRadius + 0.1 + (currentRadius /
// 10);
- if (!drawEllipse(x, y, (int) currentRadius, (int) currentRadius,
- colour))
+ if (!drawEllipse(x, y, (int) currentRadius, (int) currentRadius, colour))
stop = true;
}
}
+ class Noise extends Effect {
+ final Rectangle area;
+ final long endTime;
+
+ public Noise(Rectangle area, long endTime) {
+ this.area = area;
+ this.endTime = endTime;
+ }
+
+ @Override
+ public void draw() {
+ if (endTime >= System.currentTimeMillis()) {
+ for (int y = area.y; y < (area.y + area.height); y++) {
+ for (int x = area.x; x < (area.x + area.width); x++) {
+ double random = Math.random();
+ if (random < 0.25) {
+ setLEDInternal(x, y, Colour.Red);
+ } else if (random < 0.5) {
+ setLEDInternal(x, y, Colour.Yellow);
+ } else if (random < 0.75) {
+ setLEDInternal(x, y, Colour.Green);
+ } else {
+ setLEDInternal(x, y, Colour.None);
+ }
+ }
+ }
+ } else {
+ stop();
+ }
+ }
+ }
+
public boolean drawEllipse(int x, int y, int radx, int rady, Colour colour) {
if (radx == 0 && rady == 0) {
return setLEDInternal(x, y, colour);
@@ -226,15 +264,12 @@ public class PunchingBag implements Runnable {
for (int y = 0; y <= 18; y++) {
for (int x = 0; x <= 6; x++) {
if ((y % 2) == 0) {
- if (leds[x][y] == Colour.Green
- || leds[x][y] == Colour.Yellow) {
- rawLeds[(int) Math.floor(y / 4)] = (byte) (rawLeds[(int) Math
- .floor(y / 4)] | (1 << (7 - x)));
+ if (leds[x][y] == Colour.Green || leds[x][y] == Colour.Yellow) {
+ rawLeds[(int) Math.floor(y / 4)] = (byte) (rawLeds[(int) Math.floor(y / 4)] | (1 << (7 - x)));
}
} else {
if (leds[x][y] == Colour.Red || leds[x][y] == Colour.Yellow) {
- rawLeds[(int) Math.floor(y / 4)] = (byte) (rawLeds[(int) Math
- .floor(y / 4)] | (1 << (7 - x)));
+ rawLeds[(int) Math.floor(y / 4)] = (byte) (rawLeds[(int) Math.floor(y / 4)] | (1 << (7 - x)));
}
}
}
@@ -293,8 +328,7 @@ public class PunchingBag implements Runnable {
// ConcurrentModificationException's
// (havent managed to produce one
// though
- for (Iterator<Effect> iter = runningEffects.iterator(); iter
- .hasNext();) {
+ for (Iterator<Effect> iter = runningEffects.iterator(); iter.hasNext();) {
Effect ef = (Effect) iter.next();
if (ef.stop) {
iter.remove();
@@ -320,16 +354,14 @@ public class PunchingBag implements Runnable {
*/
if (ledsChanged) {
- LEDListener[] LEDListeners = ledListenerList
- .getListeners(LEDListener.class);
+ 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);
+ 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++) {