Display an Image -Scaled

Create an applet that displays an image located in the same directory as the web-page that includes the image. THIS time scale it so that it fits into the current applet size.
  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 test7b extends Applet {

    Image butch;

    int a_Height, a_Width;
  
    public void init() {

      butch = getImage(getDocumentBase(), "B.jpg");

      //deterine the size of the applet 
      a_Height= this.size().height;  //can use the new getSize() method
      a_Width= this.size().width;

    }

    //draw Scaled Image.
       public void paint(Graphics g) {
      g.drawImage(butch, 0, 0, a_Width, a_Height, this);
    }

}