我的for循环似乎没有在我的paint方法中运行

时间:2011-06-05 07:25:17

标签: java swing paint

所以即时通讯在Java上构建破坏程序,到目前为止,我已经完成了大部分用户界面,但我遇到了在我的UI上显示我的砖块的问题。我已经在我的paint方法中编写了代码,它构建了我的面板,然后我的面板被添加到另一个类的JFrame中。一切都显示除了我的砖块,我似乎无法弄清楚为什么..

// 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();
    }


}

2 个答案:

答案 0 :(得分:1)

您似乎没有在代码中为brickHeightbrickWidth分配任何值。这可能是问题所在。

答案 1 :(得分:0)

MByD说的是,虽然这些字段是本地包,但您可能会在其他地方设置这些字段。此外,这里还有一个NPE问题:

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

我不确定你是否在发现事情发生之前或之后添加了这个内容,但这是一个肯定的NullPointerException,它会导致其余的绘制代码在抛出时不执行

编辑:我假设你已经注释掉了那些无效的代码,但这是你想要的工作。