From 132db792e5e0f636318a9dacb8efa783c1ade085 Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Tue, 13 Sep 2011 16:06:35 +0100 Subject: Improved threading, sound and button processing. --- PunchingBag/src/PunchingBagGUI.java | 95 +++++++++++++++++++++++++++++++++---- 1 file changed, 85 insertions(+), 10 deletions(-) (limited to 'PunchingBag/src/PunchingBagGUI.java') 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