Java - How to add an image to resource - java

I really have a problem adding images to my java project, and before you ask, yes i have searched already and tried everything, but i just can't get it working.
Here is my problem:
At the moment i am using this code to get the images:
ImageIcon goldIcon = new ImageIcon("res/Gold_coin.png");
ImageIcon silverIcon = new ImageIcon("res/Silver_icon.png");
ImageIcon copperIcon = new ImageIcon("res/Copper_icon.png");
My project structure is as following:
I have one project folder with two sub folders.
Both sub folders are specified as source folders, one is the "src" folder and the other one i named "res". In the "src" folder i have one package with all classes in it. In the "res" folder i have all the images saved.
Now the strange thing is, the "Gold_icon" DOES work, but both silver and copper do NOT. I am using eclipse luna and if someone could give me a step by step instruction how to add an image would be really nice.
Because all i find is always "add to resource" , "add it to resources folder" and honestly, i tried creatig a new folder, i copied it to the "src" folder, i tried every possible call, from ("res/Gold_coin.png") over ("/Gold_coin.png") to
("Gold_coin.png") and ("/res/Gold_coin.png")
I refreshed the project, the folders, the package, the classes, i restarted eclipse but nothing helps
I just don't get it..
Please help :(
If you need the information what i want to do with this images afterwards, i am adding them together into a JPanel with flowlayout which i write into a JTable cell with a cellrenderer, which is everything working with the gold icon, but not the other two. And it also does not work to remove the gold icon (because i thought maybe for whatever reason only the first icon works..) but then nothing is displayed

You should use getResource to load images or whatever from resource folder.
For example:
String pathToImage = "res/Gold_coin.png";
ImageIcon myIcon = new ImageIcon(getClass().getClassLoader().getResource(pathToImage));
or with all project path: nameOfProject/res/Gold_coin.png.

Related

Unable to access an image while in a package [duplicate]

I have a Java Project in NetBeans 7.0.
I want to add some image to some label dynamically. The image will differ depending on the state of the program.
I put one such image, 'filling.jpg', in the 'resources' folder of my project.
I want to reach this file correctly (not by absolute or relative path, because that will cause problems when I build the jar file).
So I found this method:
ImageIcon fillingIcon = new ImageIcon(getClass().getClassLoader().getResource("filling.jpg"));
labelFontFilling.setIcon(fillingIcon);
It keeps give me java.lang.NullPointerException.
But I am sure that there is that image, because I can assign the image to the label from the NetBeans Properties menu for that label (but I don't want this, I want to add the image by Java code).
What am I doing wrong, and how can I get that image correctly?
This was a pain, using netBeans IDE 7.2.
You need to remember that Netbeans cleans up the Build folder whenever you rebuild, so
Add a resource folder to the src folder:
(project)
src
project package folder (contains .java files)
resources (whatever name you want)
images (optional subfolders)
After the clean/build this structure is propogated into the Build folder:
(project)
build
classes
project package folder (contains generated .class files)
resources (your resources)
images (your optional subfolders)
To access the resources:
dlabel = new JLabel(new ImageIcon(getClass().getClassLoader().getResource("resources/images/logo.png")));
and:
if (common.readFile(getClass().getResourceAsStream("/resources/allwise.ini"), buf).equals("OK")) {
worked for me. Note that in one case there is a leading "/" and in the other there isn't.
So the root of the path to the resources is the "classes" folder within the build folder.
Double click on the executable jar file in the dist folder. The path to the resources still works.
I have a slightly different approach that might be useful/more beneficial to some.
Under your main project folder, create a resource folder. Your folder structure should look something like this.
Project Folder
build
dist
lib
nbproject
resources
src
Go to the properties of your project. You can do this by right clicking on your project in the Projects tab window and selecting Properties in the drop down menu.
Under categories on the left side, select Sources.
In Source Package Folders on the right side, add your resource folder using the Add Folder button. Once you click OK, you should see a Resources folder under your project.
You should now be able to pull resources using this line or similar approach:
MyClass.class.getResource("/main.jpg");
If you were to create a package called Images under the resources folder, you can retrieve the resource like this:
MyClass.class.getResource("/Images/main.jpg");
Thanks, Valter Henrique, with your tip i managed to realise, that i simply entered incorrect path to this image.
In one of my tries i use
String pathToImageSortBy = "resources/testDataIcons/filling.png";
ImageIcon SortByIcon = new ImageIcon(getClass().getClassLoader().getResource(pathToImageSortBy));
But correct way was use name of my project in path to resource
String pathToImageSortBy = "nameOfProject/resources/testDataIcons/filling.png";
ImageIcon SortByIcon = new ImageIcon(getClass().getClassLoader().getResource(pathToImageSortBy));
For me it worked like I had images in icons folder under src and I wrote below code.
new ImageIcon(getClass().getResource("/icons/rsz_measurment_01.png"));

Java: Cannot access images in resources

I've been trying to export my project as a Runnable Jar, but my resources would not load because I was directly trying to access the image from my project's path. For example:
ImageIcon icon = new ImageIcon("resources/icon.png");
This would work when I ran the project from Eclipse itself, but I noticed that the images were not being included in the jar file. So after researching, I found out that I need to create source folders and put the images/text files inside them, and then use getClass().getResource() in order to access them. However, when I do this, the URL is always returned as null.
For reference, this is what my project explorer looks like:
Test
---src
---resources
---icon.png
---config
---file.ini
And here is the code that is giving me a NullPointerException when trying to access icon.png:
ImageIcon icon = getClass().getResource("/resources/icon.png");
Alternatively, I have also tried:
ImageIcon icon = getClass().getClassLoader().getResource("resources/icon.png");
But that also ends up in a NullPointerException. I have checked many solutions online but none of them have seemed to work for me. Please note that I also need to be able to access the .ini file, so a solution that only works for images won't fully solve my problem. Any help would be greatly appreciated, thanks.
Your structure needs to be:
TEST
-SRC
-Resources
-icon.png
The way your code is right now is there is no image because your code is referencing /src/resources not test/resources

Opening image asset Java JDK 8

I am attempting to load an image called Default.png stored within the project and draw it onto a canvas. I am well aware of ImageIO.read however no matter what path I give it, I can't seem to load it. Where should I put the image? I have tried putting it in a separate folder calles "res," putting it into assets.author.mypackagename.textures, but no matter what I do I cannot seem to find the right location and how to access it. Any help is appreciated, comment for further specifics.
Actually the resources are loaded in the classpath relative to the current package. If /com/daniel/project/src/ is in your classpath, and images are in /com/daniel/project/src/image then use:
ImageIO.read( ClassLoader.getSystemResource( "image/Default.png" ) );
But the src folder is not included in the classpath by IDEs generally. Try adding the image to the bin folder.
If You have it in a separate folder called res you can load the image by doing this:
ImageIO.read(this.getClass().getResource("/Default.png"));
you can also do something like:
ImageIO.read(new File("res/Default.png"));
The second method doesn't need the picture to be in another folder, but for me it's cleaner that way.

Java IDE - Eclipse, Importing resources

Hopefully someone can help me with something that is probably very simple. I'm new to Java coding (2 weeks in), and using Eclipse IDE under Linux. I'm currently in the process of creating a JFrame application, everything so far is going well.
I have one little snag though - I have included a set of Icons and assigned them to a JLabel, and have them displayed. Upon exporting an Executable JAR, they are not in the JAR as a resource. If I open the JAR file, I can see the Images, in the ROOT of the JAR, not organized in their respective folders. (icons/, etc).
TL;DR - How do I import resources, in their folders, into a JAR as a resource.
public void drawCategoryIcons() {
for (int i = 0; i < aspCategories.length; i++) {
Icon pcIcon = new ImageIcon(getClass().getResource( "/icons/" + cats[i]));
aspCategories[i] = new JLabel("", JLabel.CENTER);
aspCategories[i].setIcon(pcIcon);
panel.add(aspCategories[i], "w 200, center");
}
}
If I RUN the project within Eclipse, everything works as it should. Exporting it, I get tons of errors.
You have probably created the icons directory as separate source folder in the eclipse project (it has an icon composed of a folder icon and a package icon then). Source folders are just logical elements during development (so you can separate modules of your application logically), they are not part of the exported application. Only everything inside source folders is exported.
Therefore a simple workaround might be to instead create a package "icons" in your other source folder (typically called "src"). Those packages will exist as sub folders after export.

How to correctly get image from 'Resources' folder in NetBeans

I have a Java Project in NetBeans 7.0.
I want to add some image to some label dynamically. The image will differ depending on the state of the program.
I put one such image, 'filling.jpg', in the 'resources' folder of my project.
I want to reach this file correctly (not by absolute or relative path, because that will cause problems when I build the jar file).
So I found this method:
ImageIcon fillingIcon = new ImageIcon(getClass().getClassLoader().getResource("filling.jpg"));
labelFontFilling.setIcon(fillingIcon);
It keeps give me java.lang.NullPointerException.
But I am sure that there is that image, because I can assign the image to the label from the NetBeans Properties menu for that label (but I don't want this, I want to add the image by Java code).
What am I doing wrong, and how can I get that image correctly?
This was a pain, using netBeans IDE 7.2.
You need to remember that Netbeans cleans up the Build folder whenever you rebuild, so
Add a resource folder to the src folder:
(project)
src
project package folder (contains .java files)
resources (whatever name you want)
images (optional subfolders)
After the clean/build this structure is propogated into the Build folder:
(project)
build
classes
project package folder (contains generated .class files)
resources (your resources)
images (your optional subfolders)
To access the resources:
dlabel = new JLabel(new ImageIcon(getClass().getClassLoader().getResource("resources/images/logo.png")));
and:
if (common.readFile(getClass().getResourceAsStream("/resources/allwise.ini"), buf).equals("OK")) {
worked for me. Note that in one case there is a leading "/" and in the other there isn't.
So the root of the path to the resources is the "classes" folder within the build folder.
Double click on the executable jar file in the dist folder. The path to the resources still works.
I have a slightly different approach that might be useful/more beneficial to some.
Under your main project folder, create a resource folder. Your folder structure should look something like this.
Project Folder
build
dist
lib
nbproject
resources
src
Go to the properties of your project. You can do this by right clicking on your project in the Projects tab window and selecting Properties in the drop down menu.
Under categories on the left side, select Sources.
In Source Package Folders on the right side, add your resource folder using the Add Folder button. Once you click OK, you should see a Resources folder under your project.
You should now be able to pull resources using this line or similar approach:
MyClass.class.getResource("/main.jpg");
If you were to create a package called Images under the resources folder, you can retrieve the resource like this:
MyClass.class.getResource("/Images/main.jpg");
Thanks, Valter Henrique, with your tip i managed to realise, that i simply entered incorrect path to this image.
In one of my tries i use
String pathToImageSortBy = "resources/testDataIcons/filling.png";
ImageIcon SortByIcon = new ImageIcon(getClass().getClassLoader().getResource(pathToImageSortBy));
But correct way was use name of my project in path to resource
String pathToImageSortBy = "nameOfProject/resources/testDataIcons/filling.png";
ImageIcon SortByIcon = new ImageIcon(getClass().getClassLoader().getResource(pathToImageSortBy));
For me it worked like I had images in icons folder under src and I wrote below code.
new ImageIcon(getClass().getResource("/icons/rsz_measurment_01.png"));

Categories

Resources