import java.awt.*;
import java.applet.*;

// The ImageWithSound class reads in a ".gif" image and a ".au" audio
// file.  It displays the image and plays the sound.  The MediaTracker
// class is used to wait for an image to be completely loaded before
// displaying it or playing the sound.

public class ImageWithSound extends Applet{

    public void init() {

	// Read in an Image and an AudioClip.

	String imageName = IMAGE;
	String audioName = AUDIO;

	String param = getParameter("image");

	if (param != null) {
	    imageName = param;
	}

	param = getParameter("audio");

	if (param != null) {
	    audioName = param;
	}

	// Create a MediaTracker to inform us when the image has
	// been completely loaded.

	tracker = new MediaTracker(this);

	// getCodeBase() returns the URL of the applet's directory.
	// These calls will read in image and sound files relative to
	// the applet's directory.

	sound = getAudioClip(getCodeBase(), audioName);

	// getImage() returns immediately.  The image is not
	// actually loaded until it is first used.  We use a
	// MediaTracker to make sure the image is loaded
	// before we try to display it.

	image = getImage(getCodeBase(), imageName);

	// Add the image to the MediaTracker so that we can wait
	// for it.

	tracker.addImage(image, 0);
    }

    // Display the image.  The "this" argument to drawImage() is there
    // because drawImage() expects an "ImageObserver".  An image may
    // not be complete when drawImage() returns.  If so, the
    // ImageObserver argument is notified later.  The ImageObserver
    // is notified via its "imageUpdate" method.  Applets that do
    // elaborate image processing can override imageUpdate() to
    // get information about the state of images.  

    public void paint(Graphics g) {
	g.drawImage(image, 0, 0, this);
    }

    // Play the audio clip.

    public void start() {

	// Load the image and wait until it's done.

	try {
	    tracker.waitForID(0);
	} catch (InterruptedException e) {
	}

	repaint();
	sound.play();
    }
    
    // Default values for the image and sound filenames.

    private static final String IMAGE = "phead.gif";
    private static final String AUDIO = "drip.au";

    // Private state variables.

    private Image        image;
    private AudioClip    sound;
    private MediaTracker tracker;
}











