Reputation: 11
I finally finished my game and I want to distrubute it to couple of my friends, my only problem is that I can make it into runnable jar file and it works perfectly fine on my laptop, but when I upload it on the web and send others a link, when they download it the images are not there. I searched for solutions, but the ones I come across I do not understand at all, can someone please explain it to me? :3
Below are all images in my code.
Game Class
Graphics2D g2d = (Graphics2D) g;
ImageIcon ic= new ImageIcon ("D:/USER/Desktop/Other/Snow.jpg");
g2d.drawImage(ic.getImage(),0,0,null);
User Class
public Image useriamge(){
ImageIcon icon = new ImageIcon("D:/User/Desktop/Other/us.png");
return icon.getImage();
Obstacles Class
ImageIcon icon = new
ImageIcon("D:/User/Desktop/Other/ob.png");
return icon.getImage();
Main Class
final JFrame Starting_menu = new JFrame ("Game");
ImagePanel background = new ImagePanel("D:/USER/Desktop/Other/Snow.jpg");
Starting_menu.add(background);
final JFrame Help_screen = new JFrame ("Help menu");
ImagePanel background2 = new ImagePanel("D:/USER/Desktop/Other/HelpScreen.jpg");
Help_screen.add(background2);
Upvotes: 1
Views: 513
Reputation: 15146
The paths you are specifying are specific to your computer.
What you can do is store the images in your project's src
folder, then reference them using getClass().getResource("Snow.png")
.
I suggest creating a new package for your images, maybe called res
for resources. When you need to load the image, you'd do getClass().getResource("res/Snow.png")
Upvotes: 2
Reputation: 133
Well, correct me if I am wrong, but you are loading the images from a random folder on your hard drive. When you send the game to someone, the .jar file will look for the image file in the location you have written in the code. If your friend doesn't have the image folder on the same location, the .jar file wouldn't be able to find and load the images. If you just simply put the images in your java_game_project_folder/images, you can just load them with "images/filename.jpeg". And also you can include the resource folder in the .jar file.
Upvotes: 1
Reputation: 4125
The reason that this happens is that you are referencing specific files on your computer. Files which they do not have on their computer. Either ship the pictures along with the jar, and include either an install script to put them in the correct location, or instructions on where to put them.
Alternatively package them inside of the jar - by convention in the resources folder - so that they will always be available. However you will not be able to change the images at all without changing the jar in this case.
Upvotes: 0