Loading and Displaying Images

Much of the work of loading and displaying images is done asynchronously. The sender and receiver of images are identified by the AWT interfaces

An ImageProduceer is responsible for generating (or loading) the image itself and for notifying an ImageObserver of its progress by invoking the only method defined by the ImageObserver interface, imageUpdate().

The following example loads and displays the image below. To illustrate the asynchronous behavior of loading an image, we print the image size immediately after getImage returns the image im. Note the image size shows -1 for the width and height.

 
D:\cps592\C98\Images>appletviewer Example1.html
CodeBase: file:/D:/cps592/C98/Images/
Image width=-1 height=-1

Example1.java

import java.net.URL;
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;

public class Example1 extends Applet {
 	Image im;
	URL codebase;

	public void init() {
		codebase = getCodeBase();
		im = getImage(codebase, "saint.gif");
		showInfo();
	}
	public void paint(Graphics g) {
	  g.drawImage(im,0,0,this);
	}
	private void showInfo() {
		System.out.println("CodeBase: " + codebase);
		System.out.print  ("Image width=" + im.getWidth(this));
		System.out.println(" height=" + im.getHeight(this));
	}
}

Differences between Applets and Applications

Applets provide built-in support for obtaining images, namely the Applet.getImage() method. An application must do things differently, as shown in the example below, where a local Toolkit instance method is called.

Example2.java

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

public class Example2 extends Frame {
 	private Image im;

	public Example2() {
		super("Image Test");
		im = Toolkit.getDefaultToolkit().getImage("saint.gif");
		addWindowListener(new MyAdapter() );
		setSize( new Dimension(220, 330) );
		setLocation(100,100);
		show();
	}

	public void paint(Graphics g) {
	  g.drawImage(im,0,0,this);
	}

	public class MyAdapter extends WindowAdapter {
		public void WindowClosing(WindowEvent e) {
			System.exit(0);
		}
	}

	static public void main(String args[]) {
		Example2 app = new Example2();
	}
}

Waiting for an Image to Load

In the example below, we study the asychronous nature of image loading by adding a diagnostic print statement to ImageObserver.imageUpdate()

Example3.java

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;

public class Example3 extends Applet {
	Image im;

	public void init() {
		im = getImage(getDocumentBase(), "saint.gif");
	}

	public void paint(Graphics g) {
	  g.drawImage(im,0,0,this);
	}

	public boolean imageUpdate(Image image, int flags, 
							   int x, int y, int w, int h)
	{
 		System.out.println("imageUpdate(flags="+flags+", x=" + x + ", y=" + 
		                   y + "  w=" + w + ", h=" + h+")");
		return super.imageUpdate(image,flags,x,y,w,h);
	}
}

output

imageUpdate(flags=3, x=0, y=0  w=217, h=321)
imageUpdate(flags=4, x=0, y=0  w=0, h=0)
imageUpdate(flags=8, x=0, y=0  w=217, h=1)
imageUpdate(flags=8, x=0, y=1  w=217, h=1)
imageUpdate(flags=8, x=0, y=2  w=217, h=1)
imageUpdate(flags=8, x=0, y=3  w=217, h=1)

...

imageUpdate(flags=8, x=0, y=318  w=217, h=1)
imageUpdate(flags=8, x=0, y=319  w=217, h=1)
imageUpdate(flags=8, x=0, y=320  w=217, h=1)
imageUpdate(flags=32, x=0, y=0  w=217, h=321)

Using MediaTracker

The MediaTracker class provides a more convienient way to manage the asynchronous loading of an Image object.

Example4.java

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;

//  ImageTestAppletWithMediaTracker

public class Example4 extends Applet {
 	Image im;

	public void init() {

		MediaTracker tracker = new MediaTracker(this);

		im = getImage(getDocumentBase(), "saint.gif");

		tracker.addImage(im, 0); 
		try {
			tracker.waitForID(0);
		}
		catch(InterruptedException e) { }

		showImageSize();
	}
	public void paint(Graphics g) {
	  g.drawImage(im,0,0,this);
	}
	public boolean imageUpdate(Image image, int flags, 
							   int x, int y, int w, int h)
	{
		System.out.println("imageUpdate(flags="+flags+", x=" + x + ", y=" + 
						   y + "  w=" + w + ", h=" + h+")");
		return super.imageUpdate(image,flags,x,y,w,h);
	}

	void showImageSize() {
		System.out.print  ("Image width=" + im.getWidth(this));
		System.out.println(" height=" + im.getHeight(this));
	}
}

output

Image width=217 height=321

Now the call to showImageSize() returns the proper image size and updateImage() is not called at all.

Using Swing

Example5.java

import javax.swing.*;

public class Example5  {

	public static void main(String args[])
	{
		JFrame frame = new JFrame("Example 5");
		ImageIcon im = new ImageIcon("saint.gif");
		JLabel label = new JLabel(im);

		frame.getContentPane().add(label);
		frame.pack();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.show();
	}
}


Maintained by John Loomis, last updated 20 July 2000