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

        // Display an image and play a sound clip whenever the
        // mouse enters the applet's area.
        // The image and sound are indicated by the parameters
        // "image" and "sound", respectively.
        // Both are retrieved relative to the document base.
public class AudioApplet extends Applet {
        // Set up the image and a MouseListener.
    public void init() throws RuntimeException {
        // Check that the parameters have been provided.
        for(int i = 0; i < parameterInfo.length; i++){
            checkParameter(parameterInfo[i][0]);
        }
        // Get the image.
        showStatus("Loading the image.");
        setImage(retrieveImage(getParameter(parameterInfo[imageParameter][0])));
        showStatus("");
        // Get the sound clip.
        showStatus("Loading the audio clip.");
        final AudioClip clip = getAudioClip(getDocumentBase(),
                             getParameter(parameterInfo[soundParameter][0]));
        addMouseListener(new MouseAdapter(){
            public void mouseEntered(MouseEvent e){
                // Play the audio clip.
                clip.play();
            }
        });
        showStatus("");
    }

        // Scale the image to the full size of the applet.
    public void paint(Graphics g){
        final Dimension size = getSize();
        Image image = getImage();
        if(image != null){
            g.drawImage(image,0,0,size.width,size.height,this);
        }
    }

        // Supply information on the parameters used.
    public String[][] getParameterInfo(){
        return parameterInfo;
    }
        
        // Get the image, which is located in fileName relative
        // to the document which loaded this applet.
    protected Image retrieveImage(String fileName) throws RuntimeException {
        Image appleImage = getImage(getDocumentBase(),fileName);
        // Wait for it to load.
        MediaTracker tracker = new MediaTracker(this);
        tracker.addImage(appleImage,0);
        try{
            tracker.waitForAll();
        }
        catch(InterruptedException e){
        }
        // Make sure loading was successful.
        if(tracker.isErrorAny()){
            throw new RuntimeException("Image loading problem: "+fileName);
        }
        else{
            return appleImage;
        }
    }

        // Retrieve the given parameter from the applet's properties.
    protected String checkParameter(String parameter) throws RuntimeException {
        String value = getParameter(parameter);
        if(value == null){
            throw new RuntimeException("Parameter: "+parameter+" is missing.");
        }
        return value;
    }

    protected Image getImage(){
        return image;
    }

    protected void setImage(Image i){
        image = i;
    }

    // The image to be displayed in the full size of the applet.
    private Image image;
    // Information on the parameters of this applet.
    private final int imageParameter = 0, soundParameter = 1;
    private final String[][] parameterInfo = {
        {"image", "image file",
         "Name of the image to be displayed, relative to the document base."
        },
        {"sound", "audio file",
         "Name of the audio file to be played, relative to the document base."
        },
    };
}
