Load image from a file inside a project folder - java

I'm trying to load an image from a file without using a FileChooser.
The folders are:
TestProject
-src
--application
---(all_the_classes_i'm_using.java)
-assets
--drawIcon.png
I want to load the image in the assets folder.
I've tried:
Image image = new Image("../assets/drawIcon.png")
Image image = new Image(getClass().getResourceAsStream("../assets/drawIcon.png"))
I've tried it with the string path "/TestProject/assets/drawIcon.png", but nothing. I don't understand how to load this image!

Set the assets directory as a resource directory and then load the image as a resource from the location "/drawIcon.png":
URL url = getClass().getResource("/drawIcon.png");
Image image = ImageIO.read(url);
In case you want to create a javafx Image:
Image image = new Image("/drawIcon.png");
In this case, also, mark that folder as resource folder.
More info here: https://docs.oracle.com/javafx/2/api/javafx/scene/image/Image.html

You can use getResource(path).toString();
the path must start with /, and it starts with the verry first package in your src folder.
Image img= new Image(getClass().getResource("/path/in/your/package/structure/icon.png").toString());

Related

Export resource in jar file

I have a problem to export my Jframe app with the background resource into jar file (without any external resource folder. I prefer to have everything into jar file for better portability).
Here's the code. On Eclipse all work correctly, but when I export into jar file the app doesn't load because the resource "cccc.jpg" is not found.
I have already tried the getResource() but it didn't work as well.
// Load Background image
BufferedImage img = null;
try {
img = ImageIO.read(new File("cccc.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
// Set Background image
Image dimg = img.getScaledInstance(640, 480, Image.SCALE_SMOOTH);
ImageIcon imageIcon = new ImageIcon(dimg);
setContentPane(new JLabel(imageIcon));
like
BufferedImage bufferedImage = ImageIO.read(MyClass.class.getResource("/images/cccc.jpg"));
see: Loading image from a resource Can't read input file
Saying that your code works in ecplise is a hint that you have located the "cccc.jpg" file in the root of your project folder (this is the current folder when you run the application or a unit test within eclipse).
For having the file packed as resource into your jar: simply put it somewher into your source folder. If you have your source packages named like "my.app.main" you could place your image file directly beside the your "my" folder and load it by:
InputStream is = ClassLoader.getSystemResourceAsStream( "cccc.jpg" )
Or place it into a sub package (e.g. "my.app.images") and load it by:
InputStream is = ClassLoader.getSystemResourceAsStream( "my/app/images/cccc.jpg" )
BTW: ImageIO.read can also take an InputStream as parameter.
I solved the problem. Now everything is loaded correctly when I open the jar file.
Link to the picture of the problem solved

How do you refer to an image without using the file path?

Currently, the way I am referring to my images are by using the lines of code:
static ImageIcon img1 = new ImageIcon("src\\img1.png");
static ImageIcon img2 = new ImageIcon("src\\img2.png");
However, when I export this from eclipse into a .jar file, the picture does not show up. Is it possible to refer to the image without setting the complete file path (ex. ("C:\XXXX\XXXX\XXXX.img1.jpg)), because if I put the .jar on a flash drive, the complete file path would be useless?
On the package contains image create any empty class Example : ImageLoader
When you want to load image :
ImageIcon img1 = new ImageIcon(ImageLoader.class.getResource("img1.png"));
ImageLoader.class.getResource : will return the URL of ImageLoader class

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.

Why won't my BufferedImage show in the .jar file?

Alright, so I have created a Buffered Image to display on a JLabel. When I display the File location, it requires a src/ in front of the folder I want to access, or else an error will arise and I will not see the Buffered Image... I know that if I put the 'src/' in front of the resImg, the BufferedImage will not display outside of the IDE. Can anyone help?
This is the code that works inside of the IDE. When running outside from the .jar file, the image doesn't display.
static File f = new File("src/resImg/banner.png");
try {
banner = ImageIO.read(f);
picLabel = new JLabel(new ImageIcon(banner));
LabelPanel.add(picLabel);
} catch (IOException ex) {
Logger.getLogger(GameStart.class.getName()).log(Level.SEVERE, null, ex);
}
When the resource is in a jar file, it isn't a file on the local file system. You should use
URL imageSource = getClass().getResource("/resImg/banner.png");
if (imageSource == null) {
// Handle the resource being missing
}
picLabel = new JLabel(new ImageIcon(imageSource));
Here you're fetching the resources from the classloader, which will obviously have access to the files in your jar file.
If you want get image from jar, use smth like this:
URL imageurl = getClass().getResource("/images/imagename");
You cannot create a File object for a resource in the JAR file. Use this instead.
URL imgurl = this.getClass().getClassLoader().getResource("resImg/banner.png");
picLabel - new JLabel(new ImageIcon(imgurl));
Hope this helps.
You have to find a parent directory, the relativ path can start from like
workspace directory
directory the process starts
the relativ path is "realtiv" from where the java process is started from, so the path can change from time to time.
If the picture is placed in the jar, you can use
getClass().getResource("relativ jar path")
for finding the image. the jar itself knows, where it is placed.
Most probable cause is that your image is not packed inside jar file ( verify this with jar / zip tool ).
If image is there but does not display, it is because you trying to read this from file system relative to working directory ( and it is certainly not there )
So, proper solution would be to read image from class path:
Thread.currentThread().getContextClassloader().getResourceAsStream(... path to your image relative to jar root ---)

Categories