【问题标题】:How to paint an ArrayList of Objects in Java?如何在 Java 中绘制对象的 ArrayList?
【发布时间】:2019-02-13 03:23:56
【问题描述】:

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

我在创建积木时的第一个想法是让它们成为一个 ArrayList,然后使用 for 循环添加更多积木对象以填满屏幕。 我只能让一个砖块对象出现在屏幕上,我不知道如何绘制其他对象。我也尝试过创建一个二维数组来制作地图,但这也没有成功。有时 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);
        }
      }
    }

这是我的 Brick 类代码:

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】:

    不是这个——你在绘画组件中添加新的砖块,没有意义。

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

    【讨论】:

    • 我试过了,但它只会让原来的砖消失。我应该在 init() 中制作砖块吗?谢谢!
    • @Hannah “我应该在 init() 中制作砖块吗?” 这将是一个比@987654323 更好的地方@
    • 向 init() 类添加砖块本身是可行的,谢谢!但我仍然很难让不止一个出现。我不知道我现在所拥有的是否会起作用,我正在尝试做的是向 ArrayList 添加更多砖块并让它们出现在 JPanel 上。当在 init() 中添加相同的 for 循环并尝试在 for 循环中添加砖块时,我的屏幕变白了。对不起,我不太擅长 arrayLists :(
    • 再一次,除了绘画之外,不要使用您的绘画方法。不要在那里制造砖块。弄清楚如果你想在程序运行时添加什么事件应该触发砖块创建
    • 谢谢。我将此代码添加到 init() 并修复了我的代码。 brickList = new ArrayList(); for(int col = 0; col
    猜你喜欢
    • 1970-01-01
    • 2015-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-10
    • 1970-01-01
    • 2017-09-19
    • 1970-01-01
    相关资源
    最近更新 更多