图形绘制图像步步高游戏

时间:2013-07-21 10:09:15

标签: java image swing graphics paintcomponent

这可能是一个愚蠢的问题,但我只是一个初学者如此怜悯我的灵魂:)。 所以我的问题是我有一个叫做游戏页面的类,它是显示所有游戏的IFrame。 在它里面我有一个名为Board的类,其中包含一块板的照片,它可以使用以下方式自行绘制:

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

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

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

好的,工作正常。 所以在板内我还得到了一个类型为“Slot”的二维数组,Slot是我写的另一个类,它有一堆WhitePiece或Stack of BlackPiece 那些是我写的代表黑板或白板的课程。

所以在Board类中有一个叫做重置板的方法,它将它组织到步步高的起始位置,这样就可以对所有的插槽进行整理。

现在黑白作品在Image中得到了一张照片,我得到的结果如下:     pic = ImageIO.read(new File(“pics / whitePiece.png”));

现在的问题是在内部复位板我从Slot类调用drawSlot方法 它不能得到我认为的照片。 我称之为: 首先我称之为drawBoard:       drawBoard(板);

然后是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)

我很想知道我做错了什么。 谢谢你,我知道它的长篇文章,你真的必须进入它 但我是初学者,需要帮助:)。

1 个答案:

答案 0 :(得分:4)

请勿在swing组件上使用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()