Java project with image files

馋奶兔 提交于 2019-12-02 18:30:05

问题


I am working on a project in which I use image files. Where is the proper location to store the folder with the images? Inside src folder (created by netbeans) or outside? Also when I clean and build the project a dist folder is created with the jar file in it. Can the images folder be included in the jar or I have to copy them in the same folder with the jar if I want to run the program on another computer? Finally, I would like to know how to use the getResource when I want to refer to the image folder from the source code. Thanks in advance.


回答1:


Where is the proper location to store the folder with the images? Inside src folder (created by netbeans) or outside?

The answer is, it depends. If you want the images bundled within the Jar (AKA embedded resource), then they should be stored within the Netbeans src directory. If you want them stored externally (and you will then become responsible for ensuring that they are included with the Jar when you deploy it), then they should be stored outside the src directory.

Can the images folder be included in the jar or I have to copy them in the same folder with the jar if I want to run the program on another computer?

If the images are stored within the src directory, they are included automatically, if they stored externally, you will need to provide some custom ant code to perform the required actions you want.

Again, if they are stored externally (to the src directory), you become fully responsible for their management

Finally, I would like to know how to use the getResource when I want to refer to the image folder from the source code.

BufferedImage img = ImageIO.read(getClass().getResource("{path to images}/{image name}");

There are a number of resources/examples available which can expand on the individual requirements/differences in how to use getResource in different scenarios, but that's the basic idea



来源:https://stackoverflow.com/questions/41666768/java-project-with-image-files

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