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

public class Phone
    extends JApplet
{
    String labels[] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "*", "0", "#"};
    AudioClip clips[];
    JButton buttons[];

    public void init()
    {
	// Load the audio Clips.
	loadClips();
	Container pane = getContentPane();
	pane.setLayout(new GridLayout(4, 3, 5, 5));
	for (int i=0; i<labels.length; i++)
	{
	    // Create new button using label array;
	    JButton b = new JButton(labels[i]);
	    // Add button to the applet.
	    pane.add(b);
	    // Link button to the play audio clip.
	    final int clipIndex = i;
	    b.addActionListener(new ActionListener()
	    {
		public void actionPerformed(ActionEvent ae)
		{
		    // clipIndex must be declared as "final" to be used
		    // in this inner class.
		    clips[clipIndex].play();
		}
	    });
	}
	//resize(150, 100);
    }
    
    private void loadClips()
    {
	// Create an array of audio clips.
	// This array needs to have the name
	// number of elements as the labels array.
	clips = new AudioClip[labels.length];
	for (int i=0; i<labels.length; i++)
	{
	    // The sound clips have names like touchtone.2.au
	    // for the "2" key.  The * and # keys have "pound"
	    // and "star" in the filename.
	    //
	    // A StringBuffer is a class that can be used
	    // to create a String.  The append() method will
	    // append anything to the buffer in String form.
	    StringBuffer sb = new StringBuffer();
	    sb.append("touchtone.");
	    String key = labels[i];
	    // Check for the two special cases.
	    if (key.equals("*"))
		sb.append("star");
	    else if (key.equals("#"))
		sb.append("pound");
	    else
		sb.append(key);
	    sb.append(".au");
	    // Convert StringBuffer to String.
	    String filename = sb.toString();
	    // Set clips array.
	    clips[i] = getAudioClip(getCodeBase(), filename);
	}
    }
}

