I have a problem when exporting my java project. It cannot find the path of my image.
File Project
src
config
image
copy.png
This is where my image is located
String image1 = "image/copy.png";
shell.setImage(new Image(display, image1));
It works before i export but when i export it and update my program it gives me an error. I tried also to use InputStream but it gives me null.
With the specified class, org.eclipse.swt.graphics.Image, you will want to create the Image with method Image(Device device, InputStream stream) and use getClass().getResourceAsStream( image1 ) to supply the inputStream containing the file. You probably need "/" at the start of your path.
Create image with this method work well.
new Image(device, getClass().getResourceAsStream(localImagePath));
Related
I am trying to load an image to display on the screen (just to get an idea of how to do it).
The problem is that when the program tries to load "apple.png" (which is saved on my desktop), it cannot find the image - Where do image files need to be stored in order for them to be found? Here is my loading method:
private void loadImage() {
ImageIcon appleIcon = new ImageIcon("apple.png");
Image appleImage = appleIcon.getImage();
}
If you want to reach it from the desktop, you should use the complete path. The easiest way to handle resources would be to create a folder in you java project, which you can access via "folderName/fileName.example".
Using the full path is an option
C:\filefolder\file.jpg
To answer your question though
Wherever your java file is is where it's going to think "home is" make yourself a java workspace to put your java source files in and inside it create a folder to put any assets you want in that way you will simply be able to call "apple.jpg"
if you want use the file name only, the file path ("apple.png")is relative to the source folder. so you have to place it in there.
you could also use absolute the file path to your desktop (sthg like "C:\Users\your.name\Desktop")
From the javadoc:
Creates an ImageIcon from the specified file. The image will be preloaded by using MediaTracker to monitor the loading state of the image. The specified String can be a file name or a file path. When specifying a path, use the Internet-standard forward-slash ("/") as a separator. (The string is converted to an URL, so the forward-slash works on all systems.) For example, specify:
new ImageIcon("images/myImage.gif")
As #Shriram mentioned, when you only specify the filename (with extension), it will search for that file in the current directory.
Hint: There exists an constructor overload which takes an URL as argument.
I've built a Java application that loads an image at runtime. The location of the image is fixed relative to the project.
I would like to be able to run the program from both within Eclipse and the command line and for it to load the image correctly. However, I can only do one or the other but not both. This seems like such a trivial thing to want to do but I can't find out how to do it.
The project is set up so that it creates a bin directory for the output and puts the image in a resources sub-folder. This is fine when running from the command line as I can write my code to look in that sub folder for the file.
But when I run the program from within eclipse the current working directory is different.
What am I missing?
TIA
Update - adding some code
This is what I had originally:
BufferedImage awtImage = ImageIO.read(new File(System.getProperty("user.dir") + "/resources/image-name.png"));
Following the advice in the comments I am trying to use getResourceAsStream but I don't know what to pass to the File constructor.
InputStream temp = MyClass.class.getResourceAsStream("resources/image-name.png");
BufferedImage awtImage = ImageIO.read(new File(???));
The resource is being found because temp is not null.
I think there's 2 solutions.
1) you specify an absolute path
2) your image is in the classpath so you could load it via :
YouClass.class.getResourceAsStream("YourImg.png");
The working directory, if that's really what you mean, is not a great place to load an image from. It appears that you have an image that you would distribute with your finished program so that the program could use it. In that case, I suggest that you use Class.getResourceAsStream(), and put the image in the directory with (or near) that class.
EDIT:
Here is code I used in one of my programs for a similar purpose:
ImageIcon expandedIcon = null;
// ...
expandedIcon = new ImageIcon(TreeIcon.class.getResource("images/Expanded.png"));
The ImageIcon class is part of Swing; I don't know if you're using that, but this should serve to show you the idea. The getResource() method takes a URL; again, you might need something a little different. But this shows the pathname relative to the path of the class on which the method is called, so if TreeIcon is in x/y/z/icons, the PNG file needs to be in x/y/z/icons/images, wherever that is on that computer.
TreeIcon is a class of mine, and its internals will not help you, so I'm not posting them. All it's doing here is providing a location for the PNG file I'm loading into an ImageIcon instance.
In addition to working on a disk with a directory structure, this also works in a jar file (which is a common way to distribute a java program or library). The jar file is just a zip file, and each file in the jar/zip file has its directory associated with it, so the image can be in the jar in the correct directory just as the java classes are in their directories.
getResourceAsStream() returns a stream; if you want to use that byte stream to load as an image, find a class that converts an stream to something your image class can use as a constructor or in a load method and hook them up. This is a common thing to have to figure out with Java i/o, unfortunately there is no cookbook way to do it across all images and situations, so we can't just tell you what it is.
EDIT 2:
As from the comment, try:
ImageIO.read(new File(MyClass.class.getResource("resources/image-name.png");
I set up my Eclipse projects like this.
The input directory is added to the classpath (JavaBuildPath in Eclipse).
Finally, you access the image and / or text files like this.
private BufferedImage getIconImage() {
try {
return ImageIO.read(getClass().getResourceAsStream(
"/StockMarket.png"));
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
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'm working on a Java program using Eclipse. Right now, I have an src folder that contains 2 packages: memory.views and memory.resources.
In the memory.views package, I have my Main.java file. In the memory.resources package, I have my .txt file and .gif file.
Within the program, I have no problem accessing (and manipulating) the .txt file by using the path /memory/resources/name.txt. However, when I do the same with the .gif file using the code below, I get no result:
ImageIcon icon = new ImageIcon("/memory/resources/name.gif");
There's no error produced. The only effect is that I see no image when the program is running.
I've tried also writing the following, but none worked:
ImageIcon icon = new ImageIcon("/resources/name.gif"); <br>
ImageIcon icon = new ImageIcon("name.gif");
Now, just so nobody says that it's the .gif file's fault, I've actually entered in the full Finder path (I'm using a Mac) and that worked perfectly:
ImageIcon icon = new ImageIcon("/Users/[...]/src/memory/resources/name.gif");
However, I don't want to do the full path, because if I export the program and run it on another computer, then the non-programming computer won't display the image either.
So, right now, I don't even know what the problem is. The .gif file works sometimes, but not when it's accessed via the same path as the .txt file, which works all the time. I tried looking here (Cannot access resource by using the getResources() method), but it seems like he had a slightly different problem from me.
You can use
URL url = ClassLoader.getSystemResource("name.gif");
ImageIcon icon = new ImageIcon(url);
provided that your name.gif file ends up in classpath after compilation/build.
What is this ImageIcon class? Is it your own code?
Try Thread.currentThread().getContextClassLoader().getResourceAsStream("/memory/resources/name.gif");
See if this works...
File file = new File("/memory/resources/name.gif");
ImageIcon icon = new ImageIcon(file.getAbsolutePath());