JDK-4298019 : Toolkit.getDefaultToolkit().getImage(URL) fails to load image
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 1.3.0
  • Priority: P2
  • Status: Closed
  • Resolution: Not an Issue
  • OS: solaris_2.6
  • CPU: generic
  • Submitted: 1999-12-09
  • Updated: 1999-12-09
  • Resolved: 1999-12-09
Description
Toolkit.getDefaultToolkit().getImage(URL) fails to load the image.  The
following program named image1.java should demonstrate the problem.  It'll
occasionally display but will fail to display consistently.

Whereas the next program image2.java that uses
Toolkit.getDefaultToolkit().getImage(String) will work.

Roger Pham 12/8/99
================================================================================
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class image1 extends Frame {

    image1() {

	Image image;
	String s = "file:/net/jubilee/home/rogerp/java/SiameseCat.gif";
	try {
            image =
                Toolkit.getDefaultToolkit().getImage(new java.net.URL(s));
        }
        catch (java.net.MalformedURLException e) {
            throw new RuntimeException(e.getMessage());
        }

	add(new imageCanvas(image));

	setSize(200, 200);
	setVisible(true);

    }

    class imageCanvas extends Panel {
	Image im;
	imageCanvas(Image im) {
	    this.im = im;
	}
	public void paint(Graphics g) {
            g.drawImage(im, 0, 0, 200, 200, this);
        }

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

    }
    public static void main(String argv[]) {
	image1 f = new image1();
	f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                System.exit(0);
            }
        });
    }
}

================================================================================
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class image2 extends Frame {

    image2() {

	Image image = Toolkit.getDefaultToolkit().getImage("./SiameseCat.gif");

	add(new imageCanvas(image));

	setSize(200, 200);
	setVisible(true);

    }

    class imageCanvas extends Panel {
	Image im;
	imageCanvas(Image im) {
	    this.im = im;
	}
	public void paint(Graphics g) {
            g.drawImage(im, 0, 0, 200, 200, this);
        }

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

    }

    public static void main(String argv[]) {
	image2 f = new image2();
	f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                System.exit(0);
            }
        });
    }
}

================================================================================

Comments
EVALUATION This is not a bug. The submitter is not properly using the asynchronous Image API correctly. He assumes that getImage loads all of the image's bits into memory. However, it is well documented that the actual loading of bits does not take place until a call to Component.prepareImage or Graphics.drawImage. In addition, these two functions return *before* the Image is fully loaded. Developers are required to install an ImageObserver to listen for notification that the Image has been fully loaded. Once they receive this notification, they can repaint the Image. I have rewritten the test case to conform to this model. The rewritten test performs correctly. import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; import java.awt.image.ImageObserver; public class image1 extends Frame { public static void main(String argv[]) { image1 f = new image1(); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent we) { System.exit(0); } }); } public image1() { Image image; String s = "file:SiameseCat.gif"; try { image = Toolkit.getDefaultToolkit().getImage(new java.net.URL(s)); } catch (java.net.MalformedURLException e) { throw new RuntimeException(e.getMessage()); } add(new imageCanvas(image)); setSize(200, 200); setVisible(true); } class imageCanvas extends Panel { Image im; imageCanvas(Image im) { this.im = im; prepareImage(im, this); } public void paint(Graphics g) { g.drawImage(im, 0, 0, 200, 200, this); } public void update(Graphics g) { g.drawImage(im, 0, 0, 200, 200, this); } public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) { if ((infoflags & ImageObserver.ALLBITS) != 0) { repaint(); return false; } return true; } } } david.mendenhall@eng 1999-12-09
09-12-1999