2015-09-26 68 views
1

我正在嘗試在java中構建一個帶有遊戲類的GUI。我不斷收到一個NullPointerException異常,我不知道爲什麼,我認爲我將對象「cPlayer」傳遞給applet,然後將它傳遞給JPanel部分(它應該與cPlayer進行比較以找出命中或未命中),但是我有不知道我做錯了什麼。有沒有其他方法可以將cPlayer對象傳遞給applet,然後傳遞JPanel?連接GUI與遊戲

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) 
+0

這NullPointerException異常告訴你,我們,究竟出了什麼問題,爲什麼。請在你的問題中包含完整的堆棧跟蹤。 – VGR

回答

1

一個Applet的生命週期是從一個應用程序的生命週期不同。如果此applet運行在正常運行applet的環境中,則不會調用main()方法。在上下文調用小程序的構造函數之後,生命週期將開始調用小程序的方法init()

因此,類似下面的內容會更好。

public void init(){ 
    ComputerBoard cPlayer = new ComputerBoard(); 
    setBoard(cPlayer); 
    configureGui(); 
} 

或許脫離了setBoard方法完全和寫作

public void init(){ 
    game = new ComputerBoard(); 
    configureGui(); 
} 
+0

太棒了!有效!我真的很困擾applets。 – user3369920