在Java中创建随机颜色?

Elt*_*.fd 58 java random colors

我想在Java应用程序中的JPanel上绘制随机彩色点.有没有任何方法可以创建随机颜色?

Gre*_*reg 107

使用随机库:

import java.util.Random;
Run Code Online (Sandbox Code Playgroud)

然后创建一个随机生成器:

Random rand = new Random();
Run Code Online (Sandbox Code Playgroud)

由于颜色分为红绿和蓝,您可以通过创建随机原色来创建新的随机颜色:

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

然后最终创建颜色,将原色传递给构造函数:

Color randomColor = new Color(r, g, b);
Run Code Online (Sandbox Code Playgroud)

您还可以使用此方法创建不同的随机效果,例如创建随机颜色,更强调某些颜色...传递较少的绿色和蓝色以产生"粉红色"随机颜色.

// 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;
Run Code Online (Sandbox Code Playgroud)

或者为了确保仅生成"浅色"颜色,您可以生成每个颜色元素始终> 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;
Run Code Online (Sandbox Code Playgroud)

还有其他各种颜色函数可以与Color类一起使用,例如使颜色更亮:

randomColor.brighter();
Run Code Online (Sandbox Code Playgroud)

Color可以在此处阅读该类的概述:http://download.oracle.com/javase/6/docs/api/java/awt/Color.html

  • 你的代码只会产生明亮/浅色不会起作用.next float不接受float也不接受double作为参数,添加.5使颜色超出范围. (2认同)

Boa*_*ann 35

随机RGB值的单行:

new Color((int)(Math.random() * 0x1000000))
Run Code Online (Sandbox Code Playgroud)


Sua*_*ehi 31

如果您想要令人愉悦的柔和色彩,最好使用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);
Run Code Online (Sandbox Code Playgroud)


Kom*_*lot 17

复制粘贴,以获得明亮柔和的彩虹色

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);
Run Code Online (Sandbox Code Playgroud)


Rob*_*ett 9

如果您不希望它看起来很糟糕,我建议在数组中定义颜色列表,然后使用随机数生成器来选择一个颜色.

如果你想要一个真正的随机颜色,你可以生成0到255之间的3个随机数,然后使用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);
Run Code Online (Sandbox Code Playgroud)


Sha*_*ild 5

我知道这个答案有点晚了,但我没有看到其他人这样做.

像Greg说的那样,你想使用Random类

Random rand = new Random();
Run Code Online (Sandbox Code Playgroud)

但我要说的差别很简单:

Color color = new Color(rand.nextInt(0xFFFFFF));
Run Code Online (Sandbox Code Playgroud)

就这么简单!不需要生成许多不同的浮点数.


Dur*_*nat 5

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;
    }
}
Run Code Online (Sandbox Code Playgroud)


Dee*_*har 5

我使用了这种简单而巧妙的方法在 Java 中创建随机颜色,

Random random = new Random();
        System.out.println(String.format("#%06x", random.nextInt(256*256*256)));
Run Code Online (Sandbox Code Playgroud)

其中#%06x为您提供零填充的十六进制(始终为 6 个字符长)。