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
Related
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());
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);
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
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.
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");