Main page > Java > Code examples > Imaging

Viewer.java—Loading and displaying an image with Toolkit

This small program loads an image via java.awt.Toolkit and displays it in a window. You will need Java 1.1 or higher to run it successfully. If you leave out the call to addWindowListener even Java 1.0 will be enough. The program waits for the image to be loaded completely using MediaTracker. This text also shows how to deal with compiler deprecation warnings.

To use this program do the following:

Now, how does this work? Let's step through the code like the virtual machine does. Like all Java programs, execution starts at static void main(String[]). That main method only creates an object of Viewer and gives the name of an image file to its constructor Viewer(String). This fails if the program was called with no arguments.

Now let's go to the constructor. First, we ask for a Toolkit object from the static getDefaultToolkit method. The next line does the actual loading: image = toolkit.getImage(fileName);. More precisely, the loading process is started in a different thread. The execution will immediately return to the Viewer constructor while the image is loaded in the background.

In order to wait for the image to be loaded, we use a MediaTracker. The call to the waitFor method will make sure that the remaining statements in the constructor will be executed only after the image was loaded completely.

The rest of the constructor does the following:

If you are using Java 5 or later, your compiler may display a warning message which looks like that:

Note: Viewer.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

Following those instructions, you compile again like this:

javac -Xlint:deprecation Viewer.java

Now you get the following output:

Viewer.java:28: warning: [deprecation] show() in java.awt.Window has been deprecated
                show();
                ^
1 warning

Looking up the method show of the class java.awt.Window in the Java API documentation of JDK 5.0 or higher (online version), you can find out that show is supposed to be replaced with setVisible(boolean). Following the link to setVisible we learn that a boolean parameter value true will show a component while false will hide it. Thus, the call to show(); can be replaced with setVisible(true); to make the program compile with JDK 5 without warnings. However, that modified version may cause problems with Java Runtime Environments which do not know the setVisible method. The documentation of setVisible notes that setVisible is available since JDK 1.1. The modified program will consequently not run with Java 1.0.x. Hardly anyone is using such an old version, but you should always figure out under what circumstances your program will fail and document them.

There is one method left to be documented, the paint method. It is called by the GUI system to draw the image whenever that is necessary, e.g. when the window becomes visible for the first time or after being overlapped by another window, which has now been closed. All we do in our own version of paint is to draw our image at the top left corner (0, 0) using the Graphics object given to us. The fourth parameter is an ImageObserver which we will not use, thus giving a null value to drawImage. All this is documented in the API as well, you should look up all classes and methods used in this program as an exercise.

Source code

/* 
 * Viewer.java
 */

import java.awt.*;
import java.awt.event.*;

public class Viewer extends Frame {
	private Image image;

	public Viewer(String fileName) {
		Toolkit toolkit = Toolkit.getDefaultToolkit();
		image = toolkit.getImage(fileName);
		MediaTracker mediaTracker = new MediaTracker(this);
		mediaTracker.addImage(image, 0);
		try
		{
			mediaTracker.waitForID(0);
		}
		catch (InterruptedException ie)
		{
			System.err.println(ie);
			System.exit(1);
		}
		addWindowListener(new WindowAdapter() {
      		public void windowClosing(WindowEvent e) {
        		System.exit(0);
      		}
		});
		setSize(image.getWidth(null), image.getHeight(null));
		setTitle(fileName);
		show();
	}

	public void paint(Graphics graphics) {
		graphics.drawImage(image, 0, 0, null);
	}

	public static void main(String[] args) {
		new Viewer(args[0]);
	}
}