gpt4 book ai didi

使用 BufferedImage 加载 Sprite 时出现 JAVA 错误

转载 作者:行者123 更新时间:2023-12-02 10:20:24 25 4
gpt4 key购买 nike

我正在遵循教程,我遇到了这个错误,我似乎无法弄清楚问题是什么。我想做的就是加载 Sprite 图像。这是代码:

这是错误:

Exception in thread "Thread-0" java.lang.NullPointerException at com.mainpkg.game.Handler.gg(Handler.java:27) at com.mainpkg.game.Game.render(Game.java:107) at com.mainpkg.game.Game.run(Game.java:83) at java.base/java.lang.Thread.run(Thread.java:844)

主要游戏类:

public class Game extends Canvas implements Runnable {

public static final int WIDTH = 800, HEIGHT = WIDTH / 12 * 9;
private Thread thread;
private boolean running = false;
private Handler handler;
private BufferedImage grassTile;

public Game() {
new Window(WIDTH, HEIGHT, "MOON EXPOLATION", this);
handler = new Handler(getWidth(), getHeight());
testImage = loadImage("Resources/GrassTile.png");
}

private BufferedImage loadImage(String path) {
try {
BufferedImage loadedImage = ImageIO.read(new FileInputStream(path));
BufferedImage formattedImage = new BufferedImage(loadedImage.getWidth(), loadedImage.getHeight(),
BufferedImage.TYPE_INT_RGB);
formattedImage.getGraphics().drawImage(loadedImage, 0, 0, null);
return formattedImage;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}

public synchronized void start() {
thread = new Thread(this);
thread.start();
running = true;
}

public synchronized void stop() {
try {
thread.join();
running = false;
} catch (Exception e) {
e.printStackTrace();
}
}

public void run() {

int FRAMES = 0;
int TICKS = 0;
long lastTime = System.nanoTime();
double unprocessed = 0;
double nsPerSecs = 1000000000 / 60.0;
long Timer = System.currentTimeMillis();
while (running) {
long now = System.nanoTime();
unprocessed += (now - lastTime) / nsPerSecs;
lastTime = now;

if (unprocessed >= 1) {
TICKS++;
ticks();
unprocessed -= 1;
}
try {
Thread.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
FRAMES++;
render();

if (System.currentTimeMillis() - Timer > 1000) {
System.out.println("Ticks: " + TICKS + " FPS: " + FRAMES);
TICKS = 0;
FRAMES = 0;
Timer += 1000;
}
}
stop();
}

private void ticks() {

}

void render() {
BufferStrategy bs = this.getBufferStrategy();
if (bs == null) {
super.paint(getGraphics());
this.createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
handler.renderImage(testImage, 0, 0);
handler.render(g);
// g.setColor(Color.BLACK);
// g.fillRect(0,0,WIDTH,HEIGHT);

g.dispose();
bs.show();
}

public static void main(String[] args) {
new Game();
}

处理程序类:

public class Handler {

private BufferedImage view;
private int pixels[];

public Handler(int width, int height) {
view = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

pixels = ((DataBufferInt) view.getRaster().getDataBuffer()).getData();
}

public void render(Graphics g) {
g.drawImage(view, 0, 0, view.getWidth(), view.getHeight(), null);
}

public void renderImage(BufferedImage image, int xPosition,int yPosition) {
int[] imagepixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();

for(int y = 0; y < image.getHeight(); y++) {
for(int x = 0; x < image.getWidth(); x++) {
pixels[(x + xPosition) + (y + yPosition ) * view.getWidth()] = imagepixels[x + y * image.getWidth()];
}
}
}

最佳答案

问题出在这里:

BufferedImage loadedImage = ImageIO.read(Game.class.getResource(path));

getResource(path) 返回 null,这导致了异常。

尝试将图像路径更改为“Assets/GrassTile.png”

一个提示:

您应该避免使用绝对路径来定位您的资源。这是一个非常糟糕的主意,因为如果您更改项目的位置,它将停止工作,请尝试使用相对路径。

关于使用 BufferedImage 加载 Sprite 时出现 JAVA 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54381709/

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