Display an Image

Create an applet that displays an image located in the same directory as the web-page that includes the image.
  1. you must create a Java applet with a class that extends the Applet class. (you must import the java.applet package).
  2. you should create an init() method that gets the image and creates an instance of the Image class.
  3. you must have at least the method paint() that is part of this class. In this method you will call the drawImage() method of the Graphics class.


Here is the applet: Need Java Enabled Browser

Solution

//Purpose:  Open an image from the same directory as the web-page
// that includes this applet and display it to the screen.


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


public class test7 extends Applet {

    Image butch;

  
    public void init() {
      butch = getImage(getDocumentBase(), "B.jpg");
    }

    //draw as must as you can of the image.
    //   Note that we use the Applet itself (this) as
    //     an object that implements the ImageObserver interface
    //     the applet inheirits this from is Component superclass.
    //     this monitors the loading of the Image.
    public void paint(Graphics g) {
      g.drawImage(butch, 0, 0, this);
    }

}