• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to display image in Java from hard drive?  RSS feed

 
m brown
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • 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 ]
 
Niki Nono
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Amol Fuke
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • 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.
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • 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.
 
Shyam Prasad Murarka
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!