diff options
Diffstat (limited to 'PunchingBag/src/PunchingBag.java')
-rw-r--r-- | PunchingBag/src/PunchingBag.java | 76 |
1 files changed, 58 insertions, 18 deletions
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); |