File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes What's the best way to getImage()? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "What Watch "What New topic

What's the best way to getImage()?

Timothy Stone
Ranch Hand

Joined: Aug 01, 2001
Posts: 71
I'm having some trouble figuring out what is the best, most reliable, cross-platform method of getting a handle on an Image object from the local filesystem.
I'm noticing some confusing methods in the API but have worked around them and have something working on a Wintel platform but I want to make it as platform-agnostic as possible.
Two (2) methods exist in the Toolkit API:
public abstract Image getImage( java.lang.String ) and
public abstract Image getImage( )
I've noticed in my development wanderings that the String object passed is not noted as being relative to the class/package or to "root". I'm saying this because, given a directory "images" under the package, the String "images/imagex.gif" will return null, but the String "/images/imagex.gif" works, as does the String "C:/.../images/imagex.gif". Where is the proper String path documented?
My second dilemma is which method is better? I saw a Swing example using:
ClassLoader.getSystemResource( java.lang.String ) where the String object passed is a relative path to the image, i.e. "images/imagex.gif". The static method getSystemResource returns a URL object from relative to the CLASSPATH defined on the system. This seems to be very platform agnostic and preferred in cross platform development, but I have not found any notes that this is good/bad/ugly. Can anyone help this conflicted developer?
Thanks a ton.

Timothy Stone, MIT, SCJP
"This Satan's drink [coffee] is so delicious, we shall cheat Satan and baptize it." --Pope Clement the VIII (1592-1605)
I agree. Here's the link:
subject: What's the best way to getImage()?
It's not a secret anymore!