BufferedImage实例的数组:IIOException

时间:2014-03-28 07:11:14

标签: java file-io awt bufferedimage ioexception

我正在使用awt.Graphics创建一个菜单,当它绘制图片时,它给了我一个奇怪的IIOException。是因为该程序没有找到图像?以下是我的代码的使用方式。

public static BufferedImage[] img;

//this is where img[] gets initialized
for (int j = 0; j < worlds.length; j++) { //say worlds is about 3 or so
    String tmp = worlds.get(j);
    tmp = tmp.replace(".WORLD", "");
    img[j] = ImageIO.read(new File("C:\Users\Bryce\Desktop\" + tmp + ".png"));
}


//this is where it gets drawn
for (int i = 0; i < worlds.size(); i++) {
    String tmp = worlds.get(i);
    tmp = tmp.replace(".WORLD", "");
    try {
        for (int j = 0; j < worlds.size(); j++) {
            img[j] = ImageIO.read(new File("C:\Users\Bryce\Desktop\" + tmp + ".png"));
            g.drawImage(img[j], x + 10, y + (ySpace * i), 32, 32, null);
        }
    } catch(Exception ex) {
        ex.printStackTrace();
    }
}

它不断给我以下错误消息:

javax.imageio.IIOException: Can't read input file!

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

我实际上意识到我犯了一个愚蠢的错误,它以某种方式滑倒了我的视线。我忘了告诉BufferedImage []

有多大
img = new BufferedImage[worlds.size()];

它现在有效!