我试图将图像加载到我刚刚开始创建的游戏中,但是当我这样做时,很难找到我的图像,如下所示:
`Exception in thread "Thread-0" java.lang.IllegalArgumentException: input == null!
at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1400)
at me.louisnathan.game.gfx.ImageLoader.loadImage(ImageLoader.java:12)
at me.louisnathan.game.Game.init(Game.java:32)
at me.louisnathan.game.Game.run(Game.java:60)
at java.base/java.lang.Thread.run(Thread.java:834)`
图像位于名为textures的文件夹中,该文件夹位于另一个名为res的文件夹中。 res文件夹已添加到构建路径。我的图片叫做image.png。图像是在photoshop中制作的。
我的代码:
游戏类
public class Game implements Runnable {
public int width, height;
public String title;
private Display display;
private Thread thread;
private BufferStrategy bs;
private Graphics g;
private BufferedImage image;
private boolean isRunning = false;
public Game(String title, int width, int height) {
this.width = width;
this.height = height;
this.title = title;
}
private void init() {
display = new Display(title, width, height);
image = ImageLoader.loadImage("/textures/image.png");
}
private void tick() {
}
private void render() {
bs = display.getCanvas().getBufferStrategy();
if (bs == null) {
display.getCanvas().createBufferStrategy(3);
return;
}
g = bs.getDrawGraphics();
// Clearing Screen
g.clearRect(0, 0, width, height);
// Start Rendering
// End Rendering
bs.show();
g.dispose();
}
public void run() {
init();
while (isRunning) {
tick();
render();
}
}
public synchronized void start() {
if (isRunning)
return;
isRunning = true;
thread = new Thread(this);
thread.start();
}
public synchronized void stop() {
if (!isRunning)
return;
isRunning = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
ImageLoader类
public class ImageLoader {
public static BufferedImage loadImage(String path) {
try {
return ImageIO.read(ImageLoader.class.getResource(path));
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
return null;
}
}
答案 0 :(得分:0)
此声明
ImageLoader.class.getResource(path)
返回null
。
如果image.png
目录中有文件textures
,请检查生成的JAR(可以将其作为任何ZIP存档来对其进行操作)或类输出目录(如果从IDE运行)。