ImageIcon Loading in Java

南笙酒味 提交于 2019-12-10 11:34:30

问题


I have searched Google, SE, and Oracle's documentation for information on how to do this, but I can't seem to make it work.

I have found several posts regarding whether to use getResource() or getResourceAsStream() and how to format the file name, but none that I can apply to my situation.

My directory structure looks like this (with some extra levels in the resource folder):

Root Folder
|
|_____ bin (compiled .class files go here)
|
|_____ src
|         |
|         |_____ packageA
|         |         |
|         |         |_____ packageAa
|         |                   |
|         |                   |_____ images.java
|         |                   |
|         |                   |_____ resources
|         |                             |
|         |                             |_____ icon1.gif
|         |
|         |_____ packageB
|         |
|         ... (More packages with sub-packages)
|
|_____ resources (should it go here?)
          |
          |_____ icon2.gif

In my actual program I have 107 gif images with file names that can be derived from object values. These images are currently located in both resource folders because I have been playing around with both to try to get one to work.

My current code tries this:

String imageFileName = getFileName(obj);  // returns a string such as "/resources/cardImages/acespades.gif"
URL imageURL = getClass().getClassLoader().getResource(imageFileName);
ImageIcon icon = new ImageIcon(imageURL);

I keep getting a null pointer exception regardless of leading backslashes.

How should I format the file name and or create a URL object to get the images?

Note: I am using Sublime Text 3 with Javatar to compile my code. The folder structure (the bin folder) is created by Javatar. My "Source folder" is set to src and the "root folder" shown above is my flash drive.

====================================================
Edit:

I tried Peter's suggestions and these are the results:

URL imageURL = getClass().getResource("resources/cardImages/back1.gif");
System.out.println(imageURL);
ImageIcon icon = new ImageIcon("resources/cardImages/back1.gif");

The println statement prints out null, but the ImageIcon does correctly display the image. I understand it this loads the image from the folder next to the .java file.

URL imageURL = getClass().getResource("/resources/cardImages/back1.gif");
System.out.println(imageURL);
ImageIcon icon = new ImageIcon("/resources/cardImages/back1.gif");

The println still prints null, but now the image is not displayed. I copied and pasted the entire resource folder to make sure it was identical in both locations.


回答1:


When loading resources over a class, the path is relative to the package path, unless you have a leading slash /.

As for your icon1.gif, using

URL url = getClass().getResource("resources/icon1.gif");

should return the correct URL.

I recommend keeping the Java sources and resources separate. If resources is a source folder (whose output folder is the root of bin/the JAR), use:

URL url = getClass().getResource("/icon2.gif");

(note the leading slash).


EDIT (question updated):
Corrected the second URL (resources is a source folder, and icon2.gif lies in its root).
I just verified it in Eclipse, and I get the correct (non-null) URLs for both variants. Using the URLs in ImageIcons also gives the correct results, the images are shown.

  • Make sure that resources is a source folder. Not all project files land in the output folder (usually bin) and later the JAR.
  • Check your output folder to verify it contains the compiled classes as well as the resources.



来源:https://stackoverflow.com/questions/29307931/imageicon-loading-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!