1

i want to choose randomly colors, but only between of them (Red, Blue, Green and Yellow), here some of the code i'm trying.

public class LittleBall extends JPanel {

private Random random = new Random();
private float r = random.nextFloat();
private float g = random.nextFloat();
private float b = random.nextFloat();

.....

public void paint (Graphics g) {

Color randomColor = new Color(r, this.g, b);

g.setColor(randomColor);

    }

}

But this just give every color in the world o.O, of course because the nextFloat of the r,g, and b variables is giving random numbers. But i just want to give go between colors.

Thanks.

Memphys
  • 13
  • 5
  • Also consider a shuffled color lookup table, suggested [here](http://stackoverflow.com/a/2374625/230513). – trashgod Apr 27 '14 at 00:40

4 Answers4

2

Create an array of Colors and get a Color randomly.

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

Color randomColor = colors[(int)( Math.random() * 4)];

// or try this one
//Color randomColor = colors[new Random().nextInt(4)];
Braj
  • 46,415
  • 5
  • 60
  • 76
0
 Random generator = new Random(); 
  float i = generator.nextInt(4)+1;
Asif Bhutto
  • 3,916
  • 1
  • 24
  • 21
  • Please have a look at OP question : `choose randomly colors, but only between of them (Red, Blue, Green and Yellow)` Is it your answer? – Braj Apr 26 '14 at 19:13
  • @Brj see doc http://docs.oracle.com/javase/7/docs/api/java/awt/Color.html. I know it well. – Asif Bhutto Apr 26 '14 at 19:19
0

use as below

  Random rand = new Random();

private int r = rand.nextInt(4) + 1;
private int g =  rand.nextInt(4) + 1;
private int b =  rand.nextInt(4) + 1;
private int y =  rand.nextInt(4) + 1;

    Color randomColor = new Color(r, g, b,y);

    g.setColor(randomColor);
Karibasappa G C
  • 2,686
  • 1
  • 18
  • 27
0

Math.random() returns a random number between 0 and 1. By multiplying this value by a range of numbers, you can choose any value between the min and max. Then you will likely need to cast it to an int in order to get an actual value for R, G, and/or B.

int r = (int) Math.random() * ( max - min )

will give you an r value between the specified min and max. Therefore, you can select the range as follows:

int r = (int) Math.random() * ( 255 - 100 )

will give you a value for Red between 100 and 255. You can, of course, just do the subtraction in your head and just put

int r = (int) Math.random() * ( 155 )

You can repeat this value for Green and Blue as well.

You can probably also use float values if you like. The int will simply truncate the decimal values from your result.

EDIT: Seeing what you have so far, you should definitely use float, not int.

DerStrom8
  • 1,311
  • 2
  • 23
  • 45
  • Thanks, but i can use the Random class if i want? like @Braj example? whats the best? – Memphys Apr 26 '14 at 19:18
  • You can certainly use the Random class if you want. I think the above is just more neat and condensed, which is why I recommended it. Choice is yours though =) – DerStrom8 Apr 26 '14 at 19:19