• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Images in downloaded files

 
P Mc Gartoll
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
P Mc Gartoll
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sweet thanks so much that's awesome!!!
 
P Mc Gartoll
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
simpleShowImage2.JPG
[Thumbnail for simpleShowImage2.JPG]
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
P Mc Gartoll
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then use an absolute resource name, starting with /
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic