aboutsummaryrefslogtreecommitdiff
path: root/PunchingBag/src/PunchingBagGUI.java
diff options
context:
space:
mode:
Diffstat (limited to 'PunchingBag/src/PunchingBagGUI.java')
-rw-r--r--PunchingBag/src/PunchingBagGUI.java95
1 files changed, 85 insertions, 10 deletions
diff --git a/PunchingBag/src/PunchingBagGUI.java b/PunchingBag/src/PunchingBagGUI.java
index 03e8f97..ef34459 100644
--- a/PunchingBag/src/PunchingBagGUI.java
+++ b/PunchingBag/src/PunchingBagGUI.java
@@ -12,6 +12,8 @@ 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;
@@ -27,6 +29,19 @@ 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 {
@@ -43,6 +58,13 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener,
None, Up, Down, Left, Right
};
+ String soundDir = System.getProperty("user.dir")
+ + System.getProperty("file.separator");
+
+ File[] keys = { 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;
@@ -53,6 +75,13 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener,
}
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");
@@ -87,6 +116,26 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener,
// 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 {
@@ -533,16 +582,42 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener,
@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);
+ 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(0);
+ } else if (y < 6) {
+ bag.fillRect(0, 3, 9, 4, PunchingBag.Colour.Red, 500);
+ playKey(1);
+ } else if (y < 9) {
+ bag.fillRect(0, 6, 9, 4, PunchingBag.Colour.Red, 500);
+ playKey(2);
+ } else if (y < 12) {
+ bag.fillRect(0, 9, 9, 4, PunchingBag.Colour.Red, 500);
+ playKey(3);
+ } else if (y < 15) {
+ bag.fillRect(0, 12, 9, 4, PunchingBag.Colour.Red, 500);
+ playKey(4);
+ } else {
+ bag.fillRect(0, 15, 9, 4, PunchingBag.Colour.Red, 500);
+ playKey(4);
+ }
}
@Override