【发布时间】:2017-11-24 16:23:03
【问题描述】:
这是我正在使用的代码:
/**
* This method loads an image
*
* @param path - path of the image
*
*
*/
public static BufferedImage loadImage(String path) {
try {
return ImageIO.read(ImageLoader.class.getClass().getResourceAsStream(path));
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
return null;
}
这是错误:
线程“Thread-2”中的异常 java.lang.IllegalArgumentException: 输入 == null!
在 javax.imageio.ImageIO.read(未知来源)
在 io.deadspace.graphics.ImageLoader.loadImage(ImageLoader.java:19)
在 io.deadspace.graphics.asset.assets.EntityAssets.initEntityAssets(EntityAssets.java:15)
在 io.deadspace.graphics.asset.Assets.init(Assets.java:37)
在 io.deadspace.Game.init(Game.java:73)
在 io.deadspace.Game.run(Game.java:127)
在 java.lang.Thread.run(Unknown Source)
我最近重做了将图像加载到游戏中的方式,以前效果很好,但现在,它不再有效了。我已经尝试过 getResource 和 getResourceAsStream。
以下是我如何加载图像的一些示例:
public void initEntityAssets() {
sheet = new SpriteSheet(ImageLoader.loadImage("res/textures/sheet.png"));
wood = sheet.crop(width, height, width, height);
tree = sheet.crop(0, 0, width, height * 2);
rock = sheet.crop(0, height * 2, width, height);
rockDropItem = sheet.crop(1, height * 2, width, height);
}
public void initHotbarAssets() {
sheet = new SpriteSheet(ImageLoader.loadImage("res/textures/sheet.png"));
hotbar = sheet.crop(0, height * 4, width, height);
hotbar_selected = sheet.crop(0, height * 5, width, height);
}
【问题讨论】:
-
还可以考虑加载一次图像并重复使用它。
标签: java