• 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 to display image in Java from hard drive?

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okay, i have this code in which i display a image from MY hardrive.



which works pefectly fine on my pc.

But i have to turn this assignment in to the professor. Hes probably going to run the program on his machine, and the image address in my code wont work on his pc.

so the question is, how do i create a "universal image address" that will load the image no matter whose machine it runs on....man, i tried googling this but it didnt work....please help the assignment is due very soon!

[ April 27, 2005: Message edited by: m brown ]
[ April 27, 2005: Message edited by: m brown ]
 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why dont you ask the user for the image path and then pass it to the method of yours.
if you are using applets then maybe you can use the filedialog class.

that way there wont be hard-coding problems.
 
m brown
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well actually its multiple images that i need to display..i dont want to keep asking for the image path for each image
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use properties file which user have to edit(for paths).and then you can read this propety file.
Other altyernative wud be quite constly where you will have to install local database and then you can store the image in blobs column.IN this case user should also have the same database.But this will take time.
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, guys. You're getting way too complicated here. The way you show images in a reliable way across platforms/environment is by using classpath resources. You can ask your classloader to load the image data from the classpath. Check out ClassLoader.getResource() and ClassLoader.getResourceAsStream(). If you have to turn this into a professor, you can turn it in as a jar file containing all of the images you need.
 
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Readers,
I know I am a bit late in answering this but all the same since I had another idea. Instead of using exact pathnames use relative pathnames. Store the images in a folder called images in the same directory as the class files. Then use relative path like this:

Image image = Toolkit.getDefaultToolkit().getImage("../images/1.gif");

The ../ indicates the parent directory.
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That only works if the "working directory" for your running program is the same as the location of your class files. If you use classpath resources, it will work no matter where your classes are running.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic