尽管文件看起来很好找到,但以下代码似乎不起作用。
images = new BufferedImage[32];
FileInputStream fis = null;
for (int i = 0; i < 32; i++) {
File file = new File("tiles\\"+i+".bmp");
if (!file.exists()){
System.out.println("File "+i+" failed");
}
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException e) {
System.err.println(e + "" + i);
}
try {
images[i] = ImageIO.read(fis);
} catch (IOException e) {
System.err.println(e + "" + i);
}
if (images[i] == null) {
System.out.println("Image "+i+" failed");
}
}
预先感谢您的任何帮助。
编辑:结果是我尝试 Graphics.drawImage(images[0]); ,它给了我一个空指针异常。这里的代码完成得很好。
编辑:更改按照建议移动 if(!file.exists()) ,并将文件包装在输入流中。
ImageIO.read(*...)
只会加载这些图像类型GIF, PNG, JPEG, BMP, and WBMP.
任何其他图像类型都将返回null
没有错误。
参考:http://docs.oracle.com/javase/tutorial/2d/images/loadimage.html
我确实意识到这不是特定原始问题的解决方案,但它是所提出问题的解决方案。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)