gpt4 book ai didi

java - 无法读取输入文件-Java

转载 作者:行者123 更新时间:2023-12-01 06:13:34 25 4
gpt4 key购买 nike

所以我正在遵循一系列教程来学习如何用java制作游戏(我还很新),我想我完全遵循了他的代码,但当我运行它时它仍然打印堆栈跟踪。这是我的代码...

游戏.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确实存在

最佳答案

好吧,我明白了。我的 sprite_sheet.png 位于我的“res”文件夹中,我需要将其添加到构建路径

关于java - 无法读取输入文件-Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29761032/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com