diff options
author | Christopher Baines <cbaines8@gmail.com> | 2011-09-14 10:42:54 +0100 |
---|---|---|
committer | Christopher Baines <cbaines8@gmail.com> | 2011-09-14 10:42:54 +0100 |
commit | ae8432ffe5cea8421b04068d0fe62d8e15175505 (patch) | |
tree | 118bb4c9ed8270fdf194b92ae216e1c85c0c4f6f | |
parent | f4b220237f3e7d16618c9e95ebb03a9fb6991aa2 (diff) | |
download | punchingbag-ae8432ffe5cea8421b04068d0fe62d8e15175505.tar punchingbag-ae8432ffe5cea8421b04068d0fe62d8e15175505.tar.gz |
New package management, needs more work, not enough is public. Split
programs from the gui, these are included as examples.
16 files changed, 1143 insertions, 969 deletions
diff --git a/PunchingBag/G4.wav b/PunchingBag/G4.wav Binary files differnew file mode 100644 index 0000000..fb50cc5 --- /dev/null +++ b/PunchingBag/G4.wav diff --git a/PunchingBag/src/ButtonArduino.java b/PunchingBag/src/ButtonArduino.java deleted file mode 100644 index b56a9ed..0000000 --- a/PunchingBag/src/ButtonArduino.java +++ /dev/null @@ -1,9 +0,0 @@ -
-public class ButtonArduino extends Arduino {
-
- void setButtons(boolean[][] buttons) {
-
-
- }
-
-}
diff --git a/PunchingBag/src/ButtonListener.java b/PunchingBag/src/ButtonListener.java deleted file mode 100644 index 3338a08..0000000 --- a/PunchingBag/src/ButtonListener.java +++ /dev/null @@ -1,7 +0,0 @@ -import java.util.EventListener; - -interface ButtonListener extends EventListener { - - void buttonPressed(int x, int y); - -} diff --git a/PunchingBag/src/Contact.java b/PunchingBag/src/Contact.java deleted file mode 100644 index 8e8970b..0000000 --- a/PunchingBag/src/Contact.java +++ /dev/null @@ -1,19 +0,0 @@ - - - class Contact { - // Height of bag is - - final long time; // Time of the contact with the bag (in millis) - final int x; - final int y; - final int strength; // 0 - 100 - - Contact(long time, int x, int y, int strength) { - this.time = time; - this.x = x; - this.y = y; - this.strength = strength; - } - - - }
\ No newline at end of file diff --git a/PunchingBag/src/AccelListener.java b/PunchingBag/src/uk/ac/open/punchingbag/AccelListener.java index a3ad465..59b1fa0 100644 --- a/PunchingBag/src/AccelListener.java +++ b/PunchingBag/src/uk/ac/open/punchingbag/AccelListener.java @@ -1,3 +1,4 @@ +package uk.ac.open.punchingbag; import java.util.EventListener; interface AccelListener extends EventListener { diff --git a/PunchingBag/src/Arduino.java b/PunchingBag/src/uk/ac/open/punchingbag/Arduino.java index 4fa7cfa..46ed3b5 100644 --- a/PunchingBag/src/Arduino.java +++ b/PunchingBag/src/uk/ac/open/punchingbag/Arduino.java @@ -1,242 +1,243 @@ -import java.util.Enumeration;
-import java.util.HashSet;
-
-import gnu.io.CommPort;
-import gnu.io.CommPortIdentifier;
-import gnu.io.SerialPort;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-
-import javax.swing.event.EventListenerList;
-
-import gnu.io.*;
-
-public class Arduino implements Runnable {
-
- InputStream in;
- OutputStream out;
-
- private EventListenerList readListenerList = new EventListenerList();
- private EventListenerList writeListenerList = new EventListenerList();
-
- public void addSerialWriteListener(SerialWriteListener l) {
- writeListenerList.add(SerialWriteListener.class, l);
- }
-
- public void addSerialReadListener(SerialReadListener l) {
- readListenerList.add(SerialReadListener.class, l);
- }
-
- static void listPorts()
- {
- System.out.println("Listing Comm ports:");
- java.util.Enumeration<CommPortIdentifier> portEnum = CommPortIdentifier.getPortIdentifiers();
- while ( portEnum.hasMoreElements() )
- {
- CommPortIdentifier portIdentifier = portEnum.nextElement();
- System.out.println(portIdentifier.getName() + " - " + getPortTypeName(portIdentifier.getPortType()) );
- }
- }
-
- static String getPortTypeName ( int portType )
- {
- switch ( portType )
- {
- case CommPortIdentifier.PORT_I2C:
- return "I2C";
- case CommPortIdentifier.PORT_PARALLEL:
- return "Parallel";
- case CommPortIdentifier.PORT_RAW:
- return "Raw";
- case CommPortIdentifier.PORT_RS485:
- return "RS485";
- case CommPortIdentifier.PORT_SERIAL:
- return "Serial";
- default:
- return "unknown type";
- }
- }
-
- public String getID() throws IOException {
- byte[] idBytes = {0x49, 0x44};
- this.write(idBytes);
- try {
- Thread.sleep(100);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- int id;
- while ((id = this.read()) == -1) {}
- if (id == 66) {
- return "B";
- } else if (id == 76) {
- return "L";
- } else {
- return "ERROR " + id;
- }
- }
-
- /**
- * @return A HashSet containing the CommPortIdentifier for all serial ports that are not currently being used.
- */
- public static HashSet<CommPortIdentifier> getAvailableSerialPorts() {
- HashSet<CommPortIdentifier> h = new HashSet<CommPortIdentifier>();
- Enumeration thePorts = CommPortIdentifier.getPortIdentifiers();
- while (thePorts.hasMoreElements()) {
- CommPortIdentifier com = (CommPortIdentifier) thePorts.nextElement();
- switch (com.getPortType()) {
- case CommPortIdentifier.PORT_SERIAL:
- try {
- CommPort thePort = com.open("CommUtil", 50);
- thePort.close();
- h.add(com);
- //System.out.println("Found a port: " + com.getPortType());
- } catch (PortInUseException e) {
- System.out.println("Port, " + com.getName() + ", is in use.");
- } catch (Exception e) {
- System.err.println("Failed to open port " + com.getName());
- e.printStackTrace();
- }
- }
- }
- return h;
- }
-
- void connect ( String portName ) throws Exception
- {
- CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
- if ( portIdentifier.isCurrentlyOwned() )
- {
- System.out.println("Error: Port is currently in use");
- }
- else
- {
- CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);
-
- if ( commPort instanceof SerialPort )
- {
- SerialPort serialPort = (SerialPort) commPort;
- serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
-
- in = serialPort.getInputStream();
- out = serialPort.getOutputStream();
-
- //(new Thread(this)).start();
- }
- else
- {
- System.out.println("Error: Only serial ports are handled by this example.");
- }
- }
- }
-
- /** */
- public class SerialReader implements Runnable
- {
- InputStream in;
-
- public SerialReader ( InputStream in )
- {
- this.in = in;
- }
-
- public void run ()
- {
- byte[] buffer = new byte[1024];
- int len = -1;
- try
- {
- while ( ( len = this.in.read(buffer)) > -1 )
- {
- System.out.print(new String(buffer,0,len));
- input(new String(buffer,0,len));
- }
- }
- catch ( IOException e )
- {
- e.printStackTrace();
- }
- }
- }
-
- public static void input(String string) {
-
- }
-
- /** */
- public class SerialWriter implements Runnable
- {
- OutputStream out;
-
- public SerialWriter ( OutputStream out )
- {
- this.out = out;
- }
-
- public void run ()
- {
- try
- {
- int c = 0;
- while ( ( c = System.in.read()) > -1 )
- {
- this.out.write(c);
- }
- }
- catch ( IOException e )
- {
- e.printStackTrace();
- }
- }
- }
-
- public void run() {
- byte[] buffer = new byte[1024];
- int len = -1;
- try
- {
- while ( ( len = this.in.read(buffer)) > -1 )
- {
- System.out.print(new String(buffer,0,len));
- //input(new String(buffer,0,len));
- }
- }
- catch ( IOException e )
- {
- e.printStackTrace();
- }
-
- }
-
-
- public void write(byte[] bytes) throws IOException {
- out.write(bytes);
- SerialWriteListener[] sListeners = writeListenerList
- .getListeners(SerialWriteListener.class);
- for (int i = 0; i < writeListenerList.getListenerCount(); i++) {
- sListeners[i].serialWriteEvent(bytes);
- }
- }
-
- public void write(byte b) throws IOException {
- out.write(b);
- SerialWriteListener[] sListeners = writeListenerList
- .getListeners(SerialWriteListener.class);
- for (int i = 0; i < writeListenerList.getListenerCount(); i++) {
- sListeners[i].serialWriteEvent(b);
- }
- }
-
- public int read() throws IOException {
- int read = in.read();
- SerialReadListener[] sListeners = readListenerList
- .getListeners(SerialReadListener.class);
- for (int i = 0; i < readListenerList.getListenerCount(); i++) {
- sListeners[i].serialReadEvent(read);
- }
- return read;
- }
-
-}
+package uk.ac.open.punchingbag; +import java.util.Enumeration; +import java.util.HashSet; + +import gnu.io.CommPort; +import gnu.io.CommPortIdentifier; +import gnu.io.SerialPort; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import javax.swing.event.EventListenerList; + +import gnu.io.*; + +public class Arduino implements Runnable { + + InputStream in; + OutputStream out; + + private EventListenerList readListenerList = new EventListenerList(); + private EventListenerList writeListenerList = new EventListenerList(); + + public void addSerialWriteListener(SerialWriteListener l) { + writeListenerList.add(SerialWriteListener.class, l); + } + + public void addSerialReadListener(SerialReadListener l) { + readListenerList.add(SerialReadListener.class, l); + } + + static void listPorts() + { + System.out.println("Listing Comm ports:"); + java.util.Enumeration<CommPortIdentifier> portEnum = CommPortIdentifier.getPortIdentifiers(); + while ( portEnum.hasMoreElements() ) + { + CommPortIdentifier portIdentifier = portEnum.nextElement(); + System.out.println(portIdentifier.getName() + " - " + getPortTypeName(portIdentifier.getPortType()) ); + } + } + + static String getPortTypeName ( int portType ) + { + switch ( portType ) + { + case CommPortIdentifier.PORT_I2C: + return "I2C"; + case CommPortIdentifier.PORT_PARALLEL: + return "Parallel"; + case CommPortIdentifier.PORT_RAW: + return "Raw"; + case CommPortIdentifier.PORT_RS485: + return "RS485"; + case CommPortIdentifier.PORT_SERIAL: + return "Serial"; + default: + return "unknown type"; + } + } + + public String getID() throws IOException { + byte[] idBytes = {0x49, 0x44}; + this.write(idBytes); + try { + Thread.sleep(100); + } catch (InterruptedException e) { + e.printStackTrace(); + } + int id; + while ((id = this.read()) == -1) {} + if (id == 66) { + return "B"; + } else if (id == 76) { + return "L"; + } else { + return "ERROR " + id; + } + } + + /** + * @return A HashSet containing the CommPortIdentifier for all serial ports that are not currently being used. + */ + public static HashSet<CommPortIdentifier> getAvailableSerialPorts() { + HashSet<CommPortIdentifier> h = new HashSet<CommPortIdentifier>(); + Enumeration thePorts = CommPortIdentifier.getPortIdentifiers(); + while (thePorts.hasMoreElements()) { + CommPortIdentifier com = (CommPortIdentifier) thePorts.nextElement(); + switch (com.getPortType()) { + case CommPortIdentifier.PORT_SERIAL: + try { + CommPort thePort = com.open("CommUtil", 50); + thePort.close(); + h.add(com); + //System.out.println("Found a port: " + com.getPortType()); + } catch (PortInUseException e) { + System.out.println("Port, " + com.getName() + ", is in use."); + } catch (Exception e) { + System.err.println("Failed to open port " + com.getName()); + e.printStackTrace(); + } + } + } + return h; + } + + void connect ( String portName ) throws Exception + { + CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName); + if ( portIdentifier.isCurrentlyOwned() ) + { + System.out.println("Error: Port is currently in use"); + } + else + { + CommPort commPort = portIdentifier.open(this.getClass().getName(),2000); + + if ( commPort instanceof SerialPort ) + { + SerialPort serialPort = (SerialPort) commPort; + serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE); + + in = serialPort.getInputStream(); + out = serialPort.getOutputStream(); + + //(new Thread(this)).start(); + } + else + { + System.out.println("Error: Only serial ports are handled by this example."); + } + } + } + + /** */ + public class SerialReader implements Runnable + { + InputStream in; + + public SerialReader ( InputStream in ) + { + this.in = in; + } + + public void run () + { + byte[] buffer = new byte[1024]; + int len = -1; + try + { + while ( ( len = this.in.read(buffer)) > -1 ) + { + System.out.print(new String(buffer,0,len)); + input(new String(buffer,0,len)); + } + } + catch ( IOException e ) + { + e.printStackTrace(); + } + } + } + + public static void input(String string) { + + } + + /** */ + public class SerialWriter implements Runnable + { + OutputStream out; + + public SerialWriter ( OutputStream out ) + { + this.out = out; + } + + public void run () + { + try + { + int c = 0; + while ( ( c = System.in.read()) > -1 ) + { + this.out.write(c); + } + } + catch ( IOException e ) + { + e.printStackTrace(); + } + } + } + + public void run() { + byte[] buffer = new byte[1024]; + int len = -1; + try + { + while ( ( len = this.in.read(buffer)) > -1 ) + { + System.out.print(new String(buffer,0,len)); + //input(new String(buffer,0,len)); + } + } + catch ( IOException e ) + { + e.printStackTrace(); + } + + } + + + public void write(byte[] bytes) throws IOException { + out.write(bytes); + SerialWriteListener[] sListeners = writeListenerList + .getListeners(SerialWriteListener.class); + for (int i = 0; i < writeListenerList.getListenerCount(); i++) { + sListeners[i].serialWriteEvent(bytes); + } + } + + public void write(byte b) throws IOException { + out.write(b); + SerialWriteListener[] sListeners = writeListenerList + .getListeners(SerialWriteListener.class); + for (int i = 0; i < writeListenerList.getListenerCount(); i++) { + sListeners[i].serialWriteEvent(b); + } + } + + public int read() throws IOException { + int read = in.read(); + SerialReadListener[] sListeners = readListenerList + .getListeners(SerialReadListener.class); + for (int i = 0; i < readListenerList.getListenerCount(); i++) { + sListeners[i].serialReadEvent(read); + } + return read; + } + +} diff --git a/PunchingBag/src/uk/ac/open/punchingbag/ButtonListener.java b/PunchingBag/src/uk/ac/open/punchingbag/ButtonListener.java new file mode 100644 index 0000000..abea103 --- /dev/null +++ b/PunchingBag/src/uk/ac/open/punchingbag/ButtonListener.java @@ -0,0 +1,10 @@ +package uk.ac.open.punchingbag; +import java.util.EventListener; + +public interface ButtonListener extends EventListener { + + void buttonPressed(int x, int y); + + void contact(Contact c); + +} diff --git a/PunchingBag/src/uk/ac/open/punchingbag/Contact.java b/PunchingBag/src/uk/ac/open/punchingbag/Contact.java new file mode 100644 index 0000000..b33ef53 --- /dev/null +++ b/PunchingBag/src/uk/ac/open/punchingbag/Contact.java @@ -0,0 +1,23 @@ +package uk.ac.open.punchingbag; + + + public class Contact { + // Height of bag is + + public final long time; // Time of the contact with the bag (in millis) + public final int x; + public final int y; + public final int force; // 0 - 100 + + Contact(long time, int x, int y, int force) { + this.time = time; + this.x = x; + this.y = y; + this.force = force; + } + + public String toString() { + return "X: " + x + " Y: " + y + " Force: " + force; + } + + }
\ No newline at end of file diff --git a/PunchingBag/src/Font.java b/PunchingBag/src/uk/ac/open/punchingbag/Font.java index a8996bf..aec6550 100644 --- a/PunchingBag/src/Font.java +++ b/PunchingBag/src/uk/ac/open/punchingbag/Font.java @@ -1,3 +1,4 @@ +package uk.ac.open.punchingbag; public class Font { char font[] = { diff --git a/PunchingBag/src/LEDListener.java b/PunchingBag/src/uk/ac/open/punchingbag/LEDListener.java index 3031f58..a8b9c61 100644 --- a/PunchingBag/src/LEDListener.java +++ b/PunchingBag/src/uk/ac/open/punchingbag/LEDListener.java @@ -1,3 +1,4 @@ +package uk.ac.open.punchingbag; import java.util.EventListener; interface LEDListener extends EventListener { diff --git a/PunchingBag/src/PunchingBag.java b/PunchingBag/src/uk/ac/open/punchingbag/PunchingBag.java index 941e4d7..31b0575 100644 --- a/PunchingBag/src/PunchingBag.java +++ b/PunchingBag/src/uk/ac/open/punchingbag/PunchingBag.java @@ -1,3 +1,5 @@ +package uk.ac.open.punchingbag; + import gnu.io.CommPortIdentifier; import javax.swing.event.EventListenerList; @@ -42,6 +44,15 @@ public class PunchingBag implements Runnable { Arduino buttonArduino = new Arduino(); Arduino ledArduino = new Arduino(); + boolean debugTimings = false; + boolean debugSerial = false; + + static private PunchingBag bag = new PunchingBag(); + + public static PunchingBag getBag() { + return bag; + } + public enum Colour { None, Red, Yellow, Green }; @@ -55,7 +66,7 @@ public class PunchingBag implements Runnable { private EventListenerList ledListenerList = new EventListenerList(); private EventListenerList accelListenerList = new EventListenerList(); - public PunchingBag() { + private PunchingBag() { Thread bagControlThread = new Thread(this); bagControlThread.setPriority(Thread.MAX_PRIORITY); bagControlThread.start(); @@ -100,7 +111,7 @@ public class PunchingBag implements Runnable { runningEffects.add(new Point(x, y, colour)); } - void circleExpand(int x, int y, int intensity) { + public void circleExpand(int x, int y, int intensity) { runningEffects.add(new CircleExpand(x, y, 100)); } @@ -108,10 +119,10 @@ public class PunchingBag implements Runnable { runningEffects.add(new SquareExpand(x, y, intensity, colour)); } - void fillRect(int x, int y, int width, int height, Colour colour, long time) { + public void fillRect(int x, int y, int width, int height, Colour colour, long time) { runningEffects.add(new FillRect(x, y, width, height, colour, time)); } - + void rect(int x, int y, int width, int height, Colour colour) { runningEffects.add(new Rect(x, y, width, height, colour)); } @@ -173,7 +184,7 @@ public class PunchingBag implements Runnable { } } } - + class Rect extends Effect { final int x; final int y; @@ -310,8 +321,7 @@ public class PunchingBag implements Runnable { } } - public boolean drawRectCenter(int x, int y, int height, int width, - Colour colour) { + public boolean drawRectCenter(int x, int y, int height, int width, Colour colour) { if (height < 0) { height = 0; } @@ -327,20 +337,16 @@ public class PunchingBag implements Runnable { int heightEx = 0; do { - if (setLEDInternal((x - (height / 2)) + widthEx, y - (height / 2), - colour)) { + if (setLEDInternal((x - (height / 2)) + widthEx, y - (height / 2), colour)) { doneSomething = true; } - if (setLEDInternal(x - (height / 2), (y - (height / 2)) + heightEx, - colour)) { + if (setLEDInternal(x - (height / 2), (y - (height / 2)) + heightEx, colour)) { doneSomething = true; } - if (setLEDInternal((x - (height / 2)) + widthEx, y + (height / 2), - colour)) { + if (setLEDInternal((x - (height / 2)) + widthEx, y + (height / 2), colour)) { doneSomething = true; } - if (setLEDInternal(x + (height / 2), (y - (height / 2)) + heightEx, - colour)) { + if (setLEDInternal(x + (height / 2), (y - (height / 2)) + heightEx, colour)) { doneSomething = true; } if (heightEx < height) { @@ -355,8 +361,7 @@ public class PunchingBag implements Runnable { return doneSomething; } - public boolean drawRectInternal(int x, int y, int width, int height, - Colour colour) { + public boolean drawRectInternal(int x, int y, int width, int height, Colour colour) { if (height < 0) { height = 0; } @@ -461,8 +466,7 @@ public class PunchingBag implements Runnable { return drawnSomething; } - public boolean fillRectInternal(int x, int y, int height, int width, - Colour colour) { + public boolean fillRectInternal(int x, int y, int height, int width, Colour colour) { boolean doneSomething = false; for (int px = x; px < (x + height); px++) { @@ -485,16 +489,12 @@ public class PunchingBag implements Runnable { for (int x = 0; x < 8; x++) { for (int y = 0; y < 8; y++) { if ((y % 2) == 0) { - if (leds[1 + x][(grid * 4) + (y / 2)] == Colour.Green - || leds[1 + x][(grid * 4) + (y / 2)] == Colour.Yellow) { - rawLeds[(grid * 8) + x] = (byte) (rawLeds[(grid * 8) - + x] | (1 << (7 - y))); + if (leds[1 + x][(grid * 4) + (y / 2)] == Colour.Green || leds[1 + x][(grid * 4) + (y / 2)] == Colour.Yellow) { + rawLeds[(grid * 8) + x] = (byte) (rawLeds[(grid * 8) + x] | (1 << (7 - y))); } } else { - if (leds[1 + x][(grid * 4) + (y / 2)] == Colour.Red - || leds[1 + x][(grid * 4) + (y / 2)] == Colour.Yellow) { - rawLeds[(grid * 8) + x] = (byte) (rawLeds[(grid * 8) - + x] | (1 << (7 - y))); + if (leds[1 + x][(grid * 4) + (y / 2)] == Colour.Red || leds[1 + x][(grid * 4) + (y / 2)] == Colour.Yellow) { + rawLeds[(grid * 8) + x] = (byte) (rawLeds[(grid * 8) + x] | (1 << (7 - y))); } } } @@ -571,8 +571,7 @@ public class PunchingBag implements Runnable { // TODO Auto-generated catch block e1.printStackTrace(); } - HashSet<CommPortIdentifier> serialPorts = Arduino - .getAvailableSerialPorts(); + HashSet<CommPortIdentifier> serialPorts = Arduino.getAvailableSerialPorts(); for (Iterator iter = serialPorts.iterator(); iter.hasNext();) { CommPortIdentifier comPort = (CommPortIdentifier) iter.next(); // HACK FOR WINDOWS TO IDENIFY PORTS LIKELY TO CONTAIN ARDUINO @@ -591,6 +590,7 @@ public class PunchingBag implements Runnable { } private void readAccelData(String data) { + //System.out.println("Data: " + data); String[] nums = data.split(" "); for (int x = 1; x < 5; x++) { // Regex expression to strip newline at end (normally just 4th) @@ -613,11 +613,11 @@ public class PunchingBag implements Runnable { private void readButtonData(String data) { if (data.replaceAll("[^0-9]", "").length() > 0) { - System.out.print(data); + //System.out.print(data); int num = Integer.valueOf(data.replaceAll("[^0-9]", "")); - int x = ((num-1) % 8); - int y = (num-1) / 8; - System.out.println("X: " + x + " Y: " + y); + int x = ((num - 1) % 8); + int y = (num - 1) / 8; + //System.out.println("X: " + x + " Y: " + y); if (!(x < 0)) { buttons[x][y] = true; buttonsChanged = true; @@ -649,8 +649,11 @@ public class PunchingBag implements Runnable { long timeToSleep = 10; while (true) { - //System.out.println("Time since last refresh: " - // + (System.currentTimeMillis() - lastRefresh)); + if (debugTimings) { + System.out.println("Time since last refresh: " + (System.currentTimeMillis() - lastRefresh)); + if ((System.currentTimeMillis() - lastRefresh) != 0) + System.out.println("FPS: " + (1000 / (System.currentTimeMillis() - lastRefresh))); + } if ((System.currentTimeMillis() - lastRefresh) > (1000 / 60)) { if (timeToSleep > 0) { @@ -659,8 +662,8 @@ public class PunchingBag implements Runnable { } else { timeToSleep++; } - - //System.out.println("Sleeping: " + timeToSleep); + if (debugTimings) + System.out.println("Sleeping: " + timeToSleep); lastRefresh = System.currentTimeMillis(); // System.out.println("R"); @@ -672,8 +675,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(); @@ -685,8 +687,8 @@ public class PunchingBag implements Runnable { } } } - //System.out.println("Effects: " - // + (System.currentTimeMillis() - beginTimeForEffects)); + // System.out.println("Effects: " + // + (System.currentTimeMillis() - beginTimeForEffects)); } long beginTimeButtonIn = System.currentTimeMillis(); @@ -703,7 +705,7 @@ public class PunchingBag implements Runnable { // sb.append((char) read); } // System.out.println(""); - // readAccelData(str); + readAccelData(str); // String[] nums = sbString.trim().split(" "); // System.out.println(nums); // for (int x=0; x<4; x++) { @@ -730,8 +732,8 @@ public class PunchingBag implements Runnable { e.printStackTrace(); } } - //System.out.println("Button: " - // + (System.currentTimeMillis() - beginTimeButtonIn)); + if (debugTimings) + System.out.println("Button: " + (System.currentTimeMillis() - beginTimeButtonIn)); calculateRawLeds(); String str; @@ -752,21 +754,21 @@ 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++) { - if (buttons[x][y]) + if (buttons[x][y]) { buttonListeners[i].buttonPressed(x, y); + buttonListeners[i].contact(new Contact(System.currentTimeMillis(), x, y, bottomAccelX)); + } } } } diff --git a/PunchingBag/src/PunchingBagGUI.java b/PunchingBag/src/uk/ac/open/punchingbag/PunchingBagGUI.java index 5db11ef..e790abb 100644 --- a/PunchingBag/src/PunchingBagGUI.java +++ b/PunchingBag/src/uk/ac/open/punchingbag/PunchingBagGUI.java @@ -1,643 +1,650 @@ -import java.awt.BorderLayout;
-import java.awt.Color;
-import java.awt.Component;
-import java.awt.Dimension;
-import java.awt.Graphics;
-import java.awt.GridBagConstraints;
-import java.awt.GridBagLayout;
-import java.awt.Insets;
-import java.awt.Rectangle;
-import java.awt.event.ActionEvent;
-import java.awt.event.ActionListener;
-import java.awt.event.MouseEvent;
-import java.awt.event.MouseListener;
-import java.awt.event.MouseMotionListener;
-import java.io.File;
-import java.io.IOException;
-
-import javax.swing.BorderFactory;
-import javax.swing.JButton;
-import javax.swing.JComboBox;
-import javax.swing.JComponent;
-import javax.swing.JFrame;
-import javax.swing.JLabel;
-import javax.swing.JList;
-import javax.swing.JPanel;
-import javax.swing.JScrollPane;
-import javax.swing.JSplitPane;
-import javax.swing.JTabbedPane;
-import javax.swing.JTextArea;
-import javax.swing.ListCellRenderer;
-
-import javax.sound.sampled.AudioFormat;
-import javax.sound.sampled.AudioInputStream;
-import javax.sound.sampled.AudioSystem;
-import javax.sound.sampled.Clip;
-import javax.sound.sampled.DataLine;
-import javax.sound.sampled.Line;
-import javax.sound.sampled.LineUnavailableException;
-import javax.sound.sampled.Mixer;
-import javax.sound.sampled.TargetDataLine;
-import javax.sound.sampled.UnsupportedAudioFileException;
-import javax.sound.sampled.AudioFormat.Encoding;
-import javax.sound.sampled.Mixer.Info;
-
-public class PunchingBagGUI implements MouseListener, MouseMotionListener,
- ButtonListener, LEDListener {
-
- public static enum Mode {
- Menu, Interactive, Game
- };
-
- Mode currentMode = Mode.Interactive;
-
- static JFrame frame;
-
- // Contact
- public enum Direction {
- None, Up, Down, Left, Right
- };
-
- String soundDir = System.getProperty("user.dir")
- + System.getProperty("file.separator");
-
- File[] keys = { new File(soundDir + "G4.wav"),
- new File(soundDir + "C5.wav"), new File(soundDir + "D5.wav"),
- new File(soundDir + "E5.wav"), new File(soundDir + "F5.wav"),
- new File(soundDir + "G5.wav") };
-
- static PunchingBag bag = new PunchingBag();
-
- final boolean useDebugScreen = true;
-
- public static void main(String[] args) throws InterruptedException {
- new PunchingBagGUI();
-
- }
-
- PunchingBagGUI() throws InterruptedException {
-
- // System.out.println(AudioSystem.getMixerInfo()[0].getName());
- // System.out.println(AudioSystem.getMixerInfo()[0].getDescription());
- // Mixer mix = AudioSystem.getMixer(AudioSystem.getMixerInfo()[0]);
-
- bag.connectToArduinos();
-
- frame = new JFrame("Punching Bag GUI");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-
- // frame.setSize(600, 600);
- frame.setVisible(true);
-
- frame.addMouseListener(this);
- frame.addMouseMotionListener(this);
-
- bag.addButtonListener(this);
- bag.addLEDChangeListener(this);
-
- JSplitPane bottomTopSplitPane = new JSplitPane(
- JSplitPane.VERTICAL_SPLIT);
-
- JSplitPane centerTopSplitPane = new JSplitPane(
- JSplitPane.HORIZONTAL_SPLIT);
-
- JTabbedPane tabbedPane = new JTabbedPane();
- tabbedPane.addTab("LED Control Panel", null, new LEDControlPanel(),
- "Allows control of individual LED's");
-
- centerTopSplitPane.setLeftComponent(new PunchingBagSim());
- centerTopSplitPane.setRightComponent(tabbedPane);
-
- bottomTopSplitPane.setTopComponent(centerTopSplitPane);
- bottomTopSplitPane.setBottomComponent(new SerialPanel());
-
- frame.add(bottomTopSplitPane, BorderLayout.CENTER);
-
- // frame.pack();
- }
-
- void playKey(int key) {
- try {
- AudioInputStream sound = AudioSystem.getAudioInputStream(keys[key]);
- DataLine.Info dataLine = new DataLine.Info(Clip.class,
- sound.getFormat());
- Clip clip = (Clip) AudioSystem.getLine(dataLine);
- clip.open(sound);
- clip.start();
- } catch (LineUnavailableException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (UnsupportedAudioFileException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
-
- class SerialPanel extends JPanel implements ActionListener,
- SerialReadListener, SerialWriteListener {
- JTextArea buttonIn = new JTextArea();
- JTextArea buttonOut = new JTextArea();
- JTextArea ledIn = new JTextArea();
- JTextArea ledOut = new JTextArea();
-
- public SerialPanel() {
- JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
- JSplitPane buttonSplitPane = new JSplitPane(
- JSplitPane.HORIZONTAL_SPLIT);
- JSplitPane ledSplitPane = new JSplitPane(
- JSplitPane.HORIZONTAL_SPLIT);
- /*
- * splitPane.setOneTouchExpandable(true);
- * buttonSplitPane.setOneTouchExpandable(true);
- * ledSplitPane.setOneTouchExpandable(true);
- */
-
- splitPane.setMinimumSize(new Dimension(200, 50));
- buttonSplitPane.setMinimumSize(new Dimension(100, 50));
- ledSplitPane.setMinimumSize(new Dimension(100, 50));
-
- JButton clear = new JButton("Clear");
- clear.addActionListener(this);
-
- JPanel buttonInPanel = new JPanel();
- JScrollPane areaScrollPane = new JScrollPane(buttonIn);
- areaScrollPane
- .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
- areaScrollPane.setPreferredSize(new Dimension(250, 100));
- buttonInPanel.add(areaScrollPane, BorderLayout.CENTER);
- clear.setActionCommand("BI");
- buttonInPanel.add(clear, BorderLayout.SOUTH);
- buttonInPanel.setBorder(BorderFactory
- .createTitledBorder("Serial In - Button Arduino"));
- buttonSplitPane.setLeftComponent(buttonInPanel);
-
- JPanel buttonOutPanel = new JPanel();
- areaScrollPane = new JScrollPane(buttonOut);
- areaScrollPane
- .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
- areaScrollPane.setPreferredSize(new Dimension(250, 100));
- buttonOutPanel.add(areaScrollPane, BorderLayout.CENTER);
- clear = new JButton("Clear");
- clear.setActionCommand("BO");
- buttonOutPanel.add(clear, BorderLayout.SOUTH);
- buttonOutPanel.setBorder(BorderFactory
- .createTitledBorder("Serial Out - Button Arduino"));
- buttonSplitPane.setRightComponent(buttonOutPanel);
-
- JPanel ledInPanel = new JPanel();
- areaScrollPane = new JScrollPane(ledIn);
- areaScrollPane
- .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
- areaScrollPane.setPreferredSize(new Dimension(250, 100));
- ledInPanel.add(areaScrollPane, BorderLayout.CENTER);
- clear = new JButton("Clear");
- clear.setActionCommand("LI");
- ledInPanel.add(clear, BorderLayout.SOUTH);
- ledInPanel.setBorder(BorderFactory
- .createTitledBorder("Serial In - LED Arduino"));
- ledSplitPane.setLeftComponent(ledInPanel);
-
- JPanel ledOutPanel = new JPanel();
- areaScrollPane = new JScrollPane(ledOut);
- areaScrollPane
- .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
- areaScrollPane.setPreferredSize(new Dimension(250, 100));
- ledOutPanel.add(areaScrollPane, BorderLayout.CENTER);
- clear = new JButton("Clear");
- clear.setActionCommand("LO");
- ledOutPanel.add(clear, BorderLayout.SOUTH);
- ledOutPanel.setBorder(BorderFactory
- .createTitledBorder("Serial Out - LED Arduino"));
- ledSplitPane.setRightComponent(ledOutPanel);
-
- splitPane.setLeftComponent(buttonSplitPane);
- splitPane.setRightComponent(ledSplitPane);
-
- this.add(splitPane);
-
- bag.buttonArduino.addSerialReadListener(this);
- bag.buttonArduino.addSerialWriteListener(this);
-
- }
-
- @Override
- public void actionPerformed(ActionEvent arg0) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void serialWriteEvent(byte b) {
- buttonOut.append(String.valueOf((char) b));
- }
-
- @Override
- public void serialWriteEvent(byte[] b) {
- buttonOut.append(String.valueOf(b));
- }
-
- @Override
- public void serialReadEvent(int b) {
- if (b != -1)
- buttonIn.append(String.valueOf((char) b));
- }
- }
-
- class LEDControlPanel extends JPanel implements ActionListener {
- String[] colours = { " ", "R", "Y", "G" };
-
- public LEDControlPanel() {
- this.setLayout(new GridBagLayout());
-
- GridBagConstraints c = new GridBagConstraints();
- c.fill = GridBagConstraints.BOTH;
- c.anchor = GridBagConstraints.CENTER;
- // c.weightx = 0.2;
- // c.weighty = 0.2;
-
- for (int y = 0; y < bag.ledHeight; y++) {
- for (int x = 0; x < bag.ledWidth; x++) {
- c.gridx = x;
- c.gridy = y;
-
- RYGComboBoxRenderer renderer = new RYGComboBoxRenderer();
- JComboBox rygCombo = new JComboBox(colours);
- rygCombo.setSelectedIndex(0);
- rygCombo.addActionListener(this);
- rygCombo.setActionCommand(String.valueOf(x) + ","
- + String.valueOf(y));
- // rygCombo.setRenderer(renderer);
-
- this.add(rygCombo, c);
- }
- }
-
- c.gridx = 0;
- c.gridy = 20;
- c.fill = GridBagConstraints.HORIZONTAL;
- c.anchor = GridBagConstraints.CENTER;
- c.gridwidth = 9;
- JButton clear = new JButton("Clear");
- clear.setActionCommand("C");
- clear.addActionListener(this);
- this.add(clear, c);
- }
-
- public void actionPerformed(ActionEvent a) {
- if (a.getActionCommand() == "C") {
- bag.clearLEDs();
- } else {
- PunchingBag.Colour colour = PunchingBag.Colour.None;
- if (((JComboBox) a.getSource()).getSelectedIndex() == 1) {
- colour = PunchingBag.Colour.Red;
- } else if (((JComboBox) a.getSource()).getSelectedIndex() == 2) {
- colour = PunchingBag.Colour.Yellow;
- } else if (((JComboBox) a.getSource()).getSelectedIndex() == 3) {
- colour = PunchingBag.Colour.Green;
- }
- System.out.println("Setting "
- + a.getActionCommand().split(",")[0] + " "
- + a.getActionCommand().split(",")[1]);
- bag.setLED(Integer.valueOf(a.getActionCommand().split(",")[0]),
- Integer.valueOf(a.getActionCommand().split(",")[1]),
- colour);
- }
- }
-
- class RYGComboBoxRenderer extends JLabel implements ListCellRenderer {
- public RYGComboBoxRenderer() {
- super();
- }
-
- public Component getListCellRendererComponent(JList list,
- Object value, int index, boolean isSelected,
- boolean cellHasFocus) {
-
- // Get the selected index. (The index param isn't
- // always valid, so just use the value.)
-
- if (isSelected) {
- setBackground(list.getSelectionBackground());
- setForeground(list.getSelectionForeground());
- } else {
- setBackground(list.getBackground());
- setForeground(list.getForeground());
- }
-
- if (index != -1)
- setText(colours[index]);
-
- return this;
- }
- }
- }
-
- class PunchingBagSim extends JPanel implements ButtonListener, LEDListener,
- MouseListener {
- int buttonMinSize = 10;
- int buttonMaxSize = 50;
-
- int ledMinSize = 10;
- int ledMaxSize = 50;
-
- int buttonSize = 40;
- int ledSize = 8;
-
- public PunchingBagSim() {
- /*
- * this.setMinimumSize(new Dimension((bag.buttonWidth *
- * buttonMinSize) + (bag.ledWidth * ledMinSize), (bag.buttonHeight *
- * buttonMinSize) + (bag.ledHeight * ledMaxSize)));
- * this.setMaximumSize(new Dimension((bag.buttonWidth *
- * buttonMaxSize) + (bag.ledWidth * ledMaxSize), (bag.buttonHeight *
- * buttonMaxSize) + (bag.ledHeight * ledMaxSize)));
- */
- bag.addButtonListener(this);
- bag.addLEDChangeListener(this);
-
- this.addMouseListener(this);
-
- GridBagLayout gbl = new GridBagLayout();
- this.setLayout(gbl);
- GridBagConstraints c = new GridBagConstraints();
-
- // c.insets = new Insets(1, 1, 1, 1);
- c.fill = GridBagConstraints.BOTH;
- c.anchor = GridBagConstraints.CENTER;
- // c.weightx = 0.2;
- // c.weighty = 0.2;
- for (int y = 0; y < bag.ledHeight; y++) {
- for (int x = 0; x < bag.ledWidth; x++) {
- c.gridx = x * 2;
- c.gridy = y * 2;
-
- this.add(new LED(x, y), c);
- }
- }
-
- // c.weightx = 0.8;
- // c.weighty = 0.8;
- for (int y = 0; y < bag.buttonHeight; y++) {
- for (int x = 0; x < bag.buttonWidth; x++) {
- c.gridx = (x * 2) + 1;
- c.gridy = (y * 2) + 1;
-
- this.add(new Button(x, y), c);
- }
- }
-
- // this.setMinimumSize(new Dimension(500,500));
- // this.setPreferredSize(new Dimension(500,500));
-
- // this.setBackground(Color.BLUE);
-
- frame.pack();
- frame.setSize(500, 500);
-
- this.setBorder(BorderFactory.createLineBorder(Color.black));
- }
-
- /*
- * public Dimension getPreferredSize() { if () return new
- * Dimension(super.,10); }
- */
-
- class LED extends JComponent implements LEDListener {
- final int x;
- final int y;
-
- public LED(int x, int y) {
- this.x = x;
- this.y = y;
- /*
- * this.setMinimumSize(new Dimension(50,50)); this.setSize(50,
- * 50); this.setPreferredSize(new Dimension(50,50));
- */
- bag.addLEDChangeListener(this);
-
- }
-
- public Dimension getPreferredSize() {
- return new Dimension(5, 5);
- }
-
- public void paintComponent(Graphics g) {
- if (bag.getLED(x, y) == PunchingBag.Colour.Red) {
- g.setColor(Color.red);
- } else if (bag.getLED(x, y) == PunchingBag.Colour.Green) {
- g.setColor(Color.green);
- } else if (bag.getLED(x, y) == PunchingBag.Colour.Yellow) {
- g.setColor(Color.yellow);
- } else {
- g.setColor(Color.white);
- }
-
- g.fillOval(0, 0, this.getWidth(), this.getHeight());
- }
-
- @Override
- public void LEDchanged() {
- this.repaint();
- }
-
- }
-
- class Button extends JComponent implements MouseListener {
- final int x;
- final int y;
-
- public Button(int x, int y) {
- this.x = x;
- this.y = y;
- setBorder(BorderFactory.createLineBorder(Color.black));
- this.addMouseListener(this);
- }
-
- public Dimension getPreferredSize() {
- return new Dimension(20, 20);
- }
-
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
-
- g.setColor(Color.black);
- g.fillRect(0, 0, this.getWidth(), this.getHeight());
- g.setColor(new Color(220, 220, 220));
- g.fillRect(4, 4, this.getWidth() - 8, this.getHeight() - 8);
-
- }
-
- public void mouseClicked(MouseEvent arg0) {
- }
-
- public void mouseEntered(MouseEvent arg0) {
- }
-
- public void mouseExited(MouseEvent arg0) {
- }
-
- public void mousePressed(MouseEvent arg0) {
- System.out.println("Button Pressed");
- bag.buttonPressed(x, y);
- }
-
- public void mouseReleased(MouseEvent arg0) {
- }
-
- }
-
- public void buttonPressed(int x, int y) {
- this.repaint();
- }
-
- public void LEDchanged() {
- this.repaint();
-
- }
-
- @Override
- public void mouseClicked(MouseEvent arg0) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void mouseEntered(MouseEvent arg0) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void mouseExited(MouseEvent arg0) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void mousePressed(MouseEvent me) {
- if (currentMode == Mode.Interactive) {
- // bag.buttonPressed(me.getX()/(buttonSize+ledSize),
- // me.getY()/(buttonSize+ledSize));
- }
- }
-
- @Override
- public void mouseReleased(MouseEvent arg0) {
- // TODO Auto-generated method stub
-
- }
- }
-
- 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
-
- }
-
- public void mouseEntered(MouseEvent arg0) {
- // TODO Auto-generated method stub
-
- }
-
- public void mouseExited(MouseEvent arg0) {
- // TODO Auto-generated method stub
-
- }
-
- public void mousePressed(MouseEvent arg0) {
- /*
- * if (currentMode == Mode.Interactive) { runInteractively(new
- * Contact(System.currentTimeMillis(), (arg0 .getX() - 4) / 40,
- * (arg0.getY() - 30) / 40, 0)); }
- */
- }
-
- public void mouseReleased(MouseEvent arg0) {
- // TODO Auto-generated method stub
-
- }
-
- public void mouseDragged(MouseEvent arg0) {
- // TODO Auto-generated method stub
-
- }
-
- public void mouseMoved(MouseEvent arg0) {
- // TODO Auto-generated method stub
-
- }
-
- public void contact(Contact c) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void buttonPressed(int x, int y) {
- System.out.println("Button Pressed: " + x + " " + y);
- // bag.circleExpand(x, y, 16);
- // bag.setLED(x, y, PunchingBag.Colour.Red);
- // bag.noise(new Rectangle(0, 0, 9, 20), 5000);
- /*
- * for (int bx=0; bx<bag.ledWidth; bx++) { for (int by=0;
- * by<bag.ledHeight; by++) { bag.setLED(bx, by, PunchingBag.Colour.Red);
- * } }
- */
- // bag.squareExpand(x,y, 16, PunchingBag.Colour.Red);
-
- // Ode to Joy
- // bag.rect(0, 0, 9, 4, PunchingBag.Colour.Red);
- // bag.rect(0, 4, 9, 4, PunchingBag.Colour.Red);
- // bag.rect(0, 8, 9, 4, PunchingBag.Colour.Red);
- // bag.rect(0, 12, 9, 4, PunchingBag.Colour.Red);
- // bag.rect(0, 16, 9, 4, PunchingBag.Colour.Red);
- if (y < 3) {
- bag.fillRect(0, 0, 9, 4, PunchingBag.Colour.Red, 500);
- playKey(5);
- } else if (y < 6) {
- bag.fillRect(0, 3, 9, 4, PunchingBag.Colour.Red, 500);
- playKey(4);
- } else if (y < 9) {
- bag.fillRect(0, 6, 9, 4, PunchingBag.Colour.Red, 500);
- playKey(3);
- } else if (y < 12) {
- bag.fillRect(0, 9, 9, 4, PunchingBag.Colour.Red, 500);
- playKey(2);
- } else if (y < 15) {
- bag.fillRect(0, 12, 9, 4, PunchingBag.Colour.Red, 500);
- playKey(1);
- } else {
- bag.fillRect(0, 15, 9, 4, PunchingBag.Colour.Red, 500);
- playKey(0);
- }
- }
-
- @Override
- public void LEDchanged() {
- /*
- * 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); } 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); } } } }
- */
-
- }
-
- static PunchingBag getPunchingBag() {
- return bag;
- }
-
-}
+package uk.ac.open.punchingbag; +import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.Component; +import java.awt.Dimension; +import java.awt.Graphics; +import java.awt.GridBagConstraints; +import java.awt.GridBagLayout; +import java.awt.Insets; +import java.awt.Rectangle; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +import java.awt.event.MouseMotionListener; +import java.io.File; +import java.io.IOException; + +import javax.swing.BorderFactory; +import javax.swing.JButton; +import javax.swing.JComboBox; +import javax.swing.JComponent; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JList; +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.JSplitPane; +import javax.swing.JTabbedPane; +import javax.swing.JTextArea; +import javax.swing.ListCellRenderer; + +import javax.sound.sampled.AudioFormat; +import javax.sound.sampled.AudioInputStream; +import javax.sound.sampled.AudioSystem; +import javax.sound.sampled.Clip; +import javax.sound.sampled.DataLine; +import javax.sound.sampled.Line; +import javax.sound.sampled.LineUnavailableException; +import javax.sound.sampled.Mixer; +import javax.sound.sampled.TargetDataLine; +import javax.sound.sampled.UnsupportedAudioFileException; +import javax.sound.sampled.AudioFormat.Encoding; +import javax.sound.sampled.Mixer.Info; + +public class PunchingBagGUI implements MouseListener, MouseMotionListener, + ButtonListener, LEDListener { + + public static enum Mode { + Menu, Interactive, Game + }; + + Mode currentMode = Mode.Interactive; + + static JFrame frame; + + // Contact + public enum Direction { + None, Up, Down, Left, Right + }; + + String soundDir = System.getProperty("user.dir") + + System.getProperty("file.separator"); + + File[] keys = { new File(soundDir + "G4.wav"), + new File(soundDir + "C5.wav"), new File(soundDir + "D5.wav"), + new File(soundDir + "E5.wav"), new File(soundDir + "F5.wav"), + new File(soundDir + "G5.wav") }; + + static PunchingBag bag = PunchingBag.getBag(); + + final boolean useDebugScreen = true; + + public static void main(String[] args) throws InterruptedException { + new PunchingBagGUI(); + + } + + PunchingBagGUI() throws InterruptedException { + + // System.out.println(AudioSystem.getMixerInfo()[0].getName()); + // System.out.println(AudioSystem.getMixerInfo()[0].getDescription()); + // Mixer mix = AudioSystem.getMixer(AudioSystem.getMixerInfo()[0]); + + bag.connectToArduinos(); + + frame = new JFrame("Punching Bag GUI"); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + // frame.setSize(600, 600); + frame.setVisible(true); + + frame.addMouseListener(this); + frame.addMouseMotionListener(this); + + bag.addButtonListener(this); + bag.addLEDChangeListener(this); + + JSplitPane bottomTopSplitPane = new JSplitPane( + JSplitPane.VERTICAL_SPLIT); + + JSplitPane centerTopSplitPane = new JSplitPane( + JSplitPane.HORIZONTAL_SPLIT); + + JTabbedPane tabbedPane = new JTabbedPane(); + tabbedPane.addTab("LED Control Panel", null, new LEDControlPanel(), + "Allows control of individual LED's"); + + centerTopSplitPane.setLeftComponent(new PunchingBagSim()); + centerTopSplitPane.setRightComponent(tabbedPane); + + bottomTopSplitPane.setTopComponent(centerTopSplitPane); + bottomTopSplitPane.setBottomComponent(new SerialPanel()); + + frame.add(bottomTopSplitPane, BorderLayout.CENTER); + + // frame.pack(); + } + + void playKey(int key) { + try { + AudioInputStream sound = AudioSystem.getAudioInputStream(keys[key]); + DataLine.Info dataLine = new DataLine.Info(Clip.class, + sound.getFormat()); + Clip clip = (Clip) AudioSystem.getLine(dataLine); + clip.open(sound); + clip.start(); + } catch (LineUnavailableException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (UnsupportedAudioFileException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + class SerialPanel extends JPanel implements ActionListener, + SerialReadListener, SerialWriteListener { + JTextArea buttonIn = new JTextArea(); + JTextArea buttonOut = new JTextArea(); + JTextArea ledIn = new JTextArea(); + JTextArea ledOut = new JTextArea(); + + public SerialPanel() { + JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT); + JSplitPane buttonSplitPane = new JSplitPane( + JSplitPane.HORIZONTAL_SPLIT); + JSplitPane ledSplitPane = new JSplitPane( + JSplitPane.HORIZONTAL_SPLIT); + /* + * splitPane.setOneTouchExpandable(true); + * buttonSplitPane.setOneTouchExpandable(true); + * ledSplitPane.setOneTouchExpandable(true); + */ + + splitPane.setMinimumSize(new Dimension(200, 50)); + buttonSplitPane.setMinimumSize(new Dimension(100, 50)); + ledSplitPane.setMinimumSize(new Dimension(100, 50)); + + JButton clear = new JButton("Clear"); + clear.addActionListener(this); + + JPanel buttonInPanel = new JPanel(); + JScrollPane areaScrollPane = new JScrollPane(buttonIn); + areaScrollPane + .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); + areaScrollPane.setPreferredSize(new Dimension(250, 100)); + buttonInPanel.add(areaScrollPane, BorderLayout.CENTER); + clear.setActionCommand("BI"); + buttonInPanel.add(clear, BorderLayout.SOUTH); + buttonInPanel.setBorder(BorderFactory + .createTitledBorder("Serial In - Button Arduino")); + buttonSplitPane.setLeftComponent(buttonInPanel); + + JPanel buttonOutPanel = new JPanel(); + areaScrollPane = new JScrollPane(buttonOut); + areaScrollPane + .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); + areaScrollPane.setPreferredSize(new Dimension(250, 100)); + buttonOutPanel.add(areaScrollPane, BorderLayout.CENTER); + clear = new JButton("Clear"); + clear.setActionCommand("BO"); + buttonOutPanel.add(clear, BorderLayout.SOUTH); + buttonOutPanel.setBorder(BorderFactory + .createTitledBorder("Serial Out - Button Arduino")); + buttonSplitPane.setRightComponent(buttonOutPanel); + + JPanel ledInPanel = new JPanel(); + areaScrollPane = new JScrollPane(ledIn); + areaScrollPane + .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); + areaScrollPane.setPreferredSize(new Dimension(250, 100)); + ledInPanel.add(areaScrollPane, BorderLayout.CENTER); + clear = new JButton("Clear"); + clear.setActionCommand("LI"); + ledInPanel.add(clear, BorderLayout.SOUTH); + ledInPanel.setBorder(BorderFactory + .createTitledBorder("Serial In - LED Arduino")); + ledSplitPane.setLeftComponent(ledInPanel); + + JPanel ledOutPanel = new JPanel(); + areaScrollPane = new JScrollPane(ledOut); + areaScrollPane + .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); + areaScrollPane.setPreferredSize(new Dimension(250, 100)); + ledOutPanel.add(areaScrollPane, BorderLayout.CENTER); + clear = new JButton("Clear"); + clear.setActionCommand("LO"); + ledOutPanel.add(clear, BorderLayout.SOUTH); + ledOutPanel.setBorder(BorderFactory + .createTitledBorder("Serial Out - LED Arduino")); + ledSplitPane.setRightComponent(ledOutPanel); + + splitPane.setLeftComponent(buttonSplitPane); + splitPane.setRightComponent(ledSplitPane); + + this.add(splitPane); + + bag.buttonArduino.addSerialReadListener(this); + bag.buttonArduino.addSerialWriteListener(this); + + } + + @Override + public void actionPerformed(ActionEvent arg0) { + // TODO Auto-generated method stub + + } + + @Override + public void serialWriteEvent(byte b) { + buttonOut.append(String.valueOf((char) b)); + } + + @Override + public void serialWriteEvent(byte[] b) { + buttonOut.append(String.valueOf(b)); + } + + @Override + public void serialReadEvent(int b) { + if (b != -1) + buttonIn.append(String.valueOf((char) b)); + } + } + + class LEDControlPanel extends JPanel implements ActionListener { + String[] colours = { " ", "R", "Y", "G" }; + + public LEDControlPanel() { + this.setLayout(new GridBagLayout()); + + GridBagConstraints c = new GridBagConstraints(); + c.fill = GridBagConstraints.BOTH; + c.anchor = GridBagConstraints.CENTER; + // c.weightx = 0.2; + // c.weighty = 0.2; + + for (int y = 0; y < bag.ledHeight; y++) { + for (int x = 0; x < bag.ledWidth; x++) { + c.gridx = x; + c.gridy = y; + + RYGComboBoxRenderer renderer = new RYGComboBoxRenderer(); + JComboBox rygCombo = new JComboBox(colours); + rygCombo.setSelectedIndex(0); + rygCombo.addActionListener(this); + rygCombo.setActionCommand(String.valueOf(x) + "," + + String.valueOf(y)); + // rygCombo.setRenderer(renderer); + + this.add(rygCombo, c); + } + } + + c.gridx = 0; + c.gridy = 20; + c.fill = GridBagConstraints.HORIZONTAL; + c.anchor = GridBagConstraints.CENTER; + c.gridwidth = 9; + JButton clear = new JButton("Clear"); + clear.setActionCommand("C"); + clear.addActionListener(this); + this.add(clear, c); + } + + public void actionPerformed(ActionEvent a) { + if (a.getActionCommand() == "C") { + bag.clearLEDs(); + } else { + PunchingBag.Colour colour = PunchingBag.Colour.None; + if (((JComboBox) a.getSource()).getSelectedIndex() == 1) { + colour = PunchingBag.Colour.Red; + } else if (((JComboBox) a.getSource()).getSelectedIndex() == 2) { + colour = PunchingBag.Colour.Yellow; + } else if (((JComboBox) a.getSource()).getSelectedIndex() == 3) { + colour = PunchingBag.Colour.Green; + } + System.out.println("Setting " + + a.getActionCommand().split(",")[0] + " " + + a.getActionCommand().split(",")[1]); + bag.setLED(Integer.valueOf(a.getActionCommand().split(",")[0]), + Integer.valueOf(a.getActionCommand().split(",")[1]), + colour); + } + } + + class RYGComboBoxRenderer extends JLabel implements ListCellRenderer { + public RYGComboBoxRenderer() { + super(); + } + + public Component getListCellRendererComponent(JList list, + Object value, int index, boolean isSelected, + boolean cellHasFocus) { + + // Get the selected index. (The index param isn't + // always valid, so just use the value.) + + if (isSelected) { + setBackground(list.getSelectionBackground()); + setForeground(list.getSelectionForeground()); + } else { + setBackground(list.getBackground()); + setForeground(list.getForeground()); + } + + if (index != -1) + setText(colours[index]); + + return this; + } + } + } + + class PunchingBagSim extends JPanel implements ButtonListener, LEDListener, + MouseListener { + int buttonMinSize = 10; + int buttonMaxSize = 50; + + int ledMinSize = 10; + int ledMaxSize = 50; + + int buttonSize = 40; + int ledSize = 8; + + public PunchingBagSim() { + /* + * this.setMinimumSize(new Dimension((bag.buttonWidth * + * buttonMinSize) + (bag.ledWidth * ledMinSize), (bag.buttonHeight * + * buttonMinSize) + (bag.ledHeight * ledMaxSize))); + * this.setMaximumSize(new Dimension((bag.buttonWidth * + * buttonMaxSize) + (bag.ledWidth * ledMaxSize), (bag.buttonHeight * + * buttonMaxSize) + (bag.ledHeight * ledMaxSize))); + */ + bag.addButtonListener(this); + bag.addLEDChangeListener(this); + + this.addMouseListener(this); + + GridBagLayout gbl = new GridBagLayout(); + this.setLayout(gbl); + GridBagConstraints c = new GridBagConstraints(); + + // c.insets = new Insets(1, 1, 1, 1); + c.fill = GridBagConstraints.BOTH; + c.anchor = GridBagConstraints.CENTER; + // c.weightx = 0.2; + // c.weighty = 0.2; + for (int y = 0; y < bag.ledHeight; y++) { + for (int x = 0; x < bag.ledWidth; x++) { + c.gridx = x * 2; + c.gridy = y * 2; + + this.add(new LED(x, y), c); + } + } + + // c.weightx = 0.8; + // c.weighty = 0.8; + for (int y = 0; y < bag.buttonHeight; y++) { + for (int x = 0; x < bag.buttonWidth; x++) { + c.gridx = (x * 2) + 1; + c.gridy = (y * 2) + 1; + + this.add(new Button(x, y), c); + } + } + + // this.setMinimumSize(new Dimension(500,500)); + // this.setPreferredSize(new Dimension(500,500)); + + // this.setBackground(Color.BLUE); + + frame.pack(); + frame.setSize(500, 500); + + this.setBorder(BorderFactory.createLineBorder(Color.black)); + } + + /* + * public Dimension getPreferredSize() { if () return new + * Dimension(super.,10); } + */ + + class LED extends JComponent implements LEDListener { + final int x; + final int y; + + public LED(int x, int y) { + this.x = x; + this.y = y; + /* + * this.setMinimumSize(new Dimension(50,50)); this.setSize(50, + * 50); this.setPreferredSize(new Dimension(50,50)); + */ + bag.addLEDChangeListener(this); + + } + + public Dimension getPreferredSize() { + return new Dimension(5, 5); + } + + public void paintComponent(Graphics g) { + if (bag.getLED(x, y) == PunchingBag.Colour.Red) { + g.setColor(Color.red); + } else if (bag.getLED(x, y) == PunchingBag.Colour.Green) { + g.setColor(Color.green); + } else if (bag.getLED(x, y) == PunchingBag.Colour.Yellow) { + g.setColor(Color.yellow); + } else { + g.setColor(Color.white); + } + + g.fillOval(0, 0, this.getWidth(), this.getHeight()); + } + + @Override + public void LEDchanged() { + this.repaint(); + } + + } + + class Button extends JComponent implements MouseListener { + final int x; + final int y; + + public Button(int x, int y) { + this.x = x; + this.y = y; + setBorder(BorderFactory.createLineBorder(Color.black)); + this.addMouseListener(this); + } + + public Dimension getPreferredSize() { + return new Dimension(20, 20); + } + + public void paintComponent(Graphics g) { + super.paintComponent(g); + + g.setColor(Color.black); + g.fillRect(0, 0, this.getWidth(), this.getHeight()); + g.setColor(new Color(220, 220, 220)); + g.fillRect(4, 4, this.getWidth() - 8, this.getHeight() - 8); + + } + + public void mouseClicked(MouseEvent arg0) { + } + + public void mouseEntered(MouseEvent arg0) { + } + + public void mouseExited(MouseEvent arg0) { + } + + public void mousePressed(MouseEvent arg0) { + System.out.println("Button Pressed"); + bag.buttonPressed(x, y); + } + + public void mouseReleased(MouseEvent arg0) { + } + + } + + public void buttonPressed(int x, int y) { + this.repaint(); + } + + public void LEDchanged() { + this.repaint(); + + } + + @Override + public void mouseClicked(MouseEvent arg0) { + // TODO Auto-generated method stub + + } + + @Override + public void mouseEntered(MouseEvent arg0) { + // TODO Auto-generated method stub + + } + + @Override + public void mouseExited(MouseEvent arg0) { + // TODO Auto-generated method stub + + } + + @Override + public void mousePressed(MouseEvent me) { + if (currentMode == Mode.Interactive) { + // bag.buttonPressed(me.getX()/(buttonSize+ledSize), + // me.getY()/(buttonSize+ledSize)); + } + } + + @Override + public void mouseReleased(MouseEvent arg0) { + // TODO Auto-generated method stub + + } + + @Override + public void contact(Contact c) { + // TODO Auto-generated method stub + + } + } + + 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 + + } + + public void mouseEntered(MouseEvent arg0) { + // TODO Auto-generated method stub + + } + + public void mouseExited(MouseEvent arg0) { + // TODO Auto-generated method stub + + } + + public void mousePressed(MouseEvent arg0) { + /* + * if (currentMode == Mode.Interactive) { runInteractively(new + * Contact(System.currentTimeMillis(), (arg0 .getX() - 4) / 40, + * (arg0.getY() - 30) / 40, 0)); } + */ + } + + public void mouseReleased(MouseEvent arg0) { + // TODO Auto-generated method stub + + } + + public void mouseDragged(MouseEvent arg0) { + // TODO Auto-generated method stub + + } + + public void mouseMoved(MouseEvent arg0) { + // TODO Auto-generated method stub + + } + + public void contact(Contact c) { + // TODO Auto-generated method stub + + } + + @Override + public void buttonPressed(int x, int y) { + System.out.println("Button Pressed: " + x + " " + y); + // bag.circleExpand(x, y, 16); + // bag.setLED(x, y, PunchingBag.Colour.Red); + // bag.noise(new Rectangle(0, 0, 9, 20), 5000); + /* + * for (int bx=0; bx<bag.ledWidth; bx++) { for (int by=0; + * by<bag.ledHeight; by++) { bag.setLED(bx, by, PunchingBag.Colour.Red); + * } } + */ + // bag.squareExpand(x,y, 16, PunchingBag.Colour.Red); + + // Ode to Joy + // bag.rect(0, 0, 9, 4, PunchingBag.Colour.Red); + // bag.rect(0, 4, 9, 4, PunchingBag.Colour.Red); + // bag.rect(0, 8, 9, 4, PunchingBag.Colour.Red); + // bag.rect(0, 12, 9, 4, PunchingBag.Colour.Red); + // bag.rect(0, 16, 9, 4, PunchingBag.Colour.Red); + if (y < 3) { + bag.fillRect(0, 0, 9, 4, PunchingBag.Colour.Red, 500); + playKey(5); + } else if (y < 6) { + bag.fillRect(0, 3, 9, 4, PunchingBag.Colour.Red, 500); + playKey(4); + } else if (y < 9) { + bag.fillRect(0, 6, 9, 4, PunchingBag.Colour.Red, 500); + playKey(3); + } else if (y < 12) { + bag.fillRect(0, 9, 9, 4, PunchingBag.Colour.Red, 500); + playKey(2); + } else if (y < 15) { + bag.fillRect(0, 12, 9, 4, PunchingBag.Colour.Red, 500); + playKey(1); + } else { + bag.fillRect(0, 15, 9, 4, PunchingBag.Colour.Red, 500); + playKey(0); + } + } + + @Override + public void LEDchanged() { + /* + * 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); } 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); } } } } + */ + + } + + static PunchingBag getPunchingBag() { + return bag; + } + +} diff --git a/PunchingBag/src/SerialReadListener.java b/PunchingBag/src/uk/ac/open/punchingbag/SerialReadListener.java index d8e20ed..1e3f34c 100644 --- a/PunchingBag/src/SerialReadListener.java +++ b/PunchingBag/src/uk/ac/open/punchingbag/SerialReadListener.java @@ -1,3 +1,4 @@ +package uk.ac.open.punchingbag; import java.util.EventListener; interface SerialReadListener extends EventListener { diff --git a/PunchingBag/src/SerialWriteListener.java b/PunchingBag/src/uk/ac/open/punchingbag/SerialWriteListener.java index 01a8db4..85ff1ea 100644 --- a/PunchingBag/src/SerialWriteListener.java +++ b/PunchingBag/src/uk/ac/open/punchingbag/SerialWriteListener.java @@ -1,3 +1,4 @@ +package uk.ac.open.punchingbag; import java.util.EventListener; interface SerialWriteListener extends EventListener { diff --git a/PunchingBag/src/uk/ac/open/punchingbag/examples/Giggle.java b/PunchingBag/src/uk/ac/open/punchingbag/examples/Giggle.java new file mode 100644 index 0000000..89c7f62 --- /dev/null +++ b/PunchingBag/src/uk/ac/open/punchingbag/examples/Giggle.java @@ -0,0 +1,74 @@ +package uk.ac.open.punchingbag.examples; + +import java.io.File; +import java.io.IOException; + +import javax.sound.sampled.AudioInputStream; +import javax.sound.sampled.AudioSystem; +import javax.sound.sampled.Clip; +import javax.sound.sampled.DataLine; +import javax.sound.sampled.LineUnavailableException; +import javax.sound.sampled.UnsupportedAudioFileException; + +import uk.ac.open.punchingbag.ButtonListener; +import uk.ac.open.punchingbag.Contact; +import uk.ac.open.punchingbag.PunchingBag; + +public class Giggle implements ButtonListener { + + PunchingBag bag = PunchingBag.getBag(); + + String soundDir = System.getProperty("user.dir") + + System.getProperty("file.separator"); + + File[] laughs = { new File(soundDir + "G4.wav"), + new File(soundDir + "C5.wav"), new File(soundDir + "D5.wav"), + new File(soundDir + "E5.wav"), new File(soundDir + "F5.wav"), + new File(soundDir + "G5.wav") }; + + public static void main(String[] args) { + new Giggle(); + + } + + Giggle() { + bag.addButtonListener(this); + bag.connectToArduinos(); + } + + @Override + public void buttonPressed(int x, int y) { + // TODO Auto-generated method stub + + } + + @Override + public void contact(Contact c) { + System.out.println(c); + bag.circleExpand(c.x, c.y, 16); + if (c.force > 520) { + //int laugh = c.force + } + } + + void laugh(int laugh) { + try { + AudioInputStream sound = AudioSystem.getAudioInputStream(laughs[laugh]); + DataLine.Info dataLine = new DataLine.Info(Clip.class, + sound.getFormat()); + Clip clip = (Clip) AudioSystem.getLine(dataLine); + clip.open(sound); + clip.start(); + } catch (LineUnavailableException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (UnsupportedAudioFileException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + +} diff --git a/PunchingBag/src/uk/ac/open/punchingbag/examples/SimpleKeyboard.java b/PunchingBag/src/uk/ac/open/punchingbag/examples/SimpleKeyboard.java new file mode 100644 index 0000000..188b262 --- /dev/null +++ b/PunchingBag/src/uk/ac/open/punchingbag/examples/SimpleKeyboard.java @@ -0,0 +1,87 @@ +package uk.ac.open.punchingbag.examples; + +import java.io.File; +import java.io.IOException; + +import javax.sound.sampled.AudioInputStream; +import javax.sound.sampled.AudioSystem; +import javax.sound.sampled.Clip; +import javax.sound.sampled.DataLine; +import javax.sound.sampled.LineUnavailableException; +import javax.sound.sampled.UnsupportedAudioFileException; + +import uk.ac.open.punchingbag.ButtonListener; +import uk.ac.open.punchingbag.Contact; +import uk.ac.open.punchingbag.PunchingBag; + +public class SimpleKeyboard implements ButtonListener { + + String soundDir = System.getProperty("user.dir") + + System.getProperty("file.separator"); + + File[] keys = { new File(soundDir + "G4.wav"), + new File(soundDir + "C5.wav"), new File(soundDir + "D5.wav"), + new File(soundDir + "E5.wav"), new File(soundDir + "F5.wav"), + new File(soundDir + "G5.wav") }; + + static PunchingBag bag = PunchingBag.getBag(); + + public static void main(String[] args) { + new SimpleKeyboard(); + } + + SimpleKeyboard() { + bag.addButtonListener(this); + bag.connectToArduinos(); + } + + public void buttonPressed(int x, int y) { + if (y < 3) { + bag.fillRect(0, 0, 9, 4, PunchingBag.Colour.Red, 500); + playKey(5); + } else if (y < 6) { + bag.fillRect(0, 3, 9, 4, PunchingBag.Colour.Red, 500); + playKey(4); + } else if (y < 9) { + bag.fillRect(0, 6, 9, 4, PunchingBag.Colour.Red, 500); + playKey(3); + } else if (y < 12) { + bag.fillRect(0, 9, 9, 4, PunchingBag.Colour.Red, 500); + playKey(2); + } else if (y < 15) { + bag.fillRect(0, 12, 9, 4, PunchingBag.Colour.Red, 500); + playKey(1); + } else { + bag.fillRect(0, 15, 9, 4, PunchingBag.Colour.Red, 500); + playKey(0); + } + } + + void playKey(int key) { + try { + AudioInputStream sound = AudioSystem.getAudioInputStream(keys[key]); + DataLine.Info dataLine = new DataLine.Info(Clip.class, + sound.getFormat()); + Clip clip = (Clip) AudioSystem.getLine(dataLine); + clip.open(sound); + clip.start(); + } catch (LineUnavailableException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (UnsupportedAudioFileException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + @Override + public void contact(Contact c) { + // TODO Auto-generated method stub + + } + + +} |