0

这可能是一个愚蠢的问题,但我只是一个初学者,所以怜悯我的灵魂:)。所以我的问题是我有一个名为游戏页面的类,它是显示所有游戏的 IFrame。在里面我有一个名为 Board 的类,其中包含一张董事会的图片,它可以很好地使用:

public void paintComponent(Graphics page)
{
  super.paintComponent(page);
  page.drawImage(image, 0, 0, null);
}

图像是电路板的图像,我得到它是这样的:

image = ImageIO.read(new File("pics/board.png"));

好的,效果很好。所以在板子里面我也有一个“插槽”类型的二维数组,插槽是我写的另一个类,它有一堆 WhitePiece 或 Stack of BlackPiece 那些是我写的代表板上的黑色或白色块的类。

所以在 Board 类中有一个方法叫做 reset board ,它把它组织到步步高的起始位置,这样它就初始化了所有的插槽。

现在黑白片在 Image 类型内有一张图片,我得到这样的图片: pic = ImageIO.read(new File("pics/whitePiece.png"));

现在的问题是,当我在重置板内部调用 Slot 类的 drawSlot 方法时,它无法获得我认为的图片。我这样称呼它:首先我调用drawBoard:drawBoard(board);

那么这里是drawBoard:

  public void drawBoard(Slot[][] board) throws IOException{
   for(int i=0;i<2;i++){
    for(int j=0;j<12;j++){
        board[i][j].drawSlot(getGraphics());
    }
   }
  }

这是有问题的方法:drawSlot:

  public void drawSlot(Graphics g) throws IOException{    
  if(type == SlotType.empty){ 
      System.out.println("no type selected slot is empty Slot  Number"+slotNumber);
  }else
      if(type == SlotType.white){
        if(!wPieces.isEmpty()){
        Image pic = wPieces.pop().getPic();
        wPieces.push(new WhitePiece());
        if(slotNumber <= 11){
          for(int i=0;i<piecesAmount;i++){
            g.drawImage(pic, 5, i*30, null);
          }
        }
        else{
            for(int i=0;i<piecesAmount;i++){
                g.drawImage(pic, 5,300-(i*30), null);
            }
       }
        }else{
            System.out.println("Slot Stack is Empty Slot #"+slotNumber);
        }
      }else
      {
          if(!bPieces.isEmpty()){
          Image pic = bPieces.pop().getPic();
          bPieces.push(new BlackPiece());
          if(slotNumber<=11){
             for(int i=0;i<piecesAmount;i++){
                g.drawImage(pic, 5, i*30, 30, 30, null);
            }
          }else{
              for(int i=0;i<piecesAmount;i++){
                    g.drawImage(pic, 5, 300-(i*30), 30, 30, null);
              }  
          }
    }
          else{
      System.out.println("Slot Stack is empty Slot #"+slotNumber);
   }
}

 }

这是我得到的错误:(我认为这表明我获取图片的方式不好。)

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Try1.Slot.drawSlot(Slot.java:122)
at Try1.Board.drawBoard(Board.java:128)
at Try1.Board.resetBoard(Board.java:122)
at Try1.GamePage.<init>(GamePage.java:98)
at Try1.StartPage.startGame(StartPage.java:67)
at Try1.StartPage$eventHandler.actionPerformed(StartPage.java:49)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
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.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(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)

我很想知道我做错了什么。谢谢大家,我知道这是一个很长的帖子,你真的必须进入它,但我是一个初学者,需要帮助:)。

4

1 回答 1

4

不要getGraphics()在摆动部件上使用。在 中绘制插槽paintComponent()

public void paintComponent(Graphics page) {
    super.paintComponent(page);
    page.drawImage(image, 0, 0, null);
    drawBoard(page, board);
}

并修改 drawBoard() 以使用传递的图形对象:

public void drawBoard(Graphics g, Slot[][] board) {
   for(int i=0;i<2;i++) {
       for(int j=0;j<12;j++){
           board[i][j].drawSlot(g);
       }
   }
}

然后repaint()在董事会发生变化时打电话。

于 2013-07-21T10:17:19.373 回答