diff options
Diffstat (limited to 'PunchingBag/src/uk/ac/open/punchingbag/examples/SimpleKeyboard.java')
-rw-r--r-- | PunchingBag/src/uk/ac/open/punchingbag/examples/SimpleKeyboard.java | 87 |
1 files changed, 87 insertions, 0 deletions
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 + + } + + +} |