It's not a secret anymore!*
The moose likes Java in General and the fly likes Images in downloaded files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Images in downloaded files" Watch "Images in downloaded files" New topic
Author

Images in downloaded files

P Mc Gartoll
Greenhorn

Joined: Jul 12, 2010
Posts: 19
I created a small little java game and sent it to my friends but I realized that they don't have the images so they won't see the pictures that I put in the game. How would I make it able for them to view these images. Do I put them in the jar somehow? And what would I put for the path to these images?

Thanks!
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Yes, put them in the Jar, often in their own directory, possibly off of the directory that holds the class files. You'll want to access them using resources not as files. The tutorials should show you the way.
P Mc Gartoll
Greenhorn

Joined: Jul 12, 2010
Posts: 19
How do I insert it into the jar? And what tutorials are you talking about? I'm sorry I feel so silly right now but it would be really cool if I could get this working!
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
These tutorials:

http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/deployment/jar/index.html
http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/deployment/jar/build.html
P Mc Gartoll
Greenhorn

Joined: Jul 12, 2010
Posts: 19
How do I set the image path as a resource. If possible just give me an example and I should be able to figure it out!
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
The tutorials can tell you better than I can and can show you better examples. Please look here:
Loading Images Using getResource
P Mc Gartoll
Greenhorn

Joined: Jul 12, 2010
Posts: 19
Sweet thanks so much that's awesome!!!
P Mc Gartoll
Greenhorn

Joined: Jul 12, 2010
Posts: 19
I spoke too soon, I thought I had it but this code

I don't know why it doesn't work, the System.out.print prints out this
file:/PatricksFolder/java/Dodge!/Run1.jpg
Which appears correct but it won't run. Compiles fine but won't run.
Can I only run this when it is in the jar?
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
What happens when you try to run it? Any exceptions get triggered?

For my money, I'd try to create a small program that does nothing more than create an ImageIcon, places it in a JLabel, and shows the JLabel in a JOptionPane, and then make a Jar of it. If you can get this to work, you can get your main program to work, I'll bet.


[Thumbnail for simpleShowImage2.JPG]

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

P Mc Gartoll wrote:Compiles fine

If that's your real code I very much doubt so; you declare icon inside the if-statement, then try to access it afterwards when it's already out of scope.
And if it's not, PostRealCode.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
P Mc Gartoll
Greenhorn

Joined: Jul 12, 2010
Posts: 19
Here is my entire code for the main class. If you need all the other classes I can post them. The error I get is a null pointer exception for this code


Weird huh?




EDIT: I set the private ImageIcon icon = new ImageIcon();
and I don't get the null pointer exceptions but now none of the images appear
It still prints out the correct file path and everything but no image appears
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Is the Run1.jpg file located in the same folder (inside the JAR or not) as your Dodge.class file?

Oh, and change line 35 to "icon = ...". You are now declaring a new local variable that is instantiated in the if-block (if the resource is found). After that it goes out of scope (as I said) and the non-instantiated (and therefore null) icon instance field is used.
P Mc Gartoll
Greenhorn

Joined: Jul 12, 2010
Posts: 19
I am making a jar of the folder Dodge
Inside this folder are all the class files and another folder called images.
All my images are in this images folder.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Then use an absolute resource name, starting with /
 
GeeCON Prague 2014
 
subject: Images in downloaded files