我想学习如何在 java 中制作 3D 图形,并找到了这段代码并研究了它,研究了它是如何工作的,我搜索了每个命令的工作原理。我运行了代码,它给了我一个错误,我真的不明白它是如何不起作用的。它应该将纹理放置在 map 中的墙壁上,但不会放置它们。我正在使用 eclipse 来运行代码。
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;
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("res/wood.png", 64);
public static Texture brick = new Texture("res/redbrick.png", 64);
public static Texture bluestone = new Texture("res/bluestone.png", 64);
public static Texture stone = new Texture("res/greystone.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(1920, 1080, 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, 1920, 1080);
addKeyListener(camera);
setSize(1920, 1080);
setResizable(true);
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) {
new Game();
}
}
这是我每次运行代码时收到的错误消息。我尝试过使用不同的纹理位置,但不断收到相同的错误消息。
javax.imageio.IIOException: Can't read input file!
at javax.imageio.ImageIO.read(ImageIO.java:1301)
at Texture.load(Texture.java:20)
at Texture.<init>(Texture.java:15)
at Texture.<clinit>(Texture.java:29)
at Game.<init>(Game.java:46)
at Game.main(Game.java:104)
javax.imageio.IIOException: Can't read input file!
at javax.imageio.ImageIO.read(ImageIO.java:1301)
at Texture.load(Texture.java:20)
at Texture.<init>(Texture.java:15)
at Texture.<clinit>(Texture.java:30)
at Game.<init>(Game.java:46)
at Game.main(Game.java:104)
javax.imageio.IIOException: Can't read input file!
at javax.imageio.ImageIO.read(ImageIO.java:1301)
at Texture.load(Texture.java:20)
at Texture.<init>(Texture.java:15)
at Texture.<clinit>(Texture.java:31)
at Game.<init>(Game.java:46)
at Game.main(Game.java:104)
javax.imageio.IIOException: Can't read input file!
at javax.imageio.ImageIO.read(ImageIO.java:1301)
at Texture.load(Texture.java:20)
at Texture.<init>(Texture.java:15)
at Texture.<clinit>(Texture.java:32)
at Game.<init>(Game.java:46)
at Game.main(Game.java:104)
最佳答案
异常消息是不言自明的:无法读取 res/* 文件。
假设您已经有一个包含这些文件的 res 文件夹,该文件夹应该位于类路径中。如果没有将其放在类路径中或将 res/* 更改为绝对路径,例如C:\res\wood.png
javax.imageio.ImageIO 抛出异常:
public static BufferedImage read(File input) throws IOException {
if (input == null) {
throw new IllegalArgumentException("input == null!");
}
if (!input.canRead()) {
throw new IIOException("Can't read input file!");
}
File.canRead() 是(在 openJdk 1.8.0_192 上)
public boolean canRead() {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkRead(path);
}
if (isInvalid()) {
return false;
}
return fs.checkAccess(this, FileSystem.ACCESS_READ);
}
仅当路径无效或路径或文件没有读取访问权限时,input.canRead() 才会返回 false。如果在Linux中使用绝对路径,请递归地检查用户是否对该路径具有读取权限
关于抛出 javax.imageio.IIOException : Can't read input file! ,但我不知道为什么会这样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58237853/