Java无法从文件夹中读取图像

时间:2014-11-30 13:42:31

标签: java class resources

我正在尝试从资源文件夹中读取图像 这是遗传的文件夹

/src

-/classes
--Sprite.java
--Main.java
--/chars
---George.java

-/res
--george.png

Sprite.java(用于读取精灵表)

public class Sprite {

    private static BufferedImage spriteSheet;
    private static final int TILE_SIZE = 32;

    public static BufferedImage loadSprite(URL path) {

        Image sprite = null;
        sprite = Toolkit.getDefaultToolkit().getImage(path);

        return ((ToolkitImage)sprite).getBufferedImage();
    }

    public static BufferedImage getSprite(URL path, int xGrid, int yGrid) {


        spriteSheet = loadSprite(path);

        return spriteSheet.getSubimage(xGrid * TILE_SIZE, yGrid * TILE_SIZE, TILE_SIZE, TILE_SIZE);
    }

}

George.java

public class George {

    public George() {
        URL path = getClass().getResource("/res/george.png");
        BufferedImage Stand = Sprite.getSprite(path, 2, 1);
    }
}

我在Exception in thread "main" java.lang.NullPointerExceptionreturn spriteSheet.getSubimage(xGrid * TILE_SIZE, yGrid * TILE_SIZE, TILE_SIZE, TILE_SIZE);

收到错误BufferedImage Stand = Sprite.getSprite(path, 2, 1);

我似乎无法正常工作,请在这里帮助我。

0 个答案:

没有答案