Have a look at MKYong's tutorial. It shows you where to put your image.
If you want the image to be loaded as "resource", you have to put it in the resources folder. You project structure would be like this:
MyProject
+--src
+--main
+--java
| +-com
| +--me
| +--Main.java
+--resources
+--pepsi.jpg
and in your Main
class you execute that snippet:
try {
Image img= ImageIO.read(Main.class.getClassLoader().getResourceAsStream("pepsi.jpg"));
System.out.println(img.getWidth(null)); //this is just a test, when it prints out the width of your image, you have the right file loaded
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…