0

我正在尝试将 CardLayout 中的卡片从我的游戏 JPanel 切换到我的 gameOver JPanel。游戏没有切换屏幕,而是崩溃了。感谢您提前提出的任何建议。

我的游戏代码:

public class Game extends JPanel implements KeyListener {

    private Player player;
    private Stage stage;
    private Stage stageLeft;
    private Stage stageRight;
    private EnemyManager manager;

    private Window windowInstance;

    private boolean isGameOver = false;
    private boolean restart = false;

    public Game() {
        setSize(800,600);
        setPreferredSize(new Dimension(800,600));
        setFocusable(true);
        setBackground(Color.WHITE);
        requestFocus();
        addKeyListener(this);

        stage = new Stage(0, 540, 800, 100);
        stageLeft = new Stage(-1, 0, 1, 600);
        stageRight = new Stage(800, 0, 1, 600);
        player = new Player(this, 400, 400);
        manager = new EnemyManager(this, 10);
    }

    @Override
    public void update(Graphics g) {
        paint(g);
    }

    public void paint(Graphics g) {
        g.setColor(Color.WHITE);
        g.fillRect(0,0,getWidth(),getHeight());

        stage.draw(g);
        stageLeft.draw(g);
        stageRight.draw(g);
        if(!isGameOver || restart) {
            player.draw(g);
            manager.draw(g);
        } else {
            g.setColor(Color.BLACK);
            g.fillRect(0, 0, getWidth(), getHeight());

            g.setColor(Color.WHITE);
            g.setFont(new Font("Century Gothic", Font.BOLD, 24));
            g.drawString("Game Over!", 330, 300);
            //TODO: FIX CRASH ERROR
            if(!windowInstance.getGameOver()) {
                windowInstance.setGameOver(true);
            }
        }

        g.dispose();
        repaint();
    }

    @Override
    public void keyPressed(KeyEvent e) {
        int c = e.getKeyCode();
        if(c == KeyEvent.VK_W) {

        }
        if(c == KeyEvent.VK_A) {
            player.setxDir(-1);
        }
        if(c == KeyEvent.VK_S) {

        }
        if(c == KeyEvent.VK_D) {
            player.setxDir(1);
        }
    }

    public void setGameOver(boolean flag) {
        isGameOver = flag;
    }

    @Override
    public void keyReleased(KeyEvent e) {
        player.setxDir(0);
        player.setyDir(0);
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    public Stage getStage() {
        return stage;
    }

    public Stage getStageLeft() { return stageLeft; }

    public Stage getStageRight() { return stageRight; }

    public EnemyManager getEnemyManager() {
        return manager;
    }
}

我的窗口代码(错误似乎是由将 CardLayout 切换到 gameOver JPanel 引起的):

public class Window {

    JFrame frame = new JFrame("Cube Fall");

    JPanel panelContainer = new JPanel();
    JPanel startMenu = new JPanel();
    Game game = new Game();
    JPanel gameOver = new JPanel();


    CardLayout cardLayout = new CardLayout();

    // Constructor
    public Window() {
        // Layout Stuff
        panelContainer.setLayout(cardLayout);

        panelContainer.add(startMenu, "0");
        panelContainer.add(game, "1");
        panelContainer.add(gameOver, "2");
        cardLayout.show(panelContainer, "0");

        // Setup startMenu
        JButton startButton = new JButton("Start");
        startMenu.add(startButton);
        startMenu.setBackground(Color.BLACK);

        startButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                cardLayout.show(panelContainer, "1");
                game.grabFocus();
            }
        });

        // Setup gameOver
        JButton restartButton = new JButton("Restart");
        gameOver.add(restartButton);
        gameOver.setBackground(Color.BLACK);

        restartButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                cardLayout.show(panelContainer, "1");
                game.grabFocus();
            }
        });

        frame.add(panelContainer);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
        frame.setResizable(false);
        frame.requestFocus();
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Window();
            }
        });

        /*

        Game game = new Game();

        JFrame frame = new JFrame();
        frame.setTitle("Java Game");
        frame.add(game);
        frame.pack();
        frame.setPreferredSize(new Dimension(800,600));
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // Center window
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);*/
    }

    public JFrame getFrame() {
        return frame;
    }

    private boolean trueOrFalse = false;

    public void setGameOver(boolean tof) {
        if(tof) {
            JButton restartButton = new JButton("Restart");
            restartButton.setLocation(300,300);
            game.add(restartButton);
            restartButton.setVisible(true);
            game.revalidate();
            //cardLayout.show(panelContainer, "2");
            trueOrFalse = true;
        }
    }

    public boolean getGameOver() {
        return trueOrFalse;
    }
}

我得到的错误如下:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at Game.paint(Game.java:59) at javax.swing.JComponent._paintImmediately(JComponent.java:5106) at javax.swing.JComponent.paintImmediately(JComponent.java:4890) at javax.swing.RepaintManager$3.run(RepaintManager.java:814) at javax.swing.RepaintManager$3.run(RepaintManager.java:802) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:86) at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:802) at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:745) at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:725) at javax.swing.RepaintManager.access$1000(RepaintManager.java:46) at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1680) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:715) at java.awt.EventQueue.access$400(EventQueue.java:82) at java.awt.EventQueue$2.run(EventQueue.java:676) at java.awt.EventQueue$2.run(EventQueue.java:674) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:86) at java.awt.EventQueue.dispatchEvent(EventQueue.java:685) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188) at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

4

0 回答 0