简体   繁体   中英

Bricks change color every instance of time

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...

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM