I am having a folder structure like this.
Project/src/folder/folder/folder/xyz.java
/img/background.jpg
And I want to put the background.jpg into a JPanel in xyz.java
. Just for the Background.
And I've done it with
private ImageIcon createImageIcon(String path, String description) {
java.net.URL imgURL = getClass().getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL, description);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
But the problem here is that the image has to be in the same folder than the xyz.java
I searched a bit and found that this could be solved with getResourceAsStream("path")
but I can't bring up enough knowlege to solve this.
This is important for me because i want to run my programm as a *.jar file on different operating systems.
My problem is that i don't understand how getRessourceAsStream("path") should work different. I found no tutorial which fits for me. It seems like the most people use it for different .jar开发者_JS百科 files or Applets. And I don't know what a Stream can do, where getRessourceAsStream("path") starts (means how do i have to write the path). There are so much lacks of knowlage...
Edit: I found a solution where I don't have to give a total path.
img = ImageIO.read(getClass().getResource("../../../../img/bg_1.jpg"));
I didn't tested it, but it should also run on the other systems. But the Problem is, that i can only start my program in eclipse. When I export it into a *.jar File, it happens fine but It doesn't load the picture. And it shrinks the window to a minimum even though I've setPreferredSize.
So i should maybe work the Stream solution out. But now it is tome for bed.
Edit: The relative path works fine when I put the images into a package. Not what I really wanted, but after 6h guessing about this tiny problem I had to bring some fishys to swim through my ocean...
Since you haven't replied to my comment, I'll ask some questions and make suggestions as an answer:
Is all enclosed in a Jar file? Have you tried passing the path to the image or the image directory on the command line and read it in as a command line parameter? And again, why are you using ImageIcons when you're displaying an Image not an ImageIcon?
Edit 1
regarding your comments:
I am using an ImageIcon because ##java on freenode.net told me it fits for my habits.
I have no idea of who ##java is nor what "habits" you are fitting, but the current fact of the situation is you are using an Image and an ImageIcon has nothing to do with this, so really, get rid of it. Simplify and only use what you need.
My First solution was something like that: img = ImageIO.read(new File("path")); But it needs also the full path and not some URL path which I know from html.
The path requirements for ImageIO.read are no different from what you're trying to do. It is a fine addition to your solution.
The Jar file sould run without any extras on different OS. I just need one of it.
You still haven't told us if your images are in a jar or not, and where the images are in relation to your class files, for this is the key to using resources -- loading the resources relative to the location of the class files.
Loading Images Using getResource
Using an ImageIcon on a JLabel is great when you display the image at its actual size.
If you need to (dynamically) scale the image or anything (like that) then you need to do custom painting with the image.
精彩评论