This is the code I used to generate random colors in JPanel. You can adjust the values for hue & saturation to get the color you want.
Don't forget to import java.awt.Color & java.util.Random
public Color generateColor(){
Color color;
Random random = new Random();
//to get rainbow, pastel colors
final float hue = random.nextFloat();
final float saturation = 1.0f; //1.0 for brilliant, 0.0 for dull
final float luminance = 0.6f; //1.0 for brighter, 0.0 for black
color = Color.getHSBColor(hue, saturation, luminance);
return color;
}
Usage: MyPanel.setBackground(generateColor());
No comments:
Post a Comment