Is there a way to randomize specific colors across a 10x10 grid/board in java?

Viewed 95

Currently, my code runs so that it creates a 10x10 board with random colors for each square but what I want it to do is for it to randomize specific colors (red, green, blue, yellow) across the board.

public static class TestPane extends JPanel {

    protected static final int ROWS = 10;
    protected static final int COLS = 10;
    protected static final int BOX_SIZE = 50;

    private List<Color> colors;

    public TestPane() {
        int length = ROWS * COLS;
        colors = new ArrayList<>(length);
        for (int index = 0; index < length; index++) {
            int c1 = (int) (Math.random() * 255);
            int c2 = (int) (Math.random() * 255);
            int c3 = (int) (Math.random() * 255);
            colors.add(new Color(c1, c2, c3));
        }
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(COLS * BOX_SIZE, ROWS * BOX_SIZE);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g.create();

        int xOffset = (getWidth() - (COLS * BOX_SIZE)) / 2;
        int yOffset = (getHeight() - (ROWS * BOX_SIZE)) / 2;

        System.out.println("...");
        for (int row = 0; row < ROWS; row++) {
            for (int col = 0; col < COLS; col++) {
                int index = (row * COLS) + col;
                g2d.setColor(colors.get(index));
                g2d.fillRect(xOffset + (col * BOX_SIZE), 
                                yOffset + (row * BOX_SIZE), 
                                BOX_SIZE, BOX_SIZE);
            }
        }
        g2d.dispose();
    }

Any help is much appreciated.

3 Answers

First of all don't make your class static.

Second, don't dispose your graphics object.

Next, for your particular case you could have an array of available colors:

private Color[] availableColors = new Color[] {Color.YELLOW, Color.RED, Color.BLUE, Color.GREEN};

And then fill your colors ArrayList with a randomized color from there

int length = ROWS * COLS;
colors = new ArrayList<Color>();
for (int index = 0; index < length; index++) {
    int randomColor = (int) (Math.random() * availableColors.length);
    colors.add(availableColors[randomColor]);
}

enter image description here

And next time, don't forget to add a main method to your question's code.

Make an array of desired colors of length as much as there is fields on the board, then shuffle that array with Collections.shuffle(list);, then apply the shuffled array of colors to the board.

To make that first array before shuffling, repeat the colors like that: [red, green, blue, yellow, red, green, blue, yellow, red, green, blue, yellow, ...]

There aren't lots of specific colors, but you can try with something like:

Random r = new Random();
Color c = new Color(r.nextInt(3)*127, r.nextInt(3)*127, r.nextInt(3)*127);

This should give you more specific colors. If you want them to be more specific (but less in quantity) use r.nextInt(1)*255.

Related