2011-06-05 23 views
0

所以即時通訊在Java構建磚斷路器,到目前爲止我已完成大部分UI,但是在我的UI上顯示我的磚時遇到問題。我在我的paint方法中編寫了它的代碼,它構建了我的面板,然後我的面板被添加到另一個類的JFrame中。一切都顯示,除了我的磚,我似乎無法找出爲什麼..我for循環似乎不會運行在我的繪製方法

// AnimationPanel.java 
// 
// Informatics 45 Spring 2010 
// Code Example: Our Ball Animation Becomes a Game 
// 
// This is relatively similar to our AnimationPanel in the previous version 
// of our ball animation, with two changes: 
// 
// * The paddle is now drawn, in addition to just the ball. For fun, I've 
// drawn the paddle in a different color than the ball. 
// 
// * This panel has a MouseMotionListener attached to it. The job of a 
// MouseMotionListener is to listen for mouse movement within a component. 
// In this case, any mouse movement within our AnimationPanel will turn 
// into events, which we'll handle by adjusting the center x-coordinate 
// of the paddle accordingly. 
// 
// * Because we need to calculate a new position for the paddle as the mouse 
// moves, we'll need to be able to convert coordinates in both directions 
// (i.e., fractional coordinates to pixel coordinates and pixel coordinates 
// to fractional coordinates). 

package inf45.spring2010.examples.animation2.gui; 

import inf45.spring2009.examples.animation2.model.Animation; 
import inf45.spring2009.examples.animation2.model.AnimationState; 

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 


public class AnimationPanel extends JPanel 
{ 
    private Animation animation; 
    private inf45.spring2009.examples.animation2.model.AnimationState currentState; 
    boolean brickVisible[][]; 
    int bricksInRow = 4; 
    int bricksInColumn = 8; 
    int brickWidth; 
    int brickHeight; 
    int bricksLeft; 

    public AnimationPanel(Animation animation) 
    { 
     this.animation = animation; 
     currentState = null; 
     setBackground(Color.WHITE); 

     addMouseMotionListener(
      new MouseMotionAdapter() 
      { 
       public void mouseMoved(MouseEvent e) 
       { 
        updatePaddlePosition(e.getX()); 
       } 
      }); 
    } 


    public void updateState(AnimationState newState) 
    { 
     currentState = newState; 
    } 


    private void updatePaddlePosition(int pixelX) 
    { 
     double paddleCenterX = convertPixelXToX(pixelX); 
     animation.updatePaddleCenterX(paddleCenterX); 
    } 


    public void paint(Graphics g) 
    { 
     super.paint(g); 

     if (currentState == null) 
     { 
      return; 
     } 





     int centerPixelX = convertXToPixelX(currentState.getBallCenterX()); 
     int centerPixelY = convertYToPixelY(currentState.getBallCenterY()); 

     int radiusX = convertXToPixelX(currentState.getBallRadius()); 
     int radiusY = convertYToPixelY(currentState.getBallRadius()); 

     int topLeftPixelX = centerPixelX - radiusX; 
     int topLeftPixelY = centerPixelY - radiusY; 

     int paddleCenterPixelX = convertXToPixelX(currentState.getPaddleCenterX()); 
     int paddleCenterPixelY = convertYToPixelY(currentState.getPaddleCenterY()); 
     int paddleWidthPixels = convertXToPixelX(currentState.getPaddleWidth()); 
     int paddleHeightPixels = convertYToPixelY(currentState.getPaddleHeight()); 

     int paddleTopLeftPixelX = paddleCenterPixelX - (paddleWidthPixels/2); 
     int paddleTopLeftPixelY = paddleCenterPixelY - (paddleHeightPixels/2); 

     Graphics2D g2d = (Graphics2D) g; 

     /* for (int x = 0;x<bricksInRow;x++){ 
       for (int y = 0;y<bricksInColumn;y++){ 
        boolean bricks[][] = null; 
       brickVisible[x][y] = bricks[x][y] ; 
       { 
        g2d.setColor(Color.black); 
        g2d.fillRect(x*brickWidth,y*brickHeight,brickWidth-1,brickHeight-1); 
       } 
      } 
     } 
     */ 

     g2d.setRenderingHint(
      RenderingHints.KEY_ANTIALIASING, 
      RenderingHints.VALUE_ANTIALIAS_ON); 

     g2d.setColor(Color.BLUE); 

     g2d.fillOval(topLeftPixelX, topLeftPixelY, radiusX * 2, radiusY * 2); 

     g2d.setColor(Color.RED); 


     g2d.fillRect(
      paddleTopLeftPixelX, paddleTopLeftPixelY, 
      paddleWidthPixels, paddleHeightPixels); 
    } 


    private int convertXToPixelX(double x) 
    { 
     return (int) (x * getWidth()); 
    } 


    private int convertYToPixelY(double y) 
    { 
     return (int) (y * getHeight()); 
    } 


    private double convertPixelXToX(int pixelX) 
    { 
     return (double) pixelX/getWidth(); 
    } 


} 
+3

這不是因爲循環在評論中,是嗎? – Soumya 2011-06-05 07:32:11

+2

「信息學45 2010年春季刊」你對這個問題遲了一年。順便說一句 - 如果這是作業,請添加'作業'標籤。 – 2011-06-05 07:37:37

+0

您是否嘗試在for循環中添加一些包含您正在使用的變量值的日誌語句? – extraneon 2011-06-05 08:24:50

回答

1

好像你不要在你的代碼分配給brickHeightbrickWidth任何價值。這可能是問題所在。

0

MByD說了什麼,雖然由於這些字段是包本地的,您可能會在其他地方設置這些字段。此外,還有一個問題,NPE在這裏:

boolean bricks[][] = null; 
    brickVisible[x][y] = bricks[x][y] ; 

我不知道,如果你之前,或者你發現事情沒工作後添加這,但是這是一個肯定火NullPointerException這將導致其餘的繪製代碼在拋出時不執行。

編輯:我假設你註釋掉了代碼不工作,但這是你想要做的工作。

相關問題