问题描述
我正在尝试为学校重新创建一个简单版本的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次,因为这也是我设置的容量,但是什么也没发生。 我的程序没有任何改变。 角落里只剩下一块砖,没有新砖出现。
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);
}
}