Drawing Images | Heaton Research

Drawing Images

One of the most powerful graphical elements that you can display is an image. Any image, saved as a .GIF or .JPG file can be displayed by your applet. In this section I will show you how to load and display graphical images.

For this example applet we will load and display a JPG file named "sea.jpg". This applet will look like Figure 2.6 when ran.

 Display an Image

Figure 2.6: Display an Image

This program's source code can be seen in Listing 2.6.

Listing 2.6: Displaying Images (ImageApplet.java)

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.MalformedURLException;
import java.net.URL;

public class ImageApplet extends Applet
{
  public static final int IDEAL_WIDTH = 320;
  public static final int IDEAL_HEIGHT = 240;

  private Image loadImage(String name)
  {
    Image result = null;
    MediaTracker tracker = new MediaTracker(this);

    Toolkit toolkit = Toolkit.getDefaultToolkit();

    result = toolkit.getImage(name);
    tracker.addImage(result, 0);
    try
    {
      tracker.waitForAll();
    } catch (InterruptedException e)
    {
      return null;
    }

    return result;
  }

  public void paint(Graphics g)
  {
    Image sea = loadImage("sea.jpg");
    g.drawImage(sea, 0, 0, null);
  }

  public static void main(String args[])
  {
    Applet applet = new ImageApplet();
    Frame frame = new Frame();
    frame.addWindowListener(new WindowAdapter()
    {
      public void windowClosing(WindowEvent e)
      {
        System.exit(0);
      }
    });

    frame.add(applet);
    frame.setSize(IDEAL_WIDTH, IDEAL_HEIGHT);
    frame.show();
  }
}

Before an image can be displayed it must be loaded into an "Image" object. There are several steps necessary to load an image.

  • Create a Toolkit object
  • Load the image
  • Add to a MediaTracker
  • Use the media tracker to wait for the image to load

Because of these steps I created a simple method named loadImage. You simply pass loadImage the name of the image you want to load, and it returns an Image object if it was loaded successfully, or null otherwise.

It is also important to note where the image file should be located. If you are running the program as an applet, then the image file should be uploaded to the same directory as the .class file for the applet. If you are running it as an application, then the image file should be located in the directory that holds the .class files for the application.

Now I will show you, in detail, each of the steps required to load an image. First you must create a Toolkit MediaTracker objects. This is done with the following lines of code.

MediaTracker tracker = new MediaTracker(this);
Toolkit toolkit = Toolkit.getDefaultToolkit();

The MediaTracker will be used to wait for the images to load, and the Toolkit will actually load the images. Next you will call the toolkit object to load the image.

result = toolkit.getImage(name);

The image returned from getImage has not been loaded yet. You will next instruct Java to wait for this image to load. To do this you begin by adding the image to the media tracker, as follows.

tracker.addImage(result, 0);

Finally, you will wait for the image to load.

try
{
  tracker.waitForAll();
} catch (InterruptedException e)
{
  return null;
}

Once the image is loaded, it should be displayed. To display an image you should use the drawImage method in the Graphics object. The image is loaded and displayed as follows. Calling the loadImage method performs all of the steps just mentioned.

Image sea = loadImage("sea.jpg");
g.drawImage(sea, 0, 0, null);

As you can see, you pass in the Image object to be displayed, the x and y coordinates, as well as an image observer. We will not be using the image observer, so we pass in null.

Copyright 2005-2008 by Heaton Research, Inc.