Creating random color in Java?

I want to draw random colored dots on a JPanel in a Java application. Is there any way to create random colors?

+49
java random colors
Nov 22 2018-10-22
source share
13 answers

Use a random library:

import java.util.Random; 

Then create a random generator:

 Random rand = new Random(); 

When the colors are divided into red green and blue, you can create a new random color by creating random primary colors:

 // Java 'Color' class takes 3 floats, from 0 to 1. float r = rand.nextFloat(); float g = rand.nextFloat(); float b = rand.nextFloat(); 

Then, to finally create the color, pass the primary colors to the constructor:

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



You can also create various random effects using this method, for example, create random colors with a lot of attention to certain colors ... skip less green and blue to create a more pink random color.

 // Will produce a random colour with more red in it (usually "pink-ish") float r = rand.nextFloat(); float g = rand.nextFloat() / 2f; float b = rand.nextFloat() / 2f; 

Or to ensure that only "light" colors are generated, you can create colors that are always> 0.5 of each color element:

 // Will produce only bright / light colours: float r = rand.nextFloat() / 2f + 0.5; float g = rand.nextFloat() / 2f + 0.5; float b = rand.nextFloat() / 2f + 0.5; 

There are various other color functions that you can use with the Color class, for example, to make the color brighter:

 randomColor.brighter(); 

An overview of the Color class can be found here: http://download.oracle.com/javase/6/docs/api/java/awt/Color.html

+93
Nov 22 2018-10-22
source share

Single line for random RGB values:

 new Color((int)(Math.random() * 0x1000000)) 
+28
Nov 17 '13 at 14:46
source share

If you like pleasant, pastel colors, it is best to use the HLS system.

 final float hue = random.nextFloat(); // Saturation between 0.1 and 0.3 final float saturation = (random.nextInt(2000) + 1000) / 10000f; final float luminance = 0.9f; final Color color = Color.getHSBColor(hue, saturation, luminance); 
+25
Nov 22 '10 at 15:45
source share

Copy this for vibrant pastel rainbow colors.

 int R = (int)(Math.random()*256); int G = (int)(Math.random()*256); int B= (int)(Math.random()*256); Color color = new Color(R, G, B); //random color, but can be bright or dull //to get rainbow, pastel colors Random random = new Random(); final float hue = random.nextFloat(); final float saturation = 0.9f;//1.0 for brilliant, 0.0 for dull final float luminance = 1.0f; //1.0 for brighter, 0.0 for black color = Color.getHSBColor(hue, saturation, luminance); 
+17
Jan 05 2018-12-12T00: 00Z
source share

If you don't want this to look terrible, I would suggest defining a list of colors in an array, and then use a random number generator to select it.

If you want a truly random color, you can simply generate 3 random numbers from 0 to 255, and then use the Color (int, int, int) constructor to create a new Color instance.

 Random randomGenerator = new Random(); int red = randomGenerator.nextInt(256); int green = randomGenerator.nextInt(256); int blue = randomGenerator.nextInt(256); Color randomColour = new Color(red,green,blue); 
+9
Nov 22 2018-10-22
source share

I know this is a little late for this answer, but I have not seen anyone else do it.

As Greg said, you want to use the Random class

 Random rand = new Random(); 

but the difference I'm going to say is simple:

 Color color = new Color(rand.nextInt(0xFFFFFF)); 

And it's that simple! no need to create many different floats.

+3
Feb 11 '14 at 11:31
source share
 import android.graphics.Color; import java.util.Random; public class ColorDiagram { // Member variables (properties about the object) public String[] mColors = { "#39add1", // light blue "#3079ab", // dark blue "#c25975", // mauve "#e15258", // red "#f9845b", // orange "#838cc7", // lavender "#7d669e", // purple "#53bbb4", // aqua "#51b46d", // green "#e0ab18", // mustard "#637a91", // dark gray "#f092b0", // pink "#b7c0c7" // light gray }; // Method (abilities: things the object can do) public int getColor() { String color = ""; // Randomly select a fact Random randomGenerator = new Random(); // Construct a new Random number generator int randomNumber = randomGenerator.nextInt(mColors.length); color = mColors[randomNumber]; int colorAsInt = Color.parseColor(color); return colorAsInt; } } 
+3
Dec 28 '15 at 16:45
source share

You can create a color instance with three floats (r, g, b), each between 0.0 and 1.0: http://download.oracle.com/javase/6/docs/api/java/awt/Color.html#Color ( float,% 20float,% 20float ).

Using the Java Random class, you can easily create a new random color as such:

 Random r = new Random(); Color randomColor = new Color(r.nextFloat(), r.nextFloat(), r.nextFloat()); 

I can not guarantee that everyone will be beautiful, but they will be random =)

+1
Nov 22 '10 at 14:27
source share

Sure. Just generate color using random RGB values. How:

 public Color randomColor() { Random random=new Random(); // Probably really put this somewhere where it gets executed only once int red=random.nextInt(256); int green=random.nextInt(256); int blue=random.nextInt(256); return new Color(red, green, blue); } 

You might want to change the generation of random numbers if you don't like the colors he encounters. I suppose they will be quite dark.

+1
Nov 22 '10 at 2:30 p.m.
source share

You seem to need light, random colors. Not sure what you mean with light. But if you need random β€œrainbow colors,” try this

 Random r = new Random(); Color c = Color.getHSBColor(r.nextFloat(),//random hue, color 1.0,//full saturation, 1.0 for 'colorful' colors, 0.0 for grey 1.0 //1.0 for bright, 0.0 for black ); 

Find the HSB color model for more information.

+1
Nov 22 '10 at 15:37
source share
 package com.adil.util; /** * The Class RandomColor. * * @author Adil OUIDAD * @URL : http://kizana.fr */ public class RandomColor { /** * Gets the random color. * * @return the random color */ public static String getRandomColor() { String[] letters = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"}; String color = "#"; for (int i = 0; i < 6; i++ ) { color += letters[(int) Math.round(Math.random() * 15)]; } return color; } } 
0
Jun 08 '13 at 20:54 on
source share

Here is a way to get a random color:

 private static Random sRandom; public static synchronized int randomColor() { if (sRandom == null) { sRandom = new Random(); } return 0xff000000 + 256 * 256 * sRandom.nextInt(256) + 256 * sRandom.nextInt(256) + sRandom.nextInt(256); } 

Benefits:

  • Get an integer view that can be used with java.awt.Color or android.graphics.Color
  • Keep a static link to Random .
0
Jan 19 '15 at 8:54
source share

It would be helpful.

 Random randomGenerator = new Random(); int red = randomGenerator.nextInt(255); int green = randomGenerator.nextInt(255); int blue = randomGenerator.nextInt(255); Color randomColour = new Color(red,green,blue); 
-one
Dec 03 '14 at 5:27
source share



All Articles