mardi 22 août 2017

Drawing a grid with 4 random colors [duplicate]

This question already has an answer here:

I have 4 colors that I have to use to fill randomly in a 20 x 20 grid

public void paint(Graphics g){

        Random rand = new Random();
        Color dirtBrown = new Color(102,51,0);
        Color grassGreen = new Color(102,153,0);
        Color treeGreen = new Color(0,102,0);
        Color rockGrey = new Color(182,182,182);

        Color textile [] = new Color[]{dirtBrown, grassGreen,treeGreen,rockGrey};

        for (int i = 0; i < 20; i++) {
            for (int j = 0; j < 20; j++) {
                int x = i * 35 + 10;
                int y = j * 35 + 10;

                // set 2d array cell a random color in textile
                g.setColor(rand.nextInt(textile)); //ERROR here
                g.fillRect(x, y, 35, 35);

                //border
                g.setColor(Color.BLACK);
                g.drawRect(x, y, 35, 35);
            }
        }
}

What am I supposed to write here to make it pick a random color in the Color textile




Aucun commentaire:

Enregistrer un commentaire