[英]Image won't load
我正在嘗試使用此方法加載圖像。 我在這里要我的形象:
private BufferedImage image = controller.loadImage("/paddle.png");
而loadImage方法是
public BufferedImage loadImage(String path) {
BufferedImage image = null;
try {
image = ImageIO.read(getClass().getResource(path));
} catch (Exception e) {
e.printStackTrace();
}
return image;
}
我也將“ res”添加到我的班級文件夾中,但似乎沒有任何效果。 我的項目資源管理器如下所示: http : //prntscr.com/hmar35編輯:我也嘗試了URL加載,但還是無法正常工作,URL加載的代碼為:
public BufferedImage loadImage(String path) {
BufferedImage image = null;
try {
URL link = Controller.class.getResource(path);
image = (BufferedImage) Toolkit.getDefaultToolkit().getImage(link);
} catch (Exception e) {
e.printStackTrace();
}
return image;
}
嘗試使用
ImageIO.read(getClass().getClassLoader().getResourceAsStream(path));
在您的情況下,path =“ paddle.png”。
如果將res文件夾設置為源文件夾,並且“ paddle.png”直接位於res文件夾內部,則無需在文件路徑中添加“ /”。
注意:另外,請確保刷新項目,以確保所有文件都被識別。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.