gpt4 book ai didi

java - Slick2D 加载图像不起作用

转载 作者:行者123 更新时间:2023-12-01 14:04:14 25 4
gpt4 key购买 nike

因此,我尝试使用 Slick2D 加载图像,但由于某种原因,当我尝试将图像绘制到屏幕上时,出现以下错误:

Sat Sep 28 16:37:59 NZST 2013 ERROR:null
java.lang.NullPointerException
at org.newdawn.slick.Graphics.drawImage(Graphics.java:1350)
at org.newdawn.slick.Graphics.drawImage(Graphics.java:1399)
at Game.render(Game.java:58)
at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:703)
at org.newdawn.slick.AppGameContainer.gameLoop(AppGameContainer.java:456)
at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:361)
at Game.main(Game.java:42)
Sat Sep 28 16:37:59 NZST 2013 ERROR:Game.render() failure - check the game code.
org.newdawn.slick.SlickException: Game.render() failure - check the game code.
at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:706)
at org.newdawn.slick.AppGameContainer.gameLoop(AppGameContainer.java:456)
at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:361)
at Game.main(Game.java:42)

我用它来加载我的图像:

import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

public class Textures {

static Image grass;
Game game;


public Textures() {
try {
grass = new Image("grass.jpg");
} catch (SlickException e) {
e.printStackTrace();
}
}

}

我用它来绘制我的图像:

for(int x = 0; x < app.getWidth(); x+=32) {
for(int y = 0; y < app.getHeight(); y+=32) {
g.drawImage(textures.grass, x, y);
}
}

最佳答案

如果您在尝试绘制图像时遇到NullPointerException,通常是因为图像加载器在指定位置找不到图像。如果您使用的是 Eclipse,请务必将图像存储在源文件夹中(例如名为“resources”)。这可确保在编译时将图像移至 bin 文件夹(可能需要刷新源文件夹)。

无论您的设置如何,请确保图像的路径正确。

关于java - Slick2D 加载图像不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19063348/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com