gpt4 book ai didi

Java游戏无法读取图像文件

转载 作者:行者123 更新时间:2023-12-02 06:32:51 26 4
gpt4 key购买 nike

由于某种原因,我的程序找不到图像。我已经尝试了一切并确保文件路径是准确的。在我之前的游戏中它运行得很好,但现在它根本找不到图像。

实体类

package Entities;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Entity
{
public double x, y, width, height, dx, dy;

public BufferedImage image;

public Entity(String s)
{
try
{ //error occurs here
image = ImageIO.read(getClass().getResourceAsStream(s));
}
catch (IOException e)
{
e.printStackTrace();
}

width = image.getWidth();
height = image.getHeight();
}

public void update()
{
x += dx;
y += dy;
}

public void draw(Graphics2D g)
{
g.drawImage(image, (int)x, (int)y, null);
}
}

GamePanel类

package Main;

//import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;

import javax.swing.JPanel;

import Entities.Entity;

@SuppressWarnings("serial")
public class GamePanel extends JPanel implements Runnable, KeyListener
{
public static final int WIDTH = 320;
public static final int HEIGHT = 240;
public static final int SCALE = 2;

//game thread
private Thread thread;
private boolean running;
private int fps = 60;
private long targetTime = 1000/fps;

//image
private BufferedImage image;
private Graphics2D g;

//test
public Entity e;

public GamePanel()
{
super();
setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
setFocusable(true);
requestFocus();
}

public void addNotify()
{
super.addNotify();
if(thread == null)
{
thread = new Thread(this);
addKeyListener(this);
thread.start();
}
}

public void init()
{
image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
g = (Graphics2D) image.getGraphics();
running = true;

e = new Entity("Resources/Test/test.png");
}

@Override
public void keyPressed(KeyEvent key)
{
//p1.keyPressed(key.getKeyCode());
}

@Override
public void keyReleased(KeyEvent key)
{
//p1.keyReleased(key.getKeyCode());
}

@Override
public void keyTyped(KeyEvent arg0)
{

}

@Override
public void run()
{
init();

long start;
long elapsed;
long wait;

//game loop
while(running)
{
start = System.nanoTime();

update();
draw();
drawToScreen();

elapsed = System.nanoTime() - start;

wait = targetTime - elapsed/1000000;

if(wait < 0)
{
wait = 5;
}

try
{
Thread.sleep(wait);
}
catch(Exception e)
{
e.printStackTrace();
}
}

}

private void drawToScreen()
{
Graphics g2 = getGraphics();
g2.drawImage(image, 0, 0, WIDTH * SCALE, HEIGHT * SCALE, null);
g2.dispose();

}

private void draw()
{
//e.draw(g);
}

private void update()
{

}
}

这是错误

Exception in thread "Thread-2" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source)
at Entities.Entity.<init>(Entity.java:19)
at Main.GamePanel.init(GamePanel.java:67)
at Main.GamePanel.run(GamePanel.java:93)
at java.lang.Thread.run(Unknown Source)

最佳答案

路径Resources/Test/test.png建议从用于尝试加载类引用的位置开始的相对路径。

由于您正在使用 Entity 类来尝试加载图像,并且 Entity 驻留在 Entities 包中,因此这将生成一个/Entities/Resources/Test/test.png 的绝对路径(来自类路径的上下文),这显然是不正确的。

尝试使用 /Resources/Test/test.png (假设 Resources 位于路径树的顶部,例如...

+ Resources
+ Test
- test.png
+ Entities
- Entity
+ Main
- GamePanel

关于Java游戏无法读取图像文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19921633/

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