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

public class DogButton
    extends JButton
{
    
    AudioClip clip;
    URL codeBase;
    
    public DogButton(URL url)
    {
	super("Dog Button");
	// Set the code base.
	codeBase = url;
	// Now load al the images using the static method in the Applet class.
        clip = Applet.newAudioClip(appendURL("bark.au"));
	setIcon(new ImageIcon(appendURL("dog.gif")));
	setRolloverIcon(new ImageIcon(appendURL("dog-roll.gif")));
	setPressedIcon(new ImageIcon(appendURL("dog-press.gif")));
	setVerticalTextPosition(SwingConstants.BOTTOM);
	setHorizontalTextPosition(SwingConstants.CENTER);
	addActionListener(new ActionListener()
	{
	    public void actionPerformed(ActionEvent ae)
	    {
		clip.play();
	    }
	});
    }
    
    /**
     * Append the file to the codeBase URL.
     *
     * @param file the file to append.
     * @return a URL that is codeBase + file.
     */
    private URL appendURL(String file)
    {
	// This "try" and "catch" stuff is covered in Chapter 11.
	// This is a special error handing situation.
	try
	{
	    return new URL(codeBase, file);
	}
	catch (Exception ex)
	{
	    System.out.println(ex);
	    return null;
	}
    }

}
