I compiled my app with image objects, which located in my computer
ImageIcon background = new ImageIcon("D:\\pictures\\background.png")
It seems to be unseen , as soon as I change location of my pictures. Then, I copied those images in app folder and I don’t know how to set path for them. The Idea is to set path of the images for JavaFx application, which should locate at the same folder. And when my app is in someone’s computer,system should know that the pictures inside application folder , wherever this folder locates.
Thanks in advance
Here I’ve made some changes based on advice.
I added my images directly to the project folder by creating new packages resources.images and dragging images in it.
Then I created BufferedImage img; and set path for every image I needed
Image bg;
Image fg;
Image object;
img = ImageIO.read(getClass().getResource(“resources/images/img1.png”));
bg = img;
img = ImageIO.read(getClass().getResource(“resources/images/img2.png”));
fg = img;
And so on ...
Then I drew these images through paintComponent.
And everything seems to be fine, it works after compiling in my environment as it should , but as soon as I create JavaFx app and run .exe file , images don’t display, just empty frame.
:( help me deal with this guys. Thx
If I understand the question correctly, you want to use images that you define and then use those images on whatever computer your app is running. If that is what you are trying to do, I suggest the following:
Create a package called for instance 'images' in one of your source folders.
Access it with
ImageIO.read(YourMainClass.class.getClassLoader().getResource("images/imagename.png"));
(ImageIO.read is just an example of a way to load an image, use whatever you like.)
If you export your app correctly, the images should be put into the jar and be accessible for your application on any device.
Related
I am busy with a java desktop application in intellij. I am struggling to get a image to display in a JLabel
Here is my current code (in a class extending JPanel):
icon = new ImageIcon(getClass().getResource("resources/icon.png"));
lblIcon.setIcon(icon);
Here is a picture of my project structure:
The image is in the resources directory and the screen in in the screens\jpanel.java directory
If I rememeber correctly, IntelliJ automatically adds the contents of the folder marked as "resources root" to the root of your compiled project.
getClass().getResource("...") expects a path relative to your classloader, thus you simply need to provide the name of your image in this case:
icon = new ImageIcon(getClass().getResource("/icon.png"));
lblIcon.setIcon(icon);
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.
Trying to read an image into java, currently what I have to do is this:
Image img = new Image("file:E:/Javaworkspace/Project/src/resource/image.png");
However, I am not the only one going to be working on that project and this path works only in my machine. I did try
Image img = new Image("file:/resource/image.png")
but this leads to FileNotFound. I don't know what this thing is called in English, I hope you understand what I am trying to convey here.
EDIT:
I added the folder "resource" via Build Path now and am trying to get input stream as such:
ImageView imgView = new ImageView(new Image(this.getClass().getResourceAsStream("/resource/image.png")));
Needless to say, I get a NullPointerException, which according to the documentation, occurs when the path doesn't exist. How can it not exist after I specifically created it via Build Path, it exists in CLASSPATH. (Yes, the file is there too or can I not simply copy it into the folder? )
Place the image inside the project folder rather than using file system to access the file. Then you can use this:
Image image = ImageIO.read(getClass().getResourceAsStream("/resource/image.png"));
to get the image file where ever required by anyone.
I believe you can always try user.home but then if one person updates an image or adds one it won't update for you.
Image img = this.getClass().getResource("/resource/image.png");
Probably messed up parentheses
Also make sure you have folder "resource" in right folder (most likely same folder src and out are in
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.
I am developing a little game where I use some pictures for the sprites etc etc.
It works just fine when I load it from the disc like this
Image image = new Image("C:\\AppleMan.png");
but how can I load it from a foloder within the project. I am using eclipse as IDE and the language is Java :)
I took a screenshot of a sample project so you can see how I have importet the picture
So I want to load the picture from that resource folder like this pseudo code
Image image = getResource("Resources/AppleMan.png");
but that simply doesn't work.
Any help appreciated :)
1) You should add Resources folder to classpath
2) You should locate file absolutely, i.e. "/Resources/AppleMan.png"
P.S.
3) Sorry, also note that getResource returns URL: http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html#getResource%28java.lang.String
You're passing a relative path when you were passing a absolute path before. Add the path of the Eclipse workspace.
For example, if your workspace is at C:\Workspace, you need to put
Image image = getResource("C:\\Workspace\\HowToGetResources\\Resources\\AppleMan.png");