How to crate a source folder with images and then use them in that same project?
I have an "image" folder in src and I indicated that it is a source folder via build-path. I tried like a million ways to put those images on my JFrame-JLabels but none of them seems to work(it does with absolute path).What is the best way to do that?Relative path needed...
Make a resource folder. Call it "res" or whatever you want. Or you can make a method which sets directory already to folder you want and you just input file name
img = imageName.png
buffImg = ImageIO.read(new File("C:\Project\src\images\" + img));
Related
I want to read a number of images which are on a folder called images at the same folder with my source code files. The path I use to read each image, is ..\images\imageX.jpg. But it does not recognize it. I am using Netbeans.
This will get you the path to the src/ directory in your web application:
String path = YourClass.class.getProtectionDomain().getCodeSource().getLocation().getPath();
You can then append anything you need to navigate to your images directory. So assuming that the images directory were located in a folder inside the src directory, the following should do the trick:
File imageFile = new File(path + "images/imageX.jpg");
Keep in mind that getPath() will return with a trailing forward slash at the end, so you don't need to include one when further resolving the path to your files.
I created a java app and I put some images in it and even gave it an image icon as the desktop image, but when i made it a jar file, and put it on another pc, all images were gone. This is the image path :
File imageFile = new File("C:\\Users\\Favour's Computer\\workspace\\Physics Calculator\\src\\res\\icon.jpg");
I checked it online and i found out that the problem is that i got the file through the C:\\ directory, they said the image file should look like this :
File imageFile = new File("res/icon.jpg");
I tried this but it didnt work, I kept getting an error message like : file not found
This is my full code :
BufferedImage image = null;
try {
File imageFile = new File("C:\\Users\\Favour's Computer\\workspace\\Physics Calculator\\src\\res\\icon.jpg");
image = ImageIO.read(imageFile);
} catch(IOException e) {
e.printStackTrace();
}
setIconImage(image);
Please, i have been trying to solve this for weeks, do anyone know how i can solve this, please if you do, please help
The images should not be loaded from the file system, but should be bundled into the app, inside your jar.
If you put the image foo.png inside the jar, under the package com.bar.resources for example, you simply need to use
InputStream in = getClass().getResourceAsStream("/com/bar/resources/foo.png")
to load the image as an input stream.
That will use the class loader to load the image. So, during development, if you're using a standard IDE project, you just need to put the image file in the appropriate package in your source directory: the IDE will "compile" the file by copying it to the same directory as the generated .class files.
If you're using a standard Maven/Gradle project, then it needs to be put in the appropriate package under src/main/resources.
Problem is you dont have res folder in your project, first of create a folder a "res" by Right Clicking on project and place the image icon.jpg image in that "res". and
use : `File imageFile = new File("res/icon.jpg");`// To retrieve image to your project.
You cannot retrieve the image from your local system, because it is not attached with your projects workspace.
There are two options....
1)Either make res a "source folder" . You will know this if you are using Eclipse.
Then you can use like
ImageIO.read(new File("res/icon.jpg"));
2) If res is a normal folder, you will have to use. In this case res will be considered as a package
ImageIO.read(new File("src/res/icon.jpg"));
I'm trying to load an image file.
File imageCheck = new File("Users/me/Documents/workspace/ChineseChess/src/RCar.gif");
if (imageCheck.exists())
System.out.println("Image file found!");
else
System.out.println("Image file not found! :(");
button.setIcon(new ImageIcon("Users/me/Documents/workspace/ChineseChess/src/RCar.gif"));
Should I put RCar.gif in the src or bin folder of an eclipse project?
How would I refer to it without the explicit path? (I'm using a mac. I believe it should be something like ~/RCar.gif but am having trouble finding the correct syntax online.
Thanks!
If you want to use relative paths, they are going to be relative to the project's root, or when its deployed, the directory that the jar file resides in. You can use src/RCar.gif as your filename.
It would be better if you created a separate folder for resources, then address them with new File("res/RCar.gif"); Another option is to put them in the src folder and address them using the classloader.
If I recall correctly, Eclipse requires all resources to be placed in the resources directory. These will be automatically bundled with your application when you export it.
At runtime, you would simply use Class#getResource("/path/to/your/resource") where the path is the location from the "resource" folder
You can load the icon quite easily when it is in the resource folder of the class it belongs to at runtime.
See http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getResource%28java.lang.String%29
Within the class your.packagename.YourClassName you could write the following:
String folderName = YourClassName.class.getName().replace('.', '/' );
String urlString = "/"+folderName+"/RCar.gif";
URL fileUri = YourClassName.class.getResource(urlString);
button.setIcon(new ImageIcon(fileUri));
The urlString would be /your/packagename/YourClassName/RCar.gif. Of course this is also where icon should be at runtime.
HTH
yes...it takes the relative path.
public static final String FILE_PATH = "src/test/resources/car.png";
Hey is it possible to get an image from file without using the full file location in java? My code below will only work with the full file path:
Icon icon = new ImageIcon("/Users/MyMac/Documents/Project/Software/Project/src/UI/Images/default_pic.png");
Is it possible to use the file path as such?:
Icon icon = new ImageIcon("/src/UI/Images/default_pic.png");
"/src/UI/Images/default_pic.png" is an absolute path, so it will look for a src directory in the root directory, then a UI subdirectory in it, etc. Not what you want.
You can use a relative path such as "src/UI/Images/default_pic.png" (notice it doesn't start with a "/"), but as its name says, it is relative to the current directory. So it will work if your current directory is /Users/MyMac/Documents/Project/Software/Project (or any directory that contains the file in the same subpath), otherwise it won't.
Finally, another way is to access the file through the classpath. Considering that the project could be packed in a jar file, the image file might not be a separate file on the disk, but you can still get a URL or InputStream to access it. Search for getResource and getResourceAsStream in Class and ClassLoader.
When running a Java app from eclipse my ImageIcon shows up just fine.
But after creating a jar the path to the image obviously gets screwed up.
Is there a way to extract an image from the jar at runtime so I can then open it up? Or, is there a better way to do this?
I'd like to distribute a single jar file if possible.
To create an ImageIcon from an image file within the same jars your code is loaded:
new javax.swing.ImageIcon(getClass().getResource("myimage.jpeg"))
Class.getResource returns a URL of a resource (or null!). ImageIcon has a constructors that load from a URL.
To construct a URL for a resource in a jar not on your "classpath", see the documentation for java.net.JarURLConnection.
You can try something like:
InputStream stream = this.getClass().getClassLoader().getResourceAsStream("/images/image.jpg");
In your JAR file, you might have a directory structure of:
MyJAR.jar
- com (class files in here)
- images
----image.jpg
This is working for me to load and set the content pane background image:
jar (or build path) contains:
- com
- img
---- bg.png
java contains:
JFrame f = new JFrame("Testing load resource from jar");
try {
BufferedImage bg = ImageIO.read(getClass().getResource("/img/bg.png"));
f.setContentPane(new ImagePanel(bg));
} catch (IOException e) {
e.printStackTrace();
}
Tested and working in both jar and unjarred (is that the technical term) execution.
BTW getClass().getClassLoader().getResourceAsStream("/img/bg.png") - which I tried first - returned me a null InputStream.
In netbeans 8.1 what I've done is to include the folder of icons and other images called Resources inside the src folder in the project file. So whenever i build Jar file the folder is included there.The file tree should be like this:
src (Java files in source packges are here)
** PACKAGE YOU NAMED IN PROJECT**
file.java
Resources
image.jpg
The code should be like:
jToggleButton1.setIcon(new javax.swing.ImageIcon(this.getClass().getResource("/resources/image.jpg")));
Load image in from Jar file during run time is the same as loading image when executed from IDE e.g netbeans the difference is that when loading image from JAR file the path must be correct and its case sensitive (very important).
This works for me
image1 = new javax.swing.ImageIcon(getClass().getResource("/Pictures/firstgame/habitat1.jpg"));
img = image1.getImage().getScaledInstance(lblhabitat1.getWidth(), lblhabitat1.getHeight(), Image.SCALE_SMOOTH);
lblhabitat1.setIcon(new ImageIcon(img));
if p in "/Pictures/firstgame/habitat1.jpg" is in lower case it wont work. check spaces, cases and spelling