import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SelectSound
    extends JApplet
{
    
    AudioClip clip;
    JLabel title;

    public void init() {
	Image playImage = getImage(getCodeBase(), "play.gif");
	Image loopImage = getImage(getCodeBase(), "loop.gif");
	Image stopImage = getImage(getCodeBase(), "stop.gif");
	ImageIcon playIcon = new ImageIcon(playImage);
	ImageIcon loopIcon = new ImageIcon(loopImage);
	ImageIcon stopIcon = new ImageIcon(stopImage);
	title = new JLabel("", JLabel.CENTER);
	setTitle(null);
	JButton playButton = new JButton("Play", playIcon);
 	JButton loopButton = new JButton("Loop", loopIcon);
	JButton stopButton = new JButton("Stop", stopIcon);
	playButton.setVerticalTextPosition(SwingConstants.BOTTOM);
	playButton.setHorizontalTextPosition(SwingConstants.CENTER);
	loopButton.setVerticalTextPosition(SwingConstants.BOTTOM);
	loopButton.setHorizontalTextPosition(SwingConstants.CENTER);
	stopButton.setVerticalTextPosition(SwingConstants.BOTTOM);
	stopButton.setHorizontalTextPosition(SwingConstants.CENTER);
   	JPanel buttonPanel = new JPanel();
	buttonPanel.setLayout(new GridLayout(1, 3));
	buttonPanel.add(playButton);
	buttonPanel.add(loopButton);
	buttonPanel.add(stopButton);
	playButton.addActionListener(new ActionListener()
	{
	    public void actionPerformed(ActionEvent ae)
	    {
		// Make sure that clip is valid
		if (clip != null)
		    clip.play();
	    }
	});
	loopButton.addActionListener(new ActionListener()
	{
	    public void actionPerformed(ActionEvent ae)
	    {
		// Make sure that clip is valid
		if (clip != null)
		    clip.loop();
	    }
	});
	stopButton.addActionListener(new ActionListener()
	{
	    public void actionPerformed(ActionEvent ae)
	    {
		// Make sure that clip is valid
		if (clip != null)
		    clip.stop();
	    }
	});
	JPanel p = new JPanel();
	p.setLayout(new BorderLayout());
	p.add(new JLabel("Clip file:", JLabel.RIGHT), BorderLayout.WEST);
	JTextField soundFile = new JTextField();
	soundFile.addActionListener(new ActionListener()
	{
	    public void actionPerformed(ActionEvent ae)
	    {
		// Get "source" of event, which is the soundFile object.
		// Don't forget that we have to "cast" the value;
		JTextField tf = (JTextField) ae.getSource();
		// Get the URL that the user typed in.
		String url = tf.getText();
		// Set the clip to the given URL.
		setClip(url);
		// Make sure that url is valid
		setTitle(url);	
	    }
	});
	p.add(soundFile, BorderLayout.CENTER);
   	Container c = getContentPane();
	c.setLayout(new BorderLayout());
	c.add(title, BorderLayout.NORTH);
	c.add(buttonPanel, BorderLayout.CENTER);
	c.add(p, BorderLayout.SOUTH);
	setSize(400, 150);
    }

    private void setTitle(String t)
    {
	if (t != null && t.length() > 0)
	    title.setText("Clip: " + t);
	else
	    title.setText("No clip loaded");
     }
    
    private void setClip(String url)
    {
        clip = getAudioClip(getCodeBase(), url);
	// Play the clip once to know that it's loaded.
	if (clip != null)
	    clip.play();
    }    

}
