简体   繁体   中英

How can i add and access a folder of images to my java program (netbeans)

I have created a simple java game. I want to put this game on a flash drive and play it on other computers. How can i make my Images folder part of the java package and access them from the program instead of using filepaths that will only work for my computer "C:\\Users\\Me\\Desktop\\mypic.png"

this might work

import javax.swing.filechooser.*;

FileSystemView.getFileSystemView().getRoots()[0];

or

FileSystemView.getFileSystemView().getHomeDirectory();

or

System.getProperty("user.dir");

Found a Solution, Two things i was doing wrong, I added my library to the wrong part of my project. At first i was adding my image package under source packages and it was named only "images". When I instead added my package to source packages with the name "myproject.images" (myproject = whatever your project name is) i was able to access my images using this code then -

try{
Image image = ImageIO.read(getClass().getResource("images/water.jpg"));
}catch(Exception e){}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM