aboutsummaryrefslogtreecommitdiff
path: root/PunchingBag/src/uk/ac/open/punchingbag/examples/Giggle.java
diff options
context:
space:
mode:
Diffstat (limited to 'PunchingBag/src/uk/ac/open/punchingbag/examples/Giggle.java')
-rw-r--r--PunchingBag/src/uk/ac/open/punchingbag/examples/Giggle.java42
1 files changed, 26 insertions, 16 deletions
diff --git a/PunchingBag/src/uk/ac/open/punchingbag/examples/Giggle.java b/PunchingBag/src/uk/ac/open/punchingbag/examples/Giggle.java
index 89c7f62..6a69503 100644
--- a/PunchingBag/src/uk/ac/open/punchingbag/examples/Giggle.java
+++ b/PunchingBag/src/uk/ac/open/punchingbag/examples/Giggle.java
@@ -17,20 +17,20 @@ 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") };
+
+ Clip lastClip;
+ long lastContact = 0;
+
+ String soundDir = System.getProperty("user.dir") + System.getProperty("file.separator");
+
+ File[] laughs = { new File(soundDir + "Laugh1.wav"), new File(soundDir + "Laugh2.wav"), new File(soundDir + "Laugh3.wav"),
+ new File(soundDir + "Laugh4.wav"), new File(soundDir + "Laugh5.wav"), new File(soundDir + "Laugh6.wav"), new File(soundDir + "Laugh6.wav") };
public static void main(String[] args) {
new Giggle();
}
-
+
Giggle() {
bag.addButtonListener(this);
bag.connectToArduinos();
@@ -38,27 +38,37 @@ public class Giggle implements ButtonListener {
@Override
public void buttonPressed(int x, int y) {
- // TODO Auto-generated method stub
-
+ // laugh((int) (Math.random()*6));
+
}
@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
+ if ( System.currentTimeMillis() > (lastContact + 250)|| lastContact == 0) {
+ lastContact = System.currentTimeMillis();
+ if (c.force > 516) {
+ laugh(0);
+ } else if (c.force > 500) {
+ laugh(5);
+ } else if (c.force < 500) {
+ laugh(3);
+ }
}
}
-
+
void laugh(int laugh) {
try {
AudioInputStream sound = AudioSystem.getAudioInputStream(laughs[laugh]);
- DataLine.Info dataLine = new DataLine.Info(Clip.class,
- sound.getFormat());
+ DataLine.Info dataLine = new DataLine.Info(Clip.class, sound.getFormat());
+ if (lastClip != null) {
+ lastClip.stop();
+ }
Clip clip = (Clip) AudioSystem.getLine(dataLine);
clip.open(sound);
clip.start();
+ lastClip = clip;
} catch (LineUnavailableException e) {
// TODO Auto-generated catch block
e.printStackTrace();