[英]How to paint an ArrayList of Objects in Java?
我正在嘗試為學校重新創建一個簡單版本的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次,因為這也是我設置的容量,但是什么也沒發生。 我的程序沒有任何改變。 角落里只剩下一塊磚,沒有新磚出現。
不是這樣-您要在繪畫組件中添加新的積木,這毫無意義。
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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.