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

        // Retrieve an image file from a Jar archive associated
        // with the applet.
public class JarImageApplet extends Applet {
    public void init() throws RuntimeException {
        // Get the image name.
        String imageName = checkParameter(parameterInfo[imageParameter][0]);
        setImage(extractImage(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,this);
        }
    }

        // Supply information on the parameters used.
    public String[][] getParameterInfo(){
        return parameterInfo;
    }
        
        // Retrieve fileName from this applet's archive file.
    protected Image extractImage(String fileName) throws RuntimeException {
        try {
            // Identify the resource via the applet's ClassLoader.
            InputStream imageStream = getClass().getResourceAsStream(fileName);
            // Read the whole Image as a single byte array.
            byte imageBytes[] = new byte[imageStream.available()];
            imageStream.read(imageBytes);
            // Turn the data into an Image.
            Toolkit tk = Toolkit.getDefaultToolkit();
            return tk.createImage(imageBytes);
        }
        catch(Exception e){
            throw new RuntimeException(e.toString());
        }
    }

        // 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."
        },
    };
}
