• 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

Images fail to show - filepath prob?

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey you guys,

I finished a 3 tiered standalone application that has a neat looking GUI (for a swing beginner)...

problem is, when I tested the packaged app on another system everything worked fine apart from all my images, which failed to appear.

The images are used as backgrounds for all my different screens and components are positioned on top using GridbagLayout/constraints etc...
Works fine on my system and I have placed all the relevant image files in the gui package.

I think the problem lies in the filepath specification, where I defined it as:

filename = "c:/JavaParcs/project/gui/lake.jpg";



This is specific to my system but how can I fix it so that it can be accessed by any other?
(I tried taking out the c:/ and the pics dissappeared on mine too)
I also tried:

filename = "./JavaParcs/project/gui/lake.jpg";


thinking that if a dot means look in the current dir.... but that didn't work either.



Can anyone help or at least point me to a relevant resource on the matter?

Regards,
J
[ September 24, 2004: Message edited by: J Chandler ]
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use a relative path from the folder that contains your .class files to the image(s). This may take a little experimenting to find your way but you'll soon learn to navigate among your folders with ease.
 
J Chandler
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Craig -

thanks for the advice - I didn't know I could navigate back up the file hierarchy!

Had no luck in finding the solution yet though...
tried re-arranging the path in several different ways, calling getParent()
...(which compiled but didn't work)
...also done many searches to find info about navigating back up the file hierarchy, but no joy as yet.

Will keep trying
J
 
J Chandler
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok, I give in...

trawled all resources I have on hand - books, net, even grilled my 14 yr old son ... I just haven't turned up anything specific to what I'm trying to do.

More help would be gratefully received,
regards, JJ
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's an easy way to load images. ImageIO is new in j2se 1.4. Also new in j2se 1.4 the Class method getResource will look inside your jar files. The getResource method allows the class loader to find your images; so the images must be available on your class path. In the example below the image is located inside a folder named images which is in the same folder as the .class file. There is a discussion of this in the section Loading Images Using getResource on the page How To Use Icons in the java tutorial.

[ September 26, 2004: Message edited by: Craig Wood ]
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<<
thanks for the advice - I didn't know I could navigate back up the file hierarchy! >>

To back up the the file hierarchy use

filename = "c:/JavaParcs/project/gui/lake.jpg";

If your current working dir is "gui", inorder to refer a file "lake.jpg" in "JavaParcs" dir.

The relative filepath is "../../lake.jpg"

Hope this helps.
 
J Chandler
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Craig,

thanks for your reply...

So, I could pass a filename in through the constructor, each time I want to load a new image, right?


public class FindingImages {
public FindingImages(String filename) {
Image image = loadImage(filename);
ImageIcon icon = new ImageIcon(image);
etc.....
}

private Image loadImage(String filename) {
//won't need this now
//String fileName = "images/cougar.jpg";
BufferedImage image = null;
try {
URL url = getClass().getResource(filename);
image = ImageIO.read(url);
}
etc...
}

public static void main(String[] args) {
new FindingImages("images/couger.jpg");
}
}



Javaoops - I will also try backing up the hierarchy using your suggested method...
So, I don't have to keep my image files in any set place?
I had them in the gui package, but as long as I can navigate anywhere, I may as well put them in their own folder named images.

Thanks for all the help you guys...if I got it all wrong, please post a virtual kick in the pants and put me straight!

Cheers,
J

[ September 28, 2004: Message edited by: J Chandler ]
[ September 27, 2004: Message edited by: J Chandler ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic