Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2005

Tip: Looking for answers? Try searching our database.

awt's toolkit

Thread view: 
Michael G - 22 Jun 2005 15:11 GMT
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    *



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.