Reputation: 1
I'm trying to recreate a simple version of Brick Breaker for school and I'm having trouble creating the bricks. (I'm also very new to Java and Stack Overflow...)
My first idea upon creating the bricks was to make them an ArrayList and then use a for-loop to add more brick objects to fill up the screen. I've only been able to get a single brick object to appear on the screen and I'm not sure how to paint the others. I've tried creating a 2D Array as well to make a map, but that didn't work out either. Sometimes the JFrame would just go blank and nothing else would show up.
This is my code for the Board class where the paintComponent is located:
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);
}
}
}
And this is my code the for the Brick Class:
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);
}
}
I thought that if I added a new brick to the list, it would create a new one in the color green. I thought it would run 20 times too since that's what I set the capacity too, but nothing happened. Nothing changed in my program from the way it was. Only one brick still appeared in the corner and no new bricks appeared.
Upvotes: 0
Views: 1015
Reputation: 285405
Not this -- you're adding new bricks within a painting component, makes no sense.
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);
}
}
Why not instead just paint the bricks in the for loop, do nothing but paint:
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);
}
}
Upvotes: 1