• 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

How can I find my images??

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i put my images in "images" folder..for example.i have one class in same folder use the image one.gif...when i compile it in default path..it work exactly...but when i add "package" to this class ...as following:
package test;
public class Test {...}
then i compile it to F:/, and also copy one.gif to f:/test....but after do such works,,,the class can't find the image....why??? what should i do??? please help me .....
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to pass in the path name of the image, from your working folder.
Here is how I use an image in my toolbar button.

Thanks,
Jai.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
Would you tell me if i add images to the jar file,
how can i use these images?
please!
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Having images and other read-only resources on the filesystem is a Very Bad Idea IMHO. They should be packaged neatly inside the application jar. There are a number of methods in java.lang.Class (or java.lang.ClassLoader) that will help you.This call will return a URL pointing to the "/images/image.gif" file inside your application jar; you can use this URL in the ImageIcon constructor.This call will return an InputStream for the same file.
In the above snippets, we used the methods in java.lang.Class. So what's the difference with the methods of the same name in java.lang.ClassLoader?
In the code snippets so far, they would both work. The difference only shows up when you use relative paths, e.g. "images/image.gif" (note the lack of a slash "/" at the start). The callWill try to find a resouce relative to the given class, in this case, "/yourpackage/images/image.gif". This is not the case when using the ClassLoader calls, they always start at the CLASSPATH locations.
- Peter
[ January 07, 2003: Message edited by: Peter den Haan ]
 
jiaquan sun
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Peter,thank you very much!
can you give me more sample code?
which class does the 'Yourclass' means?
in this way,if user unpackage the jar file,what will happen?
will the application still run properly?
sorry for my english...
please help me.thanks!
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the code I use to load Images. The class has a constant ICON_PREFIX which is the location of all my images.
private static final String ICON_PREFIX = "/suncertify/framework/gui/icons/";
public static Icon getIcon(String iconName) {
Icons icons = new Icons();
InputStream in = icons.getClass().getResourceAsStream(ICON_PREFIX + iconName);
if (in == null) {
System.out.println("No icon found for name " + iconName);
return null;
}
Icon icon = null;
try {
byte[] bytes = new byte[in.available()];
in.read(bytes);
icon = new ImageIcon(bytes);
} catch (IOException e) {
System.out.println("Error reading image");
e.printStackTrace();
}
return icon;
Hope that helps
 
Peter den Haan
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Brian, did you try getResource()? It might simplify your code.
Jiaquan, this method loads resources using the classloader; this means that as long as you can run the application (i.e. load the application classes) you will be able to load the resources. Regardless of whether they're in a jar or somewhere on the filesystem. And if you'd rewrite your application as an applet and load classes over the web, it would still work. See why this is best practice package resources?
- Peter
 
jiaquan sun
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Brian,Peter,I have looking for the solution for very long time.I am so happy to know this!
thanks!
 
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What if the images are in a folder up in the directory hierarchy. Then can we say we can use getParent() on the File and get to the folder.

Thanks.
 
Peter den Haan
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Samual Harvey:
What if the images are in a folder up in the directory hierarchy.

Up relative to what? It is best not to make any assumptions about the current working directory, and I wouldn't want to fix my images relative to the location of the database file.
- Peter
 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Peter,
You are awesome!
This works real well in my code with getResource().
Aruna.
 
reply
    Bookmark Topic Watch Topic
  • New Topic