Making my game a jar with pictures - java

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);

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")

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.

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.

Related

Getting image path to use it as system tray icon

So I've been looking at many, many solutions to this "problem" but somehow nothing worked for me. I'm trying to set a system tray icon for my Java application, but no matter what I do, the icon just won't show up. Instead there's a blank spaceholder. I've tried different things to get the image path but it seems like I can't get the correct image path.
This is what the code currently looks like:
URL url = System.class.getResource("image.jpg");
Image img = Toolkit.getDefaultToolkit().getImage(url);
SystemTray mainTray = SystemTray.getSystemTray();
TrayIcon trayIconImage = new TrayIcon(img, "tray icon");
mainTray.add(trayIconImage);
System.print.out(url) returns null
What am I doing wrong when getting the path?
I can post a screenshot of the Eclipse folders in case that helps.
You can drag and drop the image into the package folder where your .java files are shown inside the IDE. Just drag the file from the desktop, or whatever location, and drop it into the IDE where you see your source files. after that you can simply refer to the image, "image.jpg", in your code when declaring a File or Image, or whatever class you use to make it into an object. When you export the project the image will be included so you will not have to worry about the path. Should be similar to what I did in the photo. I dragged and dropped the int.txt file into the source package.
if this was helpful accept the answer.
You should be able to use this:
Image img = new BufferedImage("image.jpg");
SystemTray mainTray = SystemTray.getSystemTray();
TrayIcon trayIconImage = new TrayIcon(img, "tray icon");
mainTray.add(trayIconImage);
Basically the program can find the file because they are in the same directory which is the source packages folder.
You may want to try using the Path class and File class if you are worried about getting paths.

Java applet not able to find resource image with ImageIcon unless entire path is used

I've placed the image TestIcon.png in the src folder of a Java applet (the directory is C:\Users\User\workspace\applettest2\src\TestIcon.png). Everything I've read says that I should be able to use the image simply by referencing "TestIcon.png". Example:
ImageIcon xIcon = new ImageIcon("TestIcon.png");
Howerver, running the applet in Eclipse with this code doesn't work; the image is not displayed. If I type out the image's entire path:
ImageIcon xIcon = new ImageIcon("C:\\Users\\User\\workspace\\applettest2\\src\\TestIcon.png");
then the applet displays the image properly. Isn't the default path for resources supposed to be in the "src" folder? Thanks.
If you are running it inside the Applet then try with Applet#getCodeBase() and Applet#getDocumentBase
Image image = getImage(getDocumentBase(), "TestIcon.png");
Find more samples Here and Here

Load image in jar, error in code

I want to reference an image in my project that I will package into a Jar file.
This code is not working:
ImageIcon image = new ImageIcon("Images/buttonBackgroundSelected.png");
Any ideas? thanks
This constructor only works if the image is available in the file system outside the JAR file
new ImageIcon("Images/buttonBackgroundSelected.png");
You almost never want to do this. You could use:
ImageIcon imageIcon =
new ImageIcon(getClass().getResource("/Images/buttonBackgroundSelected.png"));
when the folder and image and the have been included in the JAR file.
However, loading images this way fails silently if any issues loading the image. Therefore use ImageIO.read should be used:
Image image =
ImageIO.read(getClass().getResource("/Images/buttonBackgroundSelected.png"));
The resultant image can be wrapped in an ImageIcon if required.

How do I access a .gif file in a package in the src folder?

I'm working on a Java program using Eclipse. Right now, I have an src folder that contains 2 packages: memory.views and memory.resources.
In the memory.views package, I have my Main.java file. In the memory.resources package, I have my .txt file and .gif file.
Within the program, I have no problem accessing (and manipulating) the .txt file by using the path /memory/resources/name.txt. However, when I do the same with the .gif file using the code below, I get no result:
ImageIcon icon = new ImageIcon("/memory/resources/name.gif");
There's no error produced. The only effect is that I see no image when the program is running.
I've tried also writing the following, but none worked:
ImageIcon icon = new ImageIcon("/resources/name.gif"); <br>
ImageIcon icon = new ImageIcon("name.gif");
Now, just so nobody says that it's the .gif file's fault, I've actually entered in the full Finder path (I'm using a Mac) and that worked perfectly:
ImageIcon icon = new ImageIcon("/Users/[...]/src/memory/resources/name.gif");
However, I don't want to do the full path, because if I export the program and run it on another computer, then the non-programming computer won't display the image either.
So, right now, I don't even know what the problem is. The .gif file works sometimes, but not when it's accessed via the same path as the .txt file, which works all the time. I tried looking here (Cannot access resource by using the getResources() method), but it seems like he had a slightly different problem from me.
You can use
URL url = ClassLoader.getSystemResource("name.gif");
ImageIcon icon = new ImageIcon(url);
provided that your name.gif file ends up in classpath after compilation/build.
What is this ImageIcon class? Is it your own code?
Try Thread.currentThread().getContextClassLoader().getResourceAsStream("/memory/resources/name.gif");
See if this works...
File file = new File("/memory/resources/name.gif");
ImageIcon icon = new ImageIcon(file.getAbsolutePath());

Eclipse project with ImageIcon

I have an eclipse project with image folder. I need to create an ImageIcon from an image located in this folder but can not figure out how to tell it to look in the folder with out giving exact position on my system which I do not want to do as the project gets moved around to different pc's.
Code example
ImageIcon ai = new ImageIcon("/somedir");
Thanks for the help.
you have to store the image folder into your projectand then:
java.net.URL imageURL = this.getClass().getClassLoader().getResource("path_to_image_folder");
ImageIcon aceOfDiamonds = new ImageIcon(imageURL);
The path to the image folder can be relative to your class or absolute (starts with "/").
You can take a look here: How to use icons
It's very easy (remove the backslash):
ImageIcon ai = new ImageIcon("somedir");

Categories