Pato
Pato

Reputation: 13

Multi-colored Grid

my assignment is make a gaming generator for 3 different types of puzzle game. i just have to finish up the last game which is Bejeweled. i have made the grid already using the GridLayout filled with buttons.

i just need to apply 7 different colors on all the buttons. ive tried using this code before:

String[] backgroundColors = {"CYAN","PINK","YELLOW"};  
int number = (int)(Math.random() * 3);  
String c = (backgroundColors[number]);  

(then after i add the button to the pane i dd this:)

buttonBejeweled.setBackgroundColor(c);

and it failed. i thought maybe i should use and array but ive searched and haven't found any unfortunately. Please help me out with a random color generator, by use of arrays preferably.

Upvotes: 1

Views: 511

Answers (2)

Anaun Emous
Anaun Emous

Reputation: 96

Does the .setBackgroundColor() use a String parameter? If it doesn't work I'm guessing that it uses a parameter of type Color. Have you imported the color library? If not, to access colors you have to use the Color class and access colors using Color.CYAN for example, and so you would do

Color[] backgroundColors = {Color.CYAN,Color.PINK,Color.YELLOW};  
int number = (int)(Math.random() * 3);  
Color c = (backgroundColors[number]); 
buttonBejeweled.setBackgroundColor(c);

Upvotes: 1

mercutio
mercutio

Reputation: 1066

You can use

Color[] backgroundColors = {Color.RED,Color.GREEN,Color.BLUE};  
int number = (int)(Math.random() * 3);  
Color c = (backgroundColors[number]); 

Upvotes: 1

Related Questions