将GUI与游戏

时间:2015-09-26 02:11:07

标签: java swing applet

我正在尝试使用java中的游戏类构建GUI。我一直得到一个NullPointerException并且我不知道为什么,我以为我正在传递对象" cPlayer"到applet然后将它传递给JPanel部分(它应该与cPlayer比较命中或未命中)但我不知道我做错了什么。有没有其他方法将cPlayer对象传递给applet然后传递给JPanel?

public class gamePlay {
    public final static ComputerBoard cPlayer = new ComputerBoard();

    public static void main(String args){
        BattleshipApplet play = new BattleshipApplet();
        play.setBoard(cPlayer);

    }
}

public class BattleshipApplet extends JApplet {
    private final JButton playButton = new JButton("Play");
    private final JLabel msgBar = new JLabel("Click Play to start game");
    private BoardPanel panel;
    private ComputerBoard game;


    public BattleshipApplet(){
        playButton.addActionListener(this::playButtonClicked);  
    }

    public void init(){
        configureGui();
    }

    private void configureGui(){
        setLayout(new BorderLayout());
        JPanel buttons = new JPanel(new FlowLayout(FlowLayout.LEFT));
        buttons.setBorder(BorderFactory.createEmptyBorder(0,5,0,0));
        buttons.add(playButton);
        add(buttons, BorderLayout.NORTH);
        msgBar.setBorder(BorderFactory.createEmptyBorder(10,10,5,5));
        add(createBoardPanel(), BorderLayout.CENTER);
        add(msgBar, BorderLayout.SOUTH);
    }

    private BoardPanel createBoardPanel(){
        panel = new BoardPanel(game);
        return panel;
    }

    private void displayMessage(String msg){
        msgBar.setText(msg);
    }

    private void playButtonClicked(ActionEvent event){
        displayMessage("Game has started!");

    }

    void setBoard(ComputerBoard b){
        game = b;
    }

}

public class BoardPanel extends JPanel  {
    private static final int ROWS = 10;
    private static final int CELL_WIDTH = 28;
    private static final int PAD = 20;
    private static final Color GRID_COLOR = Color.blue;
    private static final Color CIRCLE_COLOR_HIT = Color.red;
    private static final Color CIRCLE_COLOR_MISS = Color.white;
    private static final Color TEXT_COLOR = Color.blue;
    private static final int SML_GAP = 2;
    private boolean[][] grid = new boolean[ROWS][ROWS];
    private int counter;
    private int x, y;
    private boolean gameBoardpiece;
    ComputerBoard gameBoard;


    public BoardPanel(ComputerBoard b) {
        addMouseListener((MouseListener) new MyMouse());
        counter =0;
        gameBoard = b;
        gameBoardpiece = false;
    }



    public void reset() {
        grid = new boolean[ROWS][ROWS]; // fills grid with false
        repaint();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);


        // draws grid
        g.setColor(GRID_COLOR);
        for (int i = 0; i <= ROWS; i++) {
            int x1 = PAD + i * CELL_WIDTH;
            int y1 = PAD;
            int x2 = x1;
            int y2 = PAD + CELL_WIDTH * ROWS;
            g.drawLine(x1, y1, x2, y2);
            g.drawLine(y1, x1, y2, x2);

        }

        // iterate through the grid boolean array
        // draw circles if the grid value is true.
        int w = CELL_WIDTH - 2 * SML_GAP; // width of the circle to draw
        int h = w;
        // nested for loop to go through the grid array
        for (int r = 0; r < grid.length; r++) {
            for (int c = 0; c < grid[r].length; c++) {
                if (grid[r][c]) {
                    //if was a hit
                    if(gameBoardpiece == false){
                        g.setColor(CIRCLE_COLOR_HIT);
                    }
                    //shot was a miss
                    else{
                        g.setColor(CIRCLE_COLOR_MISS);
                    }
                    int x = PAD + c * CELL_WIDTH + SML_GAP;
                    int y = PAD + r * CELL_WIDTH + SML_GAP;
                    g.fillOval(x, y, w, h);
                }
            }
        }
        //states the number of shots in the game
        g.setColor(TEXT_COLOR);
        g.drawString("Shots: "+counter, 305,300 );
    }



    private class MyMouse extends MouseAdapter {
        public void mousePressed(MouseEvent e) {
            x = e.getX();
            y = e.getY();

            if (x < PAD || y < PAD  ) {
                // clicked above or to right of grid
                return;
            }

            int r = (y - PAD) / CELL_WIDTH;
            int c = (x - PAD) / CELL_WIDTH;

            // if clicked to right or below grid.
            if (r >= ROWS || c >= ROWS) {
                return;
            }

            if(gameBoard.matchBoard(r,c) == 0){
                gameBoardpiece = true;
            }
            if(gameBoard.matchBoard(r,c) == 1){
                gameBoardpiece = false;
            }
            counter++;
            grid[r][c] = true;
            repaint();
        }
    }
}

我收到的错误消息是:

Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
    at battleship.BoardPanel$MyMouse.mousePressed(BoardPanel.java:113)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

1 个答案:

答案 0 :(得分:1)

applet的生命周期与应用程序的生命周期不同。如果此applet在正常运行applet的环境中运行,则不会调用<?php // Connecting, selecting database $dbconn = pg_connect("host=localhost dbname=orion user=USERNAME password=PASSWORD") or die('Could not connect: ' . pg_last_error()); // Performing SQL query $query = 'SELECT * FROM main'; $result = pg_query($query) or die('Query failed: ' . pg_last_error()); // Printing results in HTML echo "<table>\n"; while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) { echo "\t<tr>\n"; foreach ($line as $col_value) { echo "\t\t<td>$col_value</td>\n"; } echo "\t</tr>\n"; } echo "</table>\n"; // Free resultset pg_free_result($result); // Closing connection pg_close($dbconn); ?> 方法。在上下文调用applet的构造函数之后,生命周期将以调用applet的main()方法开始。

因此,以下内容会更好。

init()

或者可能完全取消public void init(){ ComputerBoard cPlayer = new ComputerBoard(); setBoard(cPlayer); configureGui(); } 方法并编写

setBoard