Java ImageIO.read(未知来源)

时间:2018-06-12 23:44:28

标签: java graphics javax.imageio sprite-sheet

所以我试图为游戏制作一个简单的图形界面,所以我制作了一张精灵表来配合它。但在我的班级< SpriteSheet.java>我正遇到这个错误:

Exception in thread "main" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source) at matrix.game.gfx.SpriteSheet.<init>(SpriteSheet.java:18)

这是&lt; SpriteSheet.java&gt;

package matrix.game.gfx;

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;

public class SpriteSheet {
    public String path;
    public int width;
    public int height;

    public int[] pixels;

    public SpriteSheet(String path) {
        BufferedImage image = null;
        try {
            image = ImageIO.read(SpriteSheet.class.getResourceAsStream(path));
        } catch (IOException e) {
            e.printStackTrace();
        }

        if(image == null) {
            return;
        }

        this.path = path;
        this.width = image.getWidth();
        this.height = image.getHeight();

        pixels = image.getRGB(0,0, width, height, null, 0, width);
        for (int i = 0; i < pixels.length; i++) {
            pixels[i] = (pixels[i] & 0xff)/64;
        }
        for (int i = 0; i < 8; i++) {
            System.out.println(pixels[i]);
        }
    }
}

3 个答案:

答案 0 :(得分:0)

您正在尝试执行未在类中声明的函数:

  

image = ImageIO.read(SpriteSheet.class.getResourceAsStream(path));

因为它失败try {并抛出它无法处理的异常。

答案 1 :(得分:0)

正如您在下面的代码中看到的那样, ImageIO 类的读取方法会在参数输入时抛出 IllegalArgumentException 为空。

public static BufferedImage read(InputStream input) throws IOException {
    if (input == null) {
        throw new IllegalArgumentException("input == null!");
    }

    ImageInputStream stream = createImageInputStream(input);
    BufferedImage bi = read(stream);
    if (bi == null) {
        stream.close();
    }
    return bi;
}

以下这一行试图在包matrix.game.gfx中找到资源。

SpriteSheet.class.getResourceAsStream(path)

如果您尝试从类包以外的目录访问文件,则应更改代码,如下所示:

SpriteSheet.class.getClassLoader().getResourceAsStream(fullPath);

在上面的代码中,类加载器将在类路径的根目录中开始搜索。

答案 2 :(得分:0)

这对我有用。

希望它也适合你。

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class SpriteSheet {
public String path;
public int width;
public int height;

public int[] pixels;

public SpriteSheet(String path) {
    BufferedImage image = null;

    //Adding a directory(FILE) object for the path specified
    File dir = new File(path);

    try {
        //Reading from the specifies directory
        image = ImageIO.read(dir);
    } catch (IOException e) {
        e.printStackTrace();
    }
    if(image == null) {
        return;
    }

    this.path = path;
    this.width = image.getWidth();
    this.height = image.getHeight();

    pixels = image.getRGB(0,0, width, height, null, 0, width);
    for (int i = 0; i < pixels.length; i++) {
        pixels[i] = (pixels[i] & 0xff)/64;
    }
    for (int i = 0; i < 8; i++) {
        System.out.println(pixels[i]);
    }
}
public static void main(String[] args) {
    new SpriteSheet(path/to/file);


}
}