import java.applet.*; import java.awt.*; import java.awt.event.*; import java.net.*; import javax.swing.*; public class LaughTrack extends JFrame { public LaughTrack() { super("Laughtrack"); setSize(190, 80); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container content = getContentPane(); FlowLayout flo = new FlowLayout(); content.setLayout(flo); LaughButton haha = new LaughButton(); content.add(haha); setContentPane(content); setVisible(true); } public static void main(String[] arguments) { LaughTrack lt = new LaughTrack(); } } class LaughButton extends JButton implements Runnable, ActionListener { AudioClip[] laugh = new AudioClip[4]; Thread runner; LaughButton() { super("Start Laughing"); addActionListener(this); for (int i = 0; i < laugh.length; i++) { try { URL laughIn = new URL("file:laugh" + i + ".wav"); laugh[i] = JApplet.newAudioClip(laughIn); } catch (MalformedURLException e) { } } } public void actionPerformed(ActionEvent event) { String command = event.getActionCommand(); if (command == "Start Laughing") startLaughing(); if (command == "Stop Laughing") stopLaughing(); } void startLaughing() { if (runner == null) { runner = new Thread(this); runner.start(); setText("Stop Laughing"); } } void stopLaughing() { if (runner != null) { for (int i = 0; i < laugh.length; i++) if (laugh[i] != null) laugh[i].stop(); runner = null; setText("Start Laughing"); } } public void run() { for (int i = 0; i < laugh.length; i++) if (laugh[i] != null) laugh[i].loop(); Thread thisThread = Thread.currentThread(); while (runner == thisThread) { try { Thread.sleep(5000); } catch (InterruptedException e) { } } } }