0

I need to set a JPanel background with light colors only in order not to cover the text (in bold black).

At this moment I have this:

import java.util.Random;

....

private Random random = new Random();
private JPanel panel = new JPanel();
panel.setBackground( new Color( random.nextInt() ) );

But it generates "any" random color. How can I do that?

Thanks

Frank
  • 2,083
  • 8
  • 34
  • 52

3 Answers3

8

Color has a constructor that takes three values for red, green and blue. If you give each of them a random value from 100 or so to 255, you will get only light colors.

The usage of the HSB system might give even better results, e.g. new Color(Color.HSBtoRGB((float) Math.random(), (float) Math.random, 0.5F + ((float) Math.random())/2F));

Landei
  • 54,104
  • 13
  • 100
  • 195
4

Create a color from RGB values, and make sure that R, G and B are large enough to make the overall color light. [0, 0, 0] is black. And [255, 255, 255] is white. So you could generate a color with values for R, G and B being random values between 200 and 255 for example.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
2

I would recommend not using random colors as a background. It will print ugly background colors and make your program look sloppy.

I recommend you to make a list of beautiful colors and randomly choose one of the colors in the list.

You can find a list of beautiful colors from the webpages below.

http://colorschemedesigner.com/

http://www.colorcombos.com/

http://www.color-swatches.com/top-swatches.html

Heejin
  • 4,463
  • 3
  • 26
  • 30