The following will not show the image nor will it throw any exceptions. Not
throwing exceptions doesn't surprise me because the documentation show that
it won't. That is a little surprising in itself. I am not using Swing
components.
public void paint(Graphics g){
String fileName = "dir/a.gif";
Toolkit toolkit = Toolkit.getDefaultToolkit();
Image img = toolkit.getImage(fileName);
// an awt panel's Graphics context doesnot show the image
g.drawImage(img, 0,0, null);
//this works fine
g.drawString("aString",50,50);
}
thanks, mike
Ian Shef - 23 Jun 2005 20:10 GMT
> The following will not show the image nor will it throw any exceptions.
> Not throwing exceptions doesn't surprise me because the documentation
[quoted text clipped - 14 lines]
>
> }
The image does not get loaded into an Image (e.g. img) until it is needed.
This happens asynchronously and may take some time. Thus, your g.drawImage
does not have any image yet to draw (but probably would the next time that
paint gets executed, except that you threw away img last time around and
are starting from scratch). You need a MediaTracker.
Suggestion #1: See
http://java.sun.com/developer/technicalArticles/Media/imagestrategies/
for advice on using a MediaTracker, for a description of asynchronous image
loading, and for recommendations on other ways to load images. Too bad
this advice isn't included in the documentation for Toolkit.getImage.
Suggestion #2: You want paint to execute quickly. Opening files and
loading images from within paint is probably not a good idea. I suggest
that the definition of fileName, the definition of toolkit, and the
definition of img, plus any use of a MediaTracker, all take place outside
of paint.
Good Luck!

Signature
Ian Shef 805/F6 * These are my personal opinions
Raytheon Company * and not those of my employer.
PO Box 11337 *
Tucson, AZ 85734-1337 *