我试图创建一个2D平台游戏,通过以下这教程。我已经创建了一个类似于这样的Images()-class : package;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Objects;
public class Images {
public static BufferedImage[] tileBlocks;
public Images(){
tileBlocks = new BufferedImage[1];
try{
tileBlocks[0] = ImageIO.read(
// TODO: Fix the error caused by this try/catch
(Objects.requireNonNull(getClass().getResourceAsStream("TileBlocks/block_brick.png")))
);
} catch (IOException e) { e.printStackTrace(); }
}
}我在我的GamePanel()-class中创建了它的实例化,如下所示
package Main;
import GameState.GameStateManager;
import Resources.Images;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.Serial;
@SuppressWarnings("BusyWait")
public class GamePanel extends JPanel implements Runnable, KeyListener {
@Serial
private static final long serialVersionUID = 1L;
// Game thread
private boolean isRunning = false;
int FPS = 60; // Frames per second
long targetTime = 1000 / FPS; // Set target time
// Set the dimension of the game-window
public static final int WIDTH = 800;
public static final int HEIGHT = 600;
private GameStateManager gsm = new GameStateManager(); // Game state manager object
public GamePanel(){
setPreferredSize(new Dimension(WIDTH, HEIGHT));
addKeyListener(this);
setFocusable(true);
// TODO: Fix error (Reports instantiation of utility classes using the new keyword)
new Images();
start();
}
// Start the game (initialize)
private void start() {
isRunning = true;
Thread thread = new Thread(this); // Referring to Runnable
thread.start(); // Call to our game-loop
}
// Game-loop
public void run() {
long start, elapsed, wait; // Keep track of time
gsm = new GameStateManager(); // Initialize game state manager
while (isRunning) {
start = System.nanoTime(); // Start timer
update(); // Update the game-logic
repaint(); // Re-paint the board (built-in method from java.awt)
elapsed = System.nanoTime() - start;
// If everything runs in under the target time we wait (so it runs equally in all computers)
wait = targetTime - elapsed / 1000000; // Divide by 1 000 000 to get milli-seconds
if (wait <= 0) { wait = 5; } // Keep wait a positive value
try {
Thread.sleep(wait);
} catch (Exception e) { e.printStackTrace(); }
}
}
// Updating all game-logic
public void update() { gsm.update(); }// Call methods from game state manager in the game panel class
// Where we draw the graphics
public void paintComponent(Graphics g){
super.paintComponent(g); // Built-in method from java.awt
g.clearRect(0,0,WIDTH,HEIGHT); // Clear the screen before drawing
gsm.draw(g); // Call method from game state manager in the game panel class
}
public void keyPressed(KeyEvent e) {
gsm.keyPressed(e.getKeyCode()); // getKeyCode() turns KeyEvent into an integer
}
public void keyReleased(KeyEvent e) {
gsm.keyReleased(e.getKeyCode()); // getKeyCode() turns KeyEvent into an integer
}
public void keyTyped(KeyEvent e) { }
}我说错了
Instantiation of utility class 'Images'
Inspection info: Reports instantiation of utility classes using the new keyword.
In utility classes, all fields and methods are static.
Instantiation of such classes is most likely unnecessary and indicates a mistake.当我尝试运行游戏时,它没有启动,我只得到了一个NullPointerException
Exception in thread "main" java.lang.NullPointerException
at java.base/java.util.Objects.requireNonNull(Objects.java:208)
at inf112.skeleton.app/Resources.Images.<init>(Images.java:17)
at inf112.skeleton.app/Main.GamePanel.<init>(GamePanel.java:35)
at inf112.skeleton.app/Main.Game.game(Game.java:10)
at inf112.skeleton.app/Main.Game.main(Game.java:20)但是如果我删除了实例化,游戏就会启动,但是它会以更多的错误崩溃。
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException:
Cannot load from object array because "Resources.Images.tileBlocks" is null
etc...如果我从Objects.requireNonNull中删除tileBlocks[0] = ImageIO.read((getClass().getResourceAsStream("TileBlocks/block_brick.png"))
游戏开始了,但是它崩溃了,我收到了另一个错误
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException:
Cannot load from object array because "Resources.Images.tileBlocks" is null我在这里做错什么了?为什么我的tileBlocks null列表?如何读取图像并将其显示在tileBlocks上
发布于 2022-02-16 09:37:57
将文件"block_brick.png“放在类路径中的"TileBlocks”目录中。
https://stackoverflow.com/questions/71139205
复制相似问题