• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

loading images from an specific directory

 
Edilson Candido
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic