【问题标题】:GUI Elements of Array Null After Button Click按钮单击后数组 Null 的 GUI 元素
【发布时间】:2022-01-12 20:34:47
【问题描述】:

我相信问题出在我的 GameEngine 之间。换句话说,我认为在一个游戏缓冲区中,我认为 GUI 元素由于某种原因会变为空。这是我的代码: 游戏引擎类:

package pong;

import static pong.Main.window;

public class GameEngine{
    static GameState gameState;
    private long timeElapsed;
    
    public GameEngine(){
        gameState=new GameState(0);//0 is for the menu
    }
    
    public void run(){
        long beginTime=System.nanoTime();
        //33 million is 30 fps
        if(timeElapsed>=33333333){
            gameState.buffer();
            timeElapsed-=33333333;
        }
        window.getPanel().repaint();
        
        timeElapsed+=System.nanoTime()-beginTime;
    }
    
    public long getTimeElapsed(){
        return timeElapsed;
    }
}

GUIButton 类:

package pong;

import java.awt.Color;
import java.awt.Graphics2D;
import static pong.Panel.mouseClicked;
import static pong.Panel.mouseX;
import static pong.Panel.mouseY;

public abstract class GUIButton extends GUI{
    private boolean mouseOverButton,readyForButtonAction;
    @Override
    public void paint(Graphics2D g) {
        g.setColor(Color.white);
        if(mouseOverButton){
            g.fillRect(x-5,y-5,110,40);
            g.setColor(Color.black);
            g.fillRect(x, y, 100, 30);
        }
        else{
            g.fillRect(x,y,100,30);
            g.setColor(Color.black);
            g.fillRect(x+5, y+5, 90, 20);
        }
    }
    
    @Override 
    public void buffer(){
        mouseOverButton=mouseX-8>x&&mouseX-8<x+100&&
            mouseY-32>y&&mouseY-32<y+30;
        if(mouseOverButton&&mouseClicked)
            readyForButtonAction=true;
    }
    
    public void buttonAction(){
        readyForButtonAction=false;
        mouseClicked=false;
    }
    
    public boolean buttonActionIsReady(){
        return readyForButtonAction;
    }
}

GUIButtonMultiplayer(将您转移到标题 GUI 到多人选项 GUI 的按钮)

package pong;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import static pong.GameState.GUI;

public class GUIButtonMultiPlayer extends GUIButton{

    public GUIButtonMultiPlayer(){
        x=300;
        y=300;
    }
    
    @Override
    public void paint(Graphics2D g) {
        super.paint(g);
        g.setColor(Color.white);
        g.setFont(new Font("Serif",Font.BOLD,12));
        g.drawString("Multiplayer",x+10,y+20);
    }
    @Override
    public void buttonAction() {
        super.buttonAction();
        GUI=new GUI[4];
        GUI[0]=new GUIButtonLocal();
        GUI[1]=new GUIButtonJoin();
        GUI[2]=new GUIButtonHost();
        GUI[3]=new GUIButtonBackMultiPlayer();
    }
}

面板类(包含 mouseClicked 变量)

package pong;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JPanel;
import static pong.GameEngine.gameState;

public class Panel extends JPanel implements KeyListener,MouseListener,MouseMotionListener{
    static int mouseX,mouseY;
    static boolean mouseDown,mouseClicked;
    private boolean keyDown[];
    
    public Panel(){
        keyDown=new boolean[4];
    }
    
    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        gameState.paint((Graphics2D)g);
    }

    public boolean getKeyDown(int index){
        return keyDown[index];
    }
    
    @Override
    public void keyTyped(KeyEvent ke) {}

    @Override
    public void keyPressed(KeyEvent ke) {
        switch(ke.getKeyCode()){
            case KeyEvent.VK_DOWN:
                keyDown[0]=true;
                break;
            case KeyEvent.VK_UP:
                keyDown[1]=true;
                break;
            case KeyEvent.VK_W:
                keyDown[2]=true;
                break;
            case KeyEvent.VK_S:
                keyDown[3]=true;
                break;
        }
    }

    @Override
    public void keyReleased(KeyEvent ke) {
        switch(ke.getKeyCode()){
            case KeyEvent.VK_DOWN:
                keyDown[0]=false;
                break;
            case KeyEvent.VK_UP:
                keyDown[1]=false;
                break;
            case KeyEvent.VK_W:
                keyDown[2]=false;
                break;
            case KeyEvent.VK_S:
                keyDown[3]=false;
                break;
        }
    }

    @Override
    public void mouseClicked(MouseEvent me) {
        mouseClicked=true;
    }

    @Override
    public void mousePressed(MouseEvent me) {
        mouseDown=true;
    }

    @Override
    public void mouseReleased(MouseEvent me) {
        mouseDown=false;
    }

    @Override
    public void mouseEntered(MouseEvent me) {}

    @Override
    public void mouseExited(MouseEvent me) {}

    @Override
    public void mouseDragged(MouseEvent me) {}

    @Override
    public void mouseMoved(MouseEvent me) {
        mouseX=me.getX();
        mouseY=me.getY();
    }
}

GameState 类(实现)

package pong;

import java.awt.Color;
import java.awt.Graphics2D;
import static pong.Panel.mouseClicked;

public class GameState{
    static GUI GUI[];
    private int ID;
    private Ball ball;
    private Paddle paddles[];
    
    public GameState(int id){
        ID=id;
        switch(ID){
            case 0://title screen
                GUI=new GUI[4];
                GUI[0]=new GUITitle();
                GUI[1]=new GUIButtonSinglePlayer();
                GUI[2]=new GUIButtonMultiPlayer();
                GUI[3]=new GUIButtonExit();
                break;
            case 1://single player
                paddles=new Paddle[2];
                //true indicates that it is a left paddle
                paddles[0]=new PaddlePlayer(true);
                paddles[1]=new PaddleCPU();
                GUI=new GUI[1];
                GUI[0]=new GUIScores();
                ball=new Ball(paddles,(GUIScores)GUI[0]);
                ((PaddleCPU)paddles[1]).setBall(ball);
                break;
            case 2://local multiplayer
                paddles=new Paddle[2];
                //true indicates that it is a left paddle
                paddles[0]=new PaddlePlayer(true);
                paddles[1]=new PaddlePlayer(false);
                GUI=new GUI[1];
                GUI[0]=new GUIScores();
                ball=new Ball(paddles,(GUIScores)GUI[0]);
                break;
            case 3://host multiplayer
                
                paddles=new Paddle[2];
                paddles[0]=new PaddleMultiplayer();
                paddles[1]=new PaddleMultiplayer();
                GUI=new GUI[1];
                GUI[0]=new GUIScores();
                ball=new Ball(paddles,(GUIScores)GUI[0]);
                break;
        }
    }
    
    public void paint(Graphics2D g){
        g.setColor(Color.black);
        g.fillRect(0,0,800,600);
        switch(ID){
            case 0://title screen
                    System.out.println();
                for(GUI gui:GUI){
                        System.out.println(gui==null);
                    gui.paint(g);
                }
                break;
            case 1:case 2:case 3://single player and multiplayer
                g.setColor(Color.white);
                for(int a=10;a<600;a+=20)
                    g.fillRect(380, a, 10, 10);
                ball.paint(g);
                for(Paddle paddle:paddles)
                    paddle.paint(g);
                GUI[0].paint(g);
                break;
        }
    }
    
    public void buffer(){
        switch(ID){
            case 0://title screen   
                for(GUI gui:GUI)
                    if(gui instanceof GUIButton && ((GUIButton)gui).buttonActionIsReady()){
                        ((GUIButton)gui).buttonAction();
                        break;
                    }
                    else gui.buffer();
                break;
            case 1:case 2:case 3://single player and multiplayer
                ball.buffer();
                for(Paddle paddle:paddles)
                    paddle.buffer();
                break;
        }
        mouseClicked=false;
    }
    
    public int getID(){
        return ID;
    }
}

此代码在绘制 GUI 时生成 NullPointerException。如果我注释掉这一行

gui.paint(g);

然后它只是创建一个 NullPointerException 来缓冲 GUI。这是输出的sn-p:

false
true
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

false
false
false
false
    at pong.GameState.paint(GameState.java:62)
    at pong.Panel.paintComponent(Panel.java:25)

false
false
false
false

false
false
false
false

false
false
false
false

false
false
false
false
    at javax.swing.JComponent.paint(JComponent.java:1056)

false
false
false
false

false
false
false
false

false
false
false
false

false
false
false
false
    at javax.swing.JComponent.paintToOffscreen(JComponent.java:5210)
    at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1579)
    at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1502)
    at javax.swing.RepaintManager.paint(RepaintManager.java:1272)
    at javax.swing.JComponent._paintImmediately(JComponent.java:5158)

false
false
false
    at javax.swing.JComponent.paintImmediately(JComponent.java:4969)
false
    at javax.swing.RepaintManager$4.run(RepaintManager.java:831)
    at javax.swing.RepaintManager$4.run(RepaintManager.java:814)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:814)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:789)
    at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:738)
    at javax.swing.RepaintManager.access$1200(RepaintManager.java:64)

哪个 GUI 元素显示为 null 似乎是随机的。

【问题讨论】:

  • GameState 类的方法paint 中,哪一行正在抛出NullPointerException?换句话说,文件GameState.java中的第62行是哪一行?
  • gui.paint(g);就在 println 之后
  • 你确定类成员GUI,在类GameState,需要静态吗?创建数组时,所有元素都为空。看起来您正在创建一个数组,但没有为每个元素设置一个值。我强烈建议您采用java naming conventions,因为我认为这将使您的代码更易于他人阅读和理解。

标签: java arrays swing


【解决方案1】:

在我看来,您似乎在为您的 GUI 元素使用静态 GameState.GUI 数组(如果我错了,请纠正我)。

在代码的其他地方,您正在按顺序访问这个静态 GUI 数组。似乎有时会发生其他代码会更改此静态 GUI 数组的内容(像这样的代码可能是罪魁祸首GUI=new GUI[1];),而其他代码即将开始在该数组中循环。您必须更改代码,以免发生这种情况。您可以同步访问此静态 GUI 数组的方法(但这会阻止其他方法并让它们等待,这对于您的用例可能合适,也可能不合适)。

如果您确定这确实是问题的原因(可能不是),并且您希望避免使用同步方法,那么您还有其他一些选择。你可以看看双缓冲是如何工作的。使用 2 个数组,始终从主要数组读取并始终修改次要数组,并在它们之间交换。

或者,如果您想保留当前代码,您可以更改以下模式:

GUI=new GUI[4];
GUI[0]=new GUITitle();
GUI[1]=new GUIButtonSinglePlayer();
GUI[2]=new GUIButtonMultiPlayer();
GUI[3]=new GUIButtonExit();

在您使用 GUI=new GUI[...]; 的代码中随处可见这种模式:

GUI[] newGui=new GUI[4];
newGui[0]=new GUITitle();
newGui[1]=new GUIButtonSinglePlayer();
newGui[2]=new GUIButtonMultiPlayer();
newGui[3]=new GUIButtonExit();
GUI=newGui

这可能足以避免问题。

【讨论】:

  • 我不应该将 GUI 设为静态。你是对的。使其成为字段成员并使用您的代码有效。
猜你喜欢
  • 2019-05-19
  • 2013-03-15
  • 1970-01-01
  • 2018-06-23
  • 2019-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-06
相关资源
最近更新 更多