From cfa29ad09cc9974a0a55e5b25bcc5f59a71d5950 Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Mon, 5 Sep 2011 23:09:28 +0100 Subject: Improved the Effect API, threading and display. --- PunchingBag/src/PunchingBag.java | 88 +++++++++++++++++++++---------------- PunchingBag/src/PunchingBagGUI.java | 15 ++----- 2 files changed, 53 insertions(+), 50 deletions(-) (limited to 'PunchingBag') 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 iter = runningEffects.iterator(); iter.hasNext();) { - ((Effect) iter.next()).stop(); - }*/ - for (int x=0; x 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 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