3D 图片无法加载 Java
我尝试在 3D 生成器中展示一些 Blockarts。但我无法弄清楚这个错误:
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 引擎,你可以在其中走动在房间里。所以这个块不会为我渲染。
这是我的代码:
Class:Texture
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);
}
Class Game:
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();
}
}
I tried to show some Blockarts in a 3D Generator. But i cant figure out this error:
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!
When i start the Programm, the whole World appears with Black Blocks:
Screen shot after Starting Programm
Im doing want to do a 3D engine, where you can just walk around in rooms. So this Blocks arent rendering for me.
This is my Code:
Class:Texture
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);
}
Class Game:
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您加载纹理时遇到问题。评估如何处理根本原因(为什么无法加载图像?)和后果(你的世界是黑色的):
根本原因
要找出图像无法加载的原因,一种措施是了解应用程序是什么图像尝试加载。像这样修改 load() 方法:
您可能会发现您正在错误的目录中搜索。或者访问权限设置不正确。或者文件内容已损坏。或者...
影响
当您的加载方法失败时,它不会停止程序。相反,您的游戏会继续运行。然而,像素数组不包含好的数据。
确保您有有意义的紧急默认值,以防方法失败。您可以将“错误图像”加载到像素数组中。这样你至少不应该得到一个黑色的窗口。
You have a problem loading your texture. Assess how to handle the root cause (why can the image not be loaded?) and the consequences (your world is black):
Root cause
To find out why the image cannot be loaded, one measure would be to know what image the application is trying to load. Modify the load() method like so:
You may find that you are searching just in the wrong directory. Or the access privileges are not set correctly. Or the file content is corrupt. Or...
Impact
When your load method fails, it does not stop the program. Instead, your game keeps running. However the pixels array does not contain good data.
Ensure you have meaningful emergency defaults in case the method fails. You could load an 'error image' into the pixels array. With that you should at least not end up with a black window.
异常堆栈跟踪非常简单,应该告诉您需要做什么。它指向
ImageIO.read(File)
代码中的一个异常,如下所示:如您所见,问题在于
canRead()
返回false
,并根据方法的 API文档,这个当且仅当此抽象路径名指定的文件存在并且可由应用程序读取时,方法返回“true
;否则返回false
”。您尝试读取的位置不存在您的文件,或者该文件具有禁止您的应用程序读取该文件的访问保护。就是这么简单。
我相信在 catch 块中打印
new File(loc).getAbsolutePath()
将帮助您调试实际位置以及为什么事情不起作用。最有可能的是,您根本不应该使用File
,而应在应用程序中使用嵌入式资源。The exception stack trace is pretty straight-forward, and should tell you what you need to do. It points to a single exception in the code of
ImageIO.read(File)
which look like this:As you can see, the problem is that
canRead()
returnsfalse
, and according to the method's API documentation, this method returns "true
if and only if the file specified by this abstract pathname exists and can be read by the application;false
otherwise".Either your file does not exist at the location you try to read from, or the file has access protection that prohibits your application to read it. It's that simple.
I believe that printing
new File(loc).getAbsolutePath()
in the catch block will help you debug the actual location and why things don't work. Most likely, you should not use aFile
at all, but instead use an embedded resource in your application.