【问题标题】:Creating random colour in Java?在Java中创建随机颜色?
【发布时间】:2011-05-13 21:06:38
【问题描述】:

我想在 Java 应用程序的 JPanel 上随机绘制彩色点。有什么方法可以创建随机颜色吗?

【问题讨论】:

    标签: java colors random


    【解决方案1】:

    使用随机库:

    import java.util.Random;
    

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

    Random rand = new Random();
    

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

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

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

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

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

    // 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;
    

    或者为了确保只生成“浅色”颜色,您可以生成每个颜色元素始终 > 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;
    

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

    randomColor.brighter();
    

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

    【讨论】:

    • 您的代码只会产生明亮/浅色将不起作用。 next float 不接受 float 或 double 作为参数,添加 .5 会使颜色超出范围。
    • @ThomasAhle 它将产生任何可能的颜色。我不能把它画在图片上。 :)
    • 如上所述,不再存在接受参数的 nextFloat 构造函数。
    • 如果你想要更“紫色”的色调,代码会有什么变化?
    • 紫色由蓝色和红色组成,因此增加这两种颜色的数量或减少绿色的数量会使其更偏紫色。希望这会有所帮助。
    【解决方案2】:

    随机 RGB 值的单线:

    new Color((int)(Math.random() * 0x1000000))
    

    【讨论】:

      【解决方案3】:

      如果你想要讨人喜欢的柔和色彩,最好使用 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);
      

      【讨论】:

      • 你有这个生成的一些颜色的样本吗?
      【解决方案4】:

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

      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);
      

      【讨论】:

      • 我更喜欢这种选择随机颜色的方式 (HSB)。使用 RGB 系统可以创建非常奇怪的组合。附带说明一下,色调值在内部乘以 360 以产生 HSB 颜色模型 (Javadoc) 中的色调角度。
      【解决方案5】:

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

      如果你想要一个真正随机的颜色,你可以生成 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);
      

      【讨论】:

      • 这样可以轻松避开与背景颜色相同的点。
      【解决方案6】:

      我知道这个答案有点晚了,但我没有看到其他人提出这个。

      就像 Greg 说的,你想使用 Random 类

      Random rand = new Random();
      

      但我要说的区别很简单:

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

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

      【讨论】:

      • 啊。这就是为什么我必须跳过它。但是,额外的颜色不会有所作为:P
      【解决方案7】:
      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;
          }
      }
      

      【讨论】:

      • 谢谢,我知道该怎么做……但我不喜欢它,你为我节省了一些时间!
      【解决方案8】:

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

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

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

      【讨论】:

        【解决方案9】:

        您可以用三个浮点数(r、g、b)实例化一种颜色,每个浮点数在 0.0 和 1.0 之间:http://download.oracle.com/javase/6/docs/api/java/awt/Color.html#Color(float,%20float,%20float)。

        使用 Java 的 Random 类,您可以轻松地实例化一个新的随机颜色:

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

        我不能保证它们都会很漂亮,但它们会是随机的 =)

        【讨论】:

          【解决方案10】:

          当然。只需使用随机 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);
          }
          

          如果您不喜欢随机数产生的颜色,您可能希望改变随机数的生成。我猜这些会很暗。

          【讨论】:

          • 太棒了。但是,我能做些什么来创造更浅的颜色呢?
          • 您可以使用 Color.brighter() 方法使任何生成的颜色看起来像。
          • Sandra,要影响亮度,请确保随机值永远不会很暗。 0 是最暗的,255 是最亮的,所以只需执行 random.nextInt(128) + 128 例如,就不会得到任何颜色比一半亮度更暗的颜色。
          • @Stijn:同上。我可以补充一点,如果你想要更均匀的亮度,你可以让第二个值取决于第一个,第三个取决于前两个。比如说 red=nextInt(255);绿色=nextInt(255-红色);等等。你可以无休止地玩这种东西,直到你得到你想要的结果。
          • 嘿,无论谁对我投了反对票,您至少可以发表评论说明您的反对意见。啊,好吧,我的老板在填写我的薪水之前不会检查我的 Stackoverflow 分数,所以我不知道我为什么在乎。
          【解决方案11】:

          您似乎想要浅色随机颜色。不确定您对光的确切含义。但如果你想要随机的“彩虹色”,试试这个

          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
                          );
          

          搜索 HSB 颜色模型以获取更多信息。

          【讨论】:

            【解决方案12】:

            这是一种获取随机颜色的方法:

            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);
            }
            

            好处:

            • 获取可与java.awt.Colorandroid.graphics.Color 一起使用的整数表示
            • 保持对Random 的静态引用。

            【讨论】:

              【解决方案13】:
              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;
                  }
              }
              

              【讨论】:

                猜你喜欢
                • 2015-04-26
                • 1970-01-01
                • 1970-01-01
                • 2017-02-20
                • 2012-08-24
                • 1970-01-01
                • 1970-01-01
                • 2012-12-15
                • 1970-01-01
                相关资源
                最近更新 更多