This site requires JavaScript, please enable it in your browser!
Greenfoot back
Dmi243m23
Dmi243m23 wrote ...

2024/8/8

Can't change color of a brick

Dmi243m23 Dmi243m23

2024/8/8

#
Greetings, I am making a Breakout/Arkanoid game. I made a method that makes a brick wall. MyWorld:
private Color[] colors = {
    Color.RED,
    Color.ORANGE,
    Color.YELLOW,
    Color.GREEN,
    Color.CYAN,
    Color.BLUE,
    Color.MAGENTA,
    Color.PINK
};
...
private void makeWall()
{
    for (int i = 0; i < 8; i++)
    {
        for (int j = 0; j < 5; j++)
        {
            int color = Greenfoot.getRandomNumber(colors.length);
            Brick brick = new Brick(colors[color]);
            int width = brick.getImage().getWidth();
            int height = brick.getImage().getHeight();
            addObject(brick, width * (i + 1) - width / 2, height * (j + 1) - height / 2);
        }
    }
}
I set color of a brick in its constructor, but the color doesn't change at all. Can someone please help me? Brick:
public Brick(Color color)
{
    getImage().scale(100, 50);
    getImage().fillRect(0, 100, 0, 50);
    getImage().setColor(color);
}
danpost danpost

2024/8/8

#
Dmi243m23 wrote...
I set color of a brick in its constructor, but the color doesn't change at all. Can someone please help me? << Code Omitted >>
Try setting the drawing color BEFORE filling the rectangle in (switch lines 4 and 5 in your Brick class constructor).
You need to login to post a reply.