无法读取输入文件-Java
Cant read input file-Java
所以我正在按照一系列教程学习如何在 java 中制作游戏(我还是很新),我想我完全按照他的代码,但它仍然打印堆栈跟踪当我 运行 它。这是我的代码...
Game.java
package com.game.src.main;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.swing.JFrame;
public class Game extends Canvas implements Runnable{
private static final long serialVersionUID = 1L;
public static final int WIDTH = 320;
public static final int HEIGHT = WIDTH / 12 * 9;
public static final int SCALE = 2;
public final String TITLE = "2D Space Game";
private JFrame frame = new JFrame(TITLE);
private boolean running = false;
private Thread thread;
private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
private BufferedImage spriteSheet = null;
private BufferedImage player;
public void init(){
BufferedImageLoader loader = new BufferedImageLoader();
spriteSheet = loader.loadImage("/sprite_sheet.png");
SpriteSheet ss = new SpriteSheet(spriteSheet);
player = ss.grabImage(1, 1, 32, 32);
}
public void run(){
long lastTime = System.nanoTime();
final double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
long now = 0;
int updates = 0, frames = 0;
long timer = System.currentTimeMillis();
init();
while(running){
now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
if(delta >= 1){
tick();
updates++;
delta--;
}
render();
frames++;
if(System.currentTimeMillis() - timer > 1000){
timer += 1000;
frame.setTitle("2-D Space Game || Updates: " + updates + ", FPS: " + frames);
frames = 0;
updates = 0;
}
}
stop();
}
private void tick(){
}
private void render(){
BufferStrategy bs = this.getBufferStrategy();
try {
Thread.sleep(2);
} catch (InterruptedException e){
e.printStackTrace();
}
if(bs == null){
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
/////////////////////////////////////
g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
g.drawImage(player, 100, 100, this);
/////////////////////////////////////
g.dispose();
bs.show();
}
private synchronized void start(){
if(running) return;
running = true;
thread = new Thread(this);
thread.start();
}
private synchronized void stop(){
if(!running) return;
try{
thread.join();
}catch(InterruptedException e){
e.printStackTrace();
}
System.exit(1);
}
public static void main(String args[]){
Game game = new Game();
game.setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
game.setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
game.setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
game.frame.add(game);
game.frame.pack();
game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.frame.setResizable(false);
game.frame.setLocationRelativeTo(null);
game.frame.setVisible(true);
game.start();
}
}
SpriteSheet.java
package com.game.src.main;
import java.awt.image.BufferedImage;
public class SpriteSheet {
private BufferedImage image;
public SpriteSheet(BufferedImage image){
this.image = image;
}
public BufferedImage grabImage(int col, int row, int width, int height){
return image.getSubimage((col * 32) - 32, (row * 32) - 32, width, height);
}
}
BufferedImageLoader.java
package com.game.src.main;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class BufferedImageLoader {
private BufferedImage image;
public BufferedImage loadImage(String path){
try {
image = ImageIO.read(getClass().getResource(path));
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
return image;
}
}
现在显示的是堆栈跟踪:
Exception in thread "Thread-0" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(ImageIO.java:1388)
at com.game.src.main.BufferedImageLoader.loadImage(BufferedImageLoader.java:13)
at com.game.src.main.Game.init(Game.java:30)
at com.game.src.main.Game.run(Game.java:45)
at java.lang.Thread.run(Thread.java:745)
有人可以帮忙吗?
顺便提一句:
sprite_sheet.png确实存在
这里的问题在于您处理 png 的方式,并且您指定的位置不在构建路径中,也不在您期望的位置。如果您向我提供您所在位置的 pwd
,我将更新您应该使用的实际路径
您要么需要提供完整路径
/home/me/myprogram/sprite_sheet.png
或使用相对路径
./sprite_sheet.png
您的程序正在查找根目录中的文件/
好的,我明白了。我的 sprite_sheet.png 在我的 "res" 文件夹中,我需要将它添加到构建路径
您需要提供相对于您的 CLASSPATH、 的完整路径 或相对于当前 class 包的相对路径。
所以我正在按照一系列教程学习如何在 java 中制作游戏(我还是很新),我想我完全按照他的代码,但它仍然打印堆栈跟踪当我 运行 它。这是我的代码...
Game.java
package com.game.src.main;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.swing.JFrame;
public class Game extends Canvas implements Runnable{
private static final long serialVersionUID = 1L;
public static final int WIDTH = 320;
public static final int HEIGHT = WIDTH / 12 * 9;
public static final int SCALE = 2;
public final String TITLE = "2D Space Game";
private JFrame frame = new JFrame(TITLE);
private boolean running = false;
private Thread thread;
private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
private BufferedImage spriteSheet = null;
private BufferedImage player;
public void init(){
BufferedImageLoader loader = new BufferedImageLoader();
spriteSheet = loader.loadImage("/sprite_sheet.png");
SpriteSheet ss = new SpriteSheet(spriteSheet);
player = ss.grabImage(1, 1, 32, 32);
}
public void run(){
long lastTime = System.nanoTime();
final double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
long now = 0;
int updates = 0, frames = 0;
long timer = System.currentTimeMillis();
init();
while(running){
now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
if(delta >= 1){
tick();
updates++;
delta--;
}
render();
frames++;
if(System.currentTimeMillis() - timer > 1000){
timer += 1000;
frame.setTitle("2-D Space Game || Updates: " + updates + ", FPS: " + frames);
frames = 0;
updates = 0;
}
}
stop();
}
private void tick(){
}
private void render(){
BufferStrategy bs = this.getBufferStrategy();
try {
Thread.sleep(2);
} catch (InterruptedException e){
e.printStackTrace();
}
if(bs == null){
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
/////////////////////////////////////
g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
g.drawImage(player, 100, 100, this);
/////////////////////////////////////
g.dispose();
bs.show();
}
private synchronized void start(){
if(running) return;
running = true;
thread = new Thread(this);
thread.start();
}
private synchronized void stop(){
if(!running) return;
try{
thread.join();
}catch(InterruptedException e){
e.printStackTrace();
}
System.exit(1);
}
public static void main(String args[]){
Game game = new Game();
game.setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
game.setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
game.setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
game.frame.add(game);
game.frame.pack();
game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.frame.setResizable(false);
game.frame.setLocationRelativeTo(null);
game.frame.setVisible(true);
game.start();
}
}
SpriteSheet.java
package com.game.src.main;
import java.awt.image.BufferedImage;
public class SpriteSheet {
private BufferedImage image;
public SpriteSheet(BufferedImage image){
this.image = image;
}
public BufferedImage grabImage(int col, int row, int width, int height){
return image.getSubimage((col * 32) - 32, (row * 32) - 32, width, height);
}
}
BufferedImageLoader.java
package com.game.src.main;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class BufferedImageLoader {
private BufferedImage image;
public BufferedImage loadImage(String path){
try {
image = ImageIO.read(getClass().getResource(path));
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
return image;
}
}
现在显示的是堆栈跟踪:
Exception in thread "Thread-0" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(ImageIO.java:1388)
at com.game.src.main.BufferedImageLoader.loadImage(BufferedImageLoader.java:13)
at com.game.src.main.Game.init(Game.java:30)
at com.game.src.main.Game.run(Game.java:45)
at java.lang.Thread.run(Thread.java:745)
有人可以帮忙吗? 顺便提一句: sprite_sheet.png确实存在
这里的问题在于您处理 png 的方式,并且您指定的位置不在构建路径中,也不在您期望的位置。如果您向我提供您所在位置的 pwd
,我将更新您应该使用的实际路径
您要么需要提供完整路径
/home/me/myprogram/sprite_sheet.png
或使用相对路径
./sprite_sheet.png
您的程序正在查找根目录中的文件/
好的,我明白了。我的 sprite_sheet.png 在我的 "res" 文件夹中,我需要将它添加到构建路径
您需要提供相对于您的 CLASSPATH、 的完整路径 或相对于当前 class 包的相对路径。