It's not a secret anymore!
The moose likes Beginning Java and the fly likes Random Colors???HELP! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Random Colors???HELP!" Watch "Random Colors???HELP!" New topic

Random Colors???HELP!

Annette Pomeroy

Joined: Nov 22, 2001
Posts: 2
I was wondering if anyone could tell me which method would i use to randomly generate colors, when i am not sure how many times i need it generated.
Rowan Brownlee
Ranch Hand

Joined: Aug 07, 2000
Posts: 97
Hi Annette,
The closest I've gotten to this is setting up an array of colours and cycling through them over and over. Though this isn't random, I suppose you could repeatedly generate a random number between 0 and the number of colours in the array to choose a colour at random.
I haven't tried to generate customised colours, but looking at the constructors in the Color class, it looks like you can feed in your own (randomly generated )numbers to make a colour. For generating random numbers, there's the Random class, and Math.random.
Constructor example from Color API
Color(int r, int g, int b, int a)
Creates an sRGB color with the specified red, green, blue, and alpha values in the range (0 - 255).
Annette Pomeroy

Joined: Nov 22, 2001
Posts: 2
Thanks for your help, I appreciate it much!
I will try what u have told me.
Thanks again,
Rikard Qvarforth
Ranch Hand

Joined: Jul 10, 2001
Posts: 107
maby you could do something like this ..
Color ColorTest(){
int R = 0;
int G = 0;
int B = 0;
R = (int)(1+Math.random()*(255));
G = (int)(1+Math.random()*(255));
B = (int)(1+Math.random()*(255));
return new Color(R,G,B));
hope it helps ..
I agree. Here's the link:
subject: Random Colors???HELP!
It's not a secret anymore!