gpt4 book ai didi

java - java游戏中绘制图像,出现空指针异常

转载 作者:行者123 更新时间:2023-12-02 00:33:37 25 4
gpt4 key购买 nike

我正在尝试在我正在开发的游戏中加载图像。我正在根据我们在类里面进行的小组游戏来设计它。在我的面板类中,我加载图像并将其绘制到背景,这有效,它找到我的图像并绘制它。

但是,无论我如何加载放入 spaceship 类中的图像并使用 spaceship 的实例绘制它,它都会给我一个空指针异常。我猜它永远找不到图像,但是我将它放在成功从中提取背景图像的源文件夹中的同一位置。另外,如果我在游戏屏幕类中加载空间图像并将其绘制到面板上,它就可以正常工作。

这两个类的代码如下。我尝试使用

加载图像
spaceship = ImageIO.read(new File("Background.png"));

以及:

Image img = Toolkit.getDefaultToolkit().createImage("spaceShip.png"); 

但是两种方式都会得到相同的空指针。我迷路了,希望有人能帮忙。

package temp;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class GameScreen extends JFrame {

//Create an instance of a spaceship object
SpaceShip spaceship;

//Create an instance of a missile object
Missile missile;

//Create an instance of a launcher object
Launcher launcher;

//create an array that holds missile objects
ArrayList<Missile> missileArray = new ArrayList<Missile>();

//Creates an array to hold spaceship objects
ArrayList<SpaceShip> enemyArray = new ArrayList<SpaceShip>();

int lives = 3;

public GameScreen() {
//sets up the panel and the parent frame

//sets the default specs of the JFrame and adds the panel to it
this.setSize(600, 700);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);

//Creates a panel object and adds it to the main frame
panel panel1 = new panel(launcher, enemyArray, missileArray);
this.add(panel1);
}
}

class panel extends JPanel {
//creates a new launcher object
Launcher launcher;

//creates a spaceship object
SpaceShip space;

//creates an arraylist of spaceship objects
static ArrayList<SpaceShip> ships;

//creates an arraylist of missile objects
ArrayList<Missile> missiles;

//loads images to draw
Image backGround;

public panel(Launcher la, ArrayList<SpaceShip> ss, ArrayList<Missile> mi) {
try {
launcher = la;
ships = ss;
missiles = mi;

backGround = ImageIO.read(new File("Background.png"));
} catch (Exception e) {
e.printStackTrace();
}
}

public void paintComponent(Graphics g) {
BufferedImage x = new BufferedImage(600, 700, BufferedImage.TYPE_INT_RGB);
Graphics g2 = x.getGraphics();

g2.drawImage(backGround, 0, 0, this);

g2.drawImage(space.img, 0, 0, this);

System.out.println(ships.size());

g.drawImage(x, 0, 0, this);
}
}

SpaceShip.java

import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

/**
* @author WymoreJW
*/
class SpaceShip extends Character {

Image img = Toolkit.getDefaultToolkit().createImage("spaceShip.png");

public void SpaceShip() {

this.health = 10;
this.speed = 2;
}
}

堆栈跟踪错误

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at missiledefense.panel.paintComponent(GameScreen.java:108)
at javax.swing.JComponent.paint(JComponent.java:1029)
at javax.swing.JComponent.paintChildren(JComponent.java:862)
at javax.swing.JComponent.paint(JComponent.java:1038)
at javax.swing.JComponent.paintChildren(JComponent.java:862)
at javax.swing.JComponent.paint(JComponent.java:1038)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:567)
at javax.swing.JComponent.paintChildren(JComponent.java:862)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5131)

at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered
(RepaintManager.java:1479)

at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1410)
at javax.swing.RepaintManager.paint(RepaintManager.java:1224)
at javax.swing.JComponent.paint(JComponent.java:1015)
at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:21)
at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60)
at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97)
at java.awt.Container.paint(Container.java:1780)
at java.awt.Window.paint(Window.java:3375)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:713)
at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:693)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run
(SystemEventQueueUtilities.java:125)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters
(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy
(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

最佳答案

最大的问题是您的注释与代码不一致(语义上):

//creates a spaceship object
SpaceShip space;

这不会创建实例。这创建了对实例的引用——但没有实例。它是null。您将其传递给(名称不佳的)panel 类构造函数,并表现得好像其中应该有一个值。但没有。

NPE,或至少其中之一:

g2.drawImage(space.img, 0, 0, this); // *foom*

至少在提供的代码中,space 从未初始化。

我还建议不要将所有这些都放入一个文件中。

关于java - java游戏中绘制图像,出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8386857/

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