aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes loading images from an specific directory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "loading images from an specific directory" Watch "loading images from an specific directory" New topic
Author

loading images from an specific directory

Edilson Candido
Greenhorn

Joined: Mar 26, 2007
Posts: 3
I am trying to load an image in a swing application. The structure is
- Application_name
- src (com.test) � just an example...
- classes (com.test)
- img
If I use:

java.net.URL url = getClass().getResource(image_name);

I can get the image. And then

Image i = createImage( (java.awt.image.ImageProducer) url.getContent() );
MediaTracker mediaTracker = new MediaTracker(this);
mediaTracker.addImage(i,1);

But I would like to put the images in the img directory and not in the classes directory. Is there any way to do it? I was thinking about a properties file specifying the image place as hard code (for instance, C:\Project\application_name\img) but I do not if that�s a good idea. Can anyone help me?
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
If the img folder is in your classpath then you should be able to load the images it contains with:
java.net.URL url = getClass().getResource("img/" + image_name);
You shouldn't have to specify where the img folder is relative to the classes folder. The class loader does that for you.
Edilson Candido
Greenhorn

Joined: Mar 26, 2007
Posts: 3
Hi Craig. Thank you for your help.

I've found another way for not use getClass().getResource(image_name).

That�s:
File file = new File("img/" + image_name);
Image i = ImageIO.read(file);


and then:
MediaTracker mediaTracker = new MediaTracker(this);
mediaTracker.addImage(i,1);


On this way I don't need to use the CLASSPATH.

Best Regards
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: loading images from an specific directory