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

        // Retrieve an image file from the same location as the
        // originating HTML file (the Document Base).
public class ImageApplet extends Applet {
    public void init() throws RuntimeException {
        // Get the image named in the parameters.
        String imageName = checkParameter(parameterInfo[imageParameter][0]);
        setImage(retrieveImage(imageName));
    }

        // 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,getBackground(),this);
        }
    }

        // Supply information on the applet's purpose.
    public String getAppletInfo(){
        return "Display an image named in the "+
                parameterInfo[imageParameter][0]+" parameter.";
    }

        // 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;
    private final String[][] parameterInfo = {
        {"image", "image file",
         "Name of the image to be displayed, relative to the document base."
        },
    };
}
