如何在Java中绘制对象的ArrayList?

问题描述 投票:0回答:1

我正在尝试为学校重新创建一个简单版本的Brick Breaker,我在创建砖块时遇到了麻烦。 (我也是Java和Stack Overflow的新手...)

我创建砖块的第一个想法是将它们设为ArrayList,然后使用for循环添加更多砖块对象来填充屏幕。我只能让一个砖块对象出现在屏幕上,我不知道如何绘制其他砖块。我也尝试过创建一个2D数组来制作地图,但这也没有用。有时JFrame会变成空白而没有其他任何东西出现。

这是我的paintComponent所在的Board类的代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;


public class Board extends JPanel implements ActionListener {

    Ball ball;
    Game game;
    Paddle paddle;
    Timer timer;
    Brick brick;
    private final int edge = 100;
    private List<Brick> brickList;


    public Board(Game game){
        setPreferredSize(new Dimension(600,800));
        setBackground(Color.BLACK);
        this.game = game;
        ball = new Ball(this, this.paddle);
        paddle = new Paddle(this.game, this, this.ball);
        brick = new Brick(this.ball);
    }
    public void init(){
        brickList = new ArrayList<Brick>(20);
        ball.setPosition(getWidth()/2,getHeight()/2);
        paddle.setPosition(getWidth()/2,getHeight()-edge);
        brick.setPosition(edge,edge);
        timer = new Timer(1000/60,this);
        timer.start();
    }
    public void actionPerformed(ActionEvent e){
        ball.move();
        paddle.move();
        ball.checkCollision(paddle);
        brick.checkCollision(ball);
        repaint();
    }
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.setColor(Color.lightGray);
        ball.paint(g);
        g.setColor(Color.white);
        paddle.paint(g);
        g.setColor(Color.red);
        brick.paint(g);
          for(int i = 0; i < brickList.size(); i++){
            brickList.add(new Brick(this.ball));
            g.setColor(Color.green);
        }
      }
    }

这是我的砖类类代码:

import java.awt.*;
import java.util.ArrayList;

public class Brick {
    /* Four Rows of Five Bricks = Total 20 Bricks */
    private int width =  120;
    private int height = 80;
    private int x,y;
    private Ball ball;

    public Brick(Ball ball){
        x = 0;
        y = 0;
        this.ball = ball;
    }
    public void setPosition(int x, int y){
        this.x =  x - width/2;
        this.y = y - height/2;
    }
    public Rectangle getBounds(){
        return new Rectangle(x,y,width,height);
    }
    public void checkCollision(Ball ball){
        if(getBounds().intersects(ball.getBounds())){
            /* Will fill in later */
        }
    }

   /* public int getWidth(){return width;}
    public int getHeight(){return height;}
    public int getX(){return x;}
    public int getY(){return y;} */

    public void paint(Graphics g){
        g.fillRect(x,y,width,height);
    }

}

我想如果我在列表中添加了一个新砖,它会创建一个绿色的新砖。我认为它也会运行20次,因为这也是我设置的容量,但没有发生任何事情。我的程序没有任何改变。角落里只出现了一块砖,没有出现新的砖块。

java for-loop arraylist paint paintcomponent
1个回答
1
投票

不是这个 - 你在绘画组件中添加新砖,没有任何意义。

public void paintComponent(Graphics g){
    super.paintComponent(g);
    g.setColor(Color.lightGray);
    ball.paint(g);
    g.setColor(Color.white);
    paddle.paint(g);
    g.setColor(Color.red);
    brick.paint(g);
      for(int i = 0; i < brickList.size(); i++){
        brickList.add(new Brick(this.ball)); // ???? what the heck???
        g.setColor(Color.green);
    }
  }

为什么不只是在for循环中绘制砖块,除了绘制之外什么都不做:

public void paintComponent(Graphics g){
    super.paintComponent(g);
    g.setColor(Color.lightGray);
    ball.paint(g);
    g.setColor(Color.white);
    paddle.paint(g);
    g.setColor(Color.red);
    // brick.paint(g); // not sure what you're painting here, what brick

    // consider setting brick color here:
    // g.setColor(BRICK_COLOR);  // assuming use of a constant
    for(int i = 0; i < brickList.size(); i++){
        // brickList.add(new Brick(this.ball));
        // g.setColor(Color.green);
        brickList.get(i).paint(g);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.