• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

loading images from an specific directory

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic