简体   繁体   中英

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.

You can use

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

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);

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