-1

so, i'm doing breakout in java (this is not h/w, school is over)

I want to bricks to be random in color, but my bricks change color while the game is running. so right now, it looks like the bricks are lighting up!! please help!!

package main;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.Random;

public class Brick {
    public static final int X = 0, Y = 0, ROW_SIZE = 8, COL_SIZE = 5;
    private Random random = new Random();
    private ArrayList<Rectangle> arr = new ArrayList<>();

public Brick(int width, int height) {
    for(int i = 0; i < COL_SIZE; i++){
        for(int j = 0; j < ROW_SIZE; j++) {
            Rectangle r = new Rectangle(X + (j * (width / ROW_SIZE)), 
                        Y + (i * (height / COL_SIZE)), (width / ROW_SIZE), (height / COL_SIZE));
            arr.add(r); 
        }
    }
}

public ArrayList<Rectangle> getList(){
    return arr;
}

public void setList(ArrayList<Rectangle> rects){
    arr = rects;
}

public void paint(Graphics g){
    for(Rectangle rect : arr){
        g.setColor(new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
        g.fillRect(rect.x, rect.y, rect.width, rect.height);
    }
}

}

package main;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;

public class GamePanel extends JPanel implements ActionListener, KeyListener {

Player player = new Player();
Ball ball = new Ball();
Brick brick = new Brick(Pong.WIDTH, Pong.HEIGHT / 3);
JLabel label, gameOverLabel;
Timer time;

public GamePanel() {
    super();
    setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    time = new Timer(50, this);
    time.start();

    this.addKeyListener(this);
    setFocusable(true);

    // Displaying score
    label = new JLabel();
    gameOverLabel = new JLabel();
    gameOverLabel.setPreferredSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE));
    gameOverLabel.setAlignmentX(CENTER_ALIGNMENT);
    label.setAlignmentX(CENTER_ALIGNMENT);
    add(label);
    add(gameOverLabel);

}

private void update() {
    player.update();

    if (ball.update()) {
        gameOverLabel.setText("GAME OVER");
        time.stop();
    }

    ball.checkCollisionWith(player);
    ball.checkBrickCollision(brick);
}

public void paintComponent(Graphics g) {
    g.setColor(Color.LIGHT_GRAY);
    g.fillRect(0, 0, getWidth(), getHeight());

    player.paint(g);
    ball.paint(g);
    brick.paint(g);
}

public void actionPerformed(ActionEvent e) {
    update();
    label.setText("Score: " + ball.getScore());
    repaint();
}

@Override
public void keyPressed(KeyEvent e) {
    // Speed of player
    int playerVelocity = 8;

    if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
        player.setXVelocity(playerVelocity);
    } else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
        player.setXVelocity(-playerVelocity);
    } else if (e.getKeyCode() == KeyEvent.VK_R) {
        ball = new Ball();
        player = new Player();
        ball.setScore(0);
        label.setText("Score: " + ball.getScore());
        gameOverLabel.setText("");
        repaint();
        time.start();
    }
}

@Override
public void keyReleased(KeyEvent e) {
    player.setXVelocity(0);
}

@Override
public void keyTyped(KeyEvent e) {
}

}

please help! thank you...

Soon Kwon
  • 25
  • 1
  • 7
  • repaint() is so that the game refreshes after it goes through the first iteration. If i were to comment repaint(), the game would start, but the ball and the player will not move. – Soon Kwon May 10 '16 at 05:11
  • i believe that my problem lies where I do the for loop on the arraylist of bricks, and setting the color as random. So everytime the compiler goes through the bricks, it changes color... – Soon Kwon May 10 '16 at 05:12

1 Answers1

0
public void paint(Graphics g){
    for(Rectangle rect : arr){
        g.setColor(new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
        g.fillRect(rect.x, rect.y, rect.width, rect.height);
    }
}

Indeed it change color on every repaint - you're creating new random Color on every call. I think you should create color in Brick constructor and reuse it.

Mikhail Kuchma
  • 583
  • 2
  • 9
  • @soon-kwon, yes, but I think you'd to it yourself. – Mikhail Kuchma May 10 '16 at 13:47
  • I wasn't asking for the complete answer, but instructions on how to get started...like, put a code that does this at this point. I have a constructor, but I don't know what kind of code to put in it... – Soon Kwon May 10 '16 at 13:55