Load image in jar, error in code - java

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.

Related

Path location of the image cannot locate in Eclipse swt

I have a problem when exporting my java project. It cannot find the path of my image.
File Project
src
config
image
copy.png
This is where my image is located
String image1 = "image/copy.png";
shell.setImage(new Image(display, image1));
It works before i export but when i export it and update my program it gives me an error. I tried also to use InputStream but it gives me null.
With the specified class, org.eclipse.swt.graphics.Image, you will want to create the Image with method Image(Device device, InputStream stream) and use getClass().getResourceAsStream( image1 ) to supply the inputStream containing the file. You probably need "/" at the start of your path.
Create image with this method work well.
new Image(device, getClass().getResourceAsStream(localImagePath));

Making my game a jar with pictures

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.

Create image from local relative path in java

I have an application in netbeans with a folder in the same directory as the application containing all the images, and I also have an attribute String in the class where I save the absolute path when I upload an image.
How can I obtain the relative path of an image inside the same jar to store it in a string?
Because then I create the image from the URL string.
Edit:
I have a jar including a logic package, a presentation package (with swing) and an image package. In the logic I've tried to do:
String img = new String("\src\Images\Image.jpg"):
ImageIcon icon = new ImageIcon(img);
Icon imagen = new ImageIcon(icon.getImage().getScaledInstance(photo.getWidth(),photo.getHeight(),Image.SCALE_DEFAULT));
photo.setIcon(imagen);
Recommendations:
Never create a new String with the new String(...) constructor. There's no need to do this, and you just waste resources unnecessarily. Instead simply use a String literal.
Don't use Files to get images held inside of Jar files. Jar files do not hold Files.
By passing a String into your ImageIcon's constructor, you're doing just that (check the API), you're trying to get the image as a File.
Instead use ImageIO.read(...) and a resource to get the image.
The resource path will all depend on your Jars structure, something you have yet to show us.
e.g.,
String path = "\Images\Image.jpg"; // this path may not be correct
InputStream imgStream = getClass().getResourceAsStream(path);
BufferedImage img = ImageIO.read(imgStream);
// process your image here if desired
ImageIcon icon = new ImageIcon(img);

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

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

Categories