Slick2D加载图片无法正常工作

时间:2013-09-28 04:39:36

标签: java graphics slick2d

所以我正在尝试使用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);
    }
}

1 个答案:

答案 0 :(得分:1)

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

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