//  File:  AudioItem\AudioItemAdapt.java

//  This version uses an inner class and extends MouseAdapter
//    This is the "preferred approach"

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.URL;

public class AudioItemAdapt extends Applet {

    /**
     * Play the next sound. The sound URLS are obtained
     * from the "snd" attribute. You can specify a list
     * of them by separating the sounds by '|'s. <p>
     * Note that the URL is constructed relative to the
     * getDocumentBase(), that is because the url is obtained
     * from within the document
     */
    public void next() {
        try {
            if (audio != null) {
                audio.stop();
                audio = null;
            }
        
            url = sounds;
            if (sounds.indexOf('|') >= 0) {
                int start = index;
                if ((index = sounds.indexOf('|', index)) < 0) {
                    url = sounds.substring(start);
                    index = 0;
                } else {
                    url = sounds.substring(start, index++);
                }
            }
            if (url.length() > 0) {
                // if you want to force users to play ONLY with a 
                // Java2-enabled browser, then change getAudioClip
                // to newAudioClip in the next line of code
                audio = getAudioClip(new URL(getDocumentBase(), url));
                audio.play();
                repaint();
                ++soundNum;
            }
        } catch (Exception e) {
            // ignore any exception...
        }
    }

    /**
    *     Initialize the applet; acquire the "sound" parameter value
    */
    public void init() {
        addMouseListener(new MyMouseListener());
        setSize(400, 250);
        sounds = getParameter("snd");
        if (sounds == null) {
            sounds = "Audio/beep.au";
        }
    }

    /**
    *     Start playing sounds; go to the next one
    */
    public void start() {
        next();
    }

    /**
    *     Stop playing sounds
    */
    public void stop() {
        if (audio != null) {
            audio.stop();
            audio = null;
        }
    }

    /**
    *     Paint the screen; draw the triangle
    */
    public void paint(Graphics g) {
        double f = ((double)(getSize().height - 1)) / ((getSize().width - 1) * 2);
        int offset = getSize().height / 2;
        for (int i = getSize().width - 1; i >= 0; i -= 3) {
            int h = (int)(i * f);
            g.drawLine(i, offset - h, i, offset + h);
        }
        g.drawString("Click anywhere in the Applet to play the next sound", 10, 20);
        g.drawString("Sound #" + soundNum + "   " + url.trim(), 10, 40);
        if (index <= 0)
            soundNum = 0;
    }
    
    /**
    *     Handle mouseReleased -- i.e., consume the event and play the next sound
    */
    private class MyMouseListener extends MouseAdapter {
        public void mouseReleased(MouseEvent e) {
            e.consume();
            next();     // When the user clicks in the applet,
        }               // play the next sound
    }

    private String sounds, url;
    private int index, soundNum;
    private AudioClip audio;
}
