在 Java 中创建随机颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4246351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating random colour in Java?
提问by Elton.fd
I want to draw random coloured points on a JPanel in a Java application. Is there any method to create random colours?
我想在 Java 应用程序的 JPanel 上绘制随机彩色点。有什么方法可以创建随机颜色吗?
采纳答案by Greg
Use the random library:
使用随机库:
import java.util.Random;
Then create a random generator:
然后创建一个随机生成器:
Random rand = new Random();
As colours are separated into red green and blue, you can create a new random colour by creating random primary colours:
由于颜色分为红绿蓝,您可以通过创建随机原色来创建新的随机颜色:
// 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 colour, pass the primary colours into the constructor:
然后最终创建颜色,将原色传递给构造函数:
Color randomColor = new Color(r, g, b);
You can also create different random effects using this method, such as creating random colours with more emphasis on certain colours ... pass in less green and blue to produce a "pinker" random colour.
您还可以使用此方法创建不同的随机效果,例如创建更强调某些颜色的随机颜色……传入较少的绿色和蓝色以产生“粉红色”随机颜色。
// 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" colours are generated, you can generate colours that are always > 0.5 of each colour element:
或者为了确保只生成“浅色”颜色,您可以生成每个颜色元素总是 > 0.5 的颜色:
// 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 colour functions that can be used with the Color
class, such as making the colour brighter:
还有各种其他颜色函数可以与Color
类一起使用,例如使颜色更亮:
randomColor.brighter();
An overview of the Color
class can be read here: http://download.oracle.com/javase/6/docs/api/java/awt/Color.html
Color
可以在此处阅读该类的概述:http: //download.oracle.com/javase/6/docs/api/java/awt/Color.html
回答by Rob Stevenson-Leggett
If you don't want it to look horrible I'd suggest defining a list of colours in an array and then using a random number generator to pick one.
如果您不希望它看起来很糟糕,我建议在数组中定义一个颜色列表,然后使用随机数生成器来选择一个。
If you want a truly random colour you can just generate 3 random numbers from 0 to 255 and then use the Color(int,int,int) constructor to create a new Color instance.
如果你想要一个真正随机的颜色,你可以只生成 3 个从 0 到 255 的随机数,然后使用 Color(int,int,int) 构造函数创建一个新的 Color 实例。
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);
回答by Jason Nichols
You can instantiate a color 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).
您可以使用三个浮点数(r、g、b)实例化颜色,每个浮点数在 0.0 和 1.0 之间:http: //download.oracle.com/javase/6/docs/api/java/awt/Color.html#Color(浮动,%20浮动,%20浮动)。
Using Java's Random class you can easily instantiate a new random color as such:
使用 Java 的 Random 类,您可以轻松地实例化一个新的随机颜色:
Random r = new Random();
Color randomColor = new Color(r.nextFloat(), r.nextFloat(), r.nextFloat());
I can't guarantee they'll all be pretty, but they'll be random =)
我不能保证它们都很漂亮,但它们是随机的 =)
回答by Jay
Sure. Just generate a color using random RGB values. Like:
当然。只需使用随机 RGB 值生成颜色。喜欢:
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 vary up the generation of the random numbers if you don't like the colors it comes up with. I'd guess these will tend to be fairly dark.
如果您不喜欢随机数产生的颜色,您可能希望改变随机数的生成。我猜这些会很暗。
回答by Ishtar
You seem to want light random colors. Not sure what you mean exactly with light. But if you want 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
);
Search for HSB color model for more information.
搜索 HSB 颜色模型以获取更多信息。
回答by Sualeh Fatehi
If you want pleasing, pastel colors, it is best to use the HLS system.
如果您想要令人愉悦的柔和色彩,最好使用 HLS 系统。
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);
回答by Komplot
Copy paste this for bright 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);
回答by user1942990
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;
}
}
回答by Boann
A one-liner for random RGB values:
随机 RGB 值的单行:
new Color((int)(Math.random() * 0x1000000))
回答by Shaun Wild
I know it's a bit late for this answer, but I've not seen anyone else put this.
我知道这个答案有点晚了,但我还没有看到其他人提出这个问题。
Like Greg said, you want to use the Random class
就像 Greg 说的,你想使用 Random 类
Random rand = new Random();
but the difference I'm going to say is simple do this:
但我要说的区别很简单,这样做:
Color color = new Color(rand.nextInt(0xFFFFFF));
And it's as simple as that! no need to generate lots of different floats.
就这么简单!无需生成大量不同的浮点数。