我试着在3D发电机上展示一些发条。但我找不出这个错误:
javax.imageio.IIOException: Can't read input file!
at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1310)
at Texture.load(Texture.java:20)
at Texture.<init>(Texture.java:15)
at Texture.<clinit>(Texture.java:29)
at Game.<init>(Game.java:45)
at Game.main(Game.java:103)
javax.imageio.IIOException: Can't read input file!当我启动程序时,整个世界都会出现黑色块:启动程序后的屏幕快照
我真的很想做一个3D引擎,你可以在房间里到处走动。所以这阻止了我的渲染。
这是我的守则:
类别:纹理
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Texture {
public int[] pixels;
private String loc;
public final int SIZE;
public Texture(String location, int SIZE) {
loc = location;
this.SIZE = SIZE;
pixels = new int[SIZE * SIZE];
load();
}
private void load() {
try {
BufferedImage image = ImageIO.read(new File(loc));
int w = image.getWidth();
int h = image.getHeight();
image.getRGB(0, 0, w, h, pixels, 0, w);
} catch (IOException e) {
e.printStackTrace();
}
}
public static Texture wood = new Texture("block1.png", 64);
public static Texture brick = new Texture("block2.png", 64);
public static Texture bluestone = new Texture("block3.png", 64);
public static Texture stone = new Texture("block1.png", 64);}
班级游戏:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.util.ArrayList;
import javax.swing.JFrame;
public class Game extends JFrame implements Runnable{
private static final long serialVersionUID = 1L;
public int mapWidth = 15;
public int mapHeight = 15;
private Thread thread;
private boolean running;
private BufferedImage image;
public int[] pixels;
public ArrayList<Texture> textures;
public Camera camera;
public Screen screen;
public static int[][] map =
{
{1,1,1,1,1,1,1,1,2,2,2,2,2,2,2},
{1,0,0,0,0,0,0,0,2,0,0,0,0,0,2},
{1,0,3,3,3,3,3,0,0,0,0,0,0,0,2},
{1,0,3,0,0,0,3,0,2,0,0,0,0,0,2},
{1,0,3,0,0,0,3,0,2,2,2,0,2,2,2},
{1,0,3,0,0,0,3,0,2,0,0,0,0,0,2},
{1,0,3,3,0,3,3,0,2,0,0,0,0,0,2},
{1,0,0,0,0,0,0,0,2,0,0,0,0,0,2},
{1,1,1,1,1,1,1,1,4,4,4,0,4,4,4},
{1,0,0,0,0,0,1,4,0,0,0,0,0,0,4},
{1,0,0,0,0,0,1,4,0,0,0,0,0,0,4},
{1,0,0,0,0,0,1,4,0,3,3,3,3,0,4},
{1,0,0,0,0,0,1,4,0,3,3,3,3,0,4},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,4},
{1,1,1,1,1,1,1,4,4,4,4,4,4,4,4}
};
public Game() {
thread = new Thread(this);
image = new BufferedImage(640, 480, BufferedImage.TYPE_INT_RGB);
pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();
textures = new ArrayList<Texture>();
textures.add(Texture.wood);
textures.add(Texture.brick);
textures.add(Texture.bluestone);
textures.add(Texture.stone);
camera = new Camera(4.5, 4.5, 1, 0, 0, -.66);
screen = new Screen(map, mapWidth, mapHeight, textures, 640, 480);
addKeyListener(camera);
setSize(640, 480);
setResizable(false);
setTitle("3D Engine");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBackground(Color.black);
setLocationRelativeTo(null);
setVisible(true);
start();
}
private synchronized void start() {
running = true;
thread.start();
}
public synchronized void stop() {
running = false;
try {
thread.join();
} catch(InterruptedException e) {
e.printStackTrace();
}
}
public void render() {
BufferStrategy bs = getBufferStrategy();
if(bs == null) {
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
bs.show();
}
public void run() {
long lastTime = System.nanoTime();
final double ns = 1000000000.0 / 60.0;//60 times per second
double delta = 0;
requestFocus();
while(running) {
long now = System.nanoTime();
delta = delta + ((now-lastTime) / ns);
lastTime = now;
while (delta >= 1)//Make sure update is only happening 60 times a second
{
//handles all of the logic restricted time
screen.update(camera, pixels);
camera.update(map);
delta--;
}
render();//displays to the screen unrestricted time
}
}
public static void main(String [] args) {
Game game = new Game();
}}
发布于 2022-02-27 21:30:47
加载纹理有问题。评估如何处理根本原因(为什么不能加载图像?)后果(你的世界是黑色的):
根本原因
要找出无法加载映像的原因,一种方法是知道应用程序试图加载什么图像。修改load()方法如下:
private void load() {
File file = new File(loc);
try {
BufferedImage image = ImageIO.read(file);
int w = image.getWidth();
int h = image.getHeight();
image.getRGB(0, 0, w, h, pixels, 0, w);
} catch (IOException e) {
System.out.println("ERROR: Could not load "+file.getAbsolutePath());
e.printStackTrace();
}
}您可能会发现,您只是在错误的目录中搜索。或者访问权限设置不正确。或者文件内容已损坏。或者..。
影响
当加载方法失败时,它不会停止程序。相反,你的游戏一直在运行。然而,像素数组并不包含良好的数据。
确保在方法失败时有有意义的紧急默认值。您可以将“错误图像”加载到像素数组中。这样你至少不应该有一个黑色的窗户。
发布于 2022-03-01 08:45:21
异常堆栈跟踪非常直观,应该告诉您需要做什么。它指向ImageIO.read(File)代码中的一个异常,其如下所示:
public static BufferedImage read(File input) throws IOException {
// ...
if (!input.canRead()) {
throw new IIOException("Can't read input file!"); // <-- This is the one
}
// ...
}如您所见,问题在于canRead()返回false,并且根据该方法的API文档,该方法返回"__true当且仅当此抽象路径名指定的文件存在并可由应用程序读取;false否则“。
要么您的文件不存在于您试图读取的位置,要么您的文件具有访问保护,从而禁止应用程序读取它。就这么简单。
我相信在catch块中打印new File(loc).getAbsolutePath()将帮助您调试实际位置以及为什么不工作。最有可能的是,您不应该使用File,而应该在应用程序中使用嵌入式资源。
https://stackoverflow.com/questions/71288454
复制相似问题