1

在运行代码以显示 pacman 级别背景时,我遇到了图像和 pacman sprite 未加载的问题。

例外:

Exception in thread "Thread-2" java.lang.IllegalArgumentException: input == null!
    at javax.imageio.ImageIO.read(ImageIO.java:1388)
    at org.pacman.Pacman.init(Pacman.java:51)
    at org.game.engine.GameLoop.run(GameLoop.java:26)

GameLoop 的第 26 行很简单game.init();

这是课程:

public class Pacman extends Game {

    public static void main(String[] args) {
        GameApplication.start(new Pacman());
    }

    private BufferedImage sheet;
    private int frame;

    public Pacman() {
        title = "Pacman";
        width = 176;
        height = 258;
        frame = 0;
    }

    @Override
    public void draw(Graphics g) {
        if (sheet != null) {
            g.drawImage(sheet, -2, 0, null);
            //draw ms pacman
            g.drawImage(sheet.getSubimage(628+(frame/3)*(15+2), 33, 15, 15), 80, 105, null);
        }
    }

    @Override
    public void init() {
        try {
            sheet = ImageIO.read(Pacman.class.getResource("/images/pacman.png"));
        } catch (IOException ex) {
            Logger.getLogger(Pacman.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void update() {
        frame++;
        if (frame > 3*2) {
            frame = 0;
        }
    }
}
4

2 回答 2

3

在 org.pacman.Pacman.init(Pacman.java:51)

所以看第 51 行:

sheet = ImageIO.read(Pacman.class.getResource("/images/pacman.png"));

Pacman.class.getResource("/images/pacman.png")也是,所以null看起来它没有加载图像。

于 2012-10-22T21:06:40.307 回答
2

尝试使用 调用ImageIO.readgetResourceAsStreamInputStreamnull事先检查。你会发现确实如此null。这意味着无法在该 URI 上检索图像。

于 2012-10-22T21:06:10.563 回答