2011-06-05 48 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这将导致其余的绘制代码在抛出时不执行。

编辑:我假设你注释掉了代码不工作,但这是你想要做的工作。

相关问题