【问题标题】:How to make dynamic color grid with java如何用java制作动态颜色网格
【发布时间】:2010-10-07 08:06:51
【问题描述】:

我想制作一个 Java 程序来创建一些动态颜色网格。比如说我要制作 10 个网格,那么颜色网格的间距会很宽。 它是30,那么它将是紧密间隔的。这是一张图片:http://i.stack.imgur.com/D2yba.png

每个网格都应该负责制作一个事件。 哦,是的,会有一系列的颜色。 10 t0 100 之类的东西。

我首先尝试了随机函数。我们知道 rgb 颜色范围是 0 到 255。所以我尝试制作一些随机颜色,但一种颜色可以再次出现,然后它会弄乱我的整个事情。我想要一些没有重复的独特颜色网格。实际上,我想要与图像完全相同的东西。

谁能帮我解决这些问题!

【问题讨论】:

  • 不,这不是家庭作业或作业。

标签: java colors


【解决方案1】:

您可以先查看JColorChooser,如How to Use Color Choosers 中所述。产生光谱系列颜色的一种简单方法是使用HSB 模型;在 0 到 315 范围内选择具有恒定饱和度和亮度的色调。您的问题不是很具体关于事件,但这里有一个example,当鼠标移过每种颜色时显示信息。

【讨论】:

    【解决方案2】:

    一个如何操纵颜色来制作可爱彩虹的示例。

    public class RainBowColorGenerator
    {
      private static final int NR255 = 255;
      private static final int YELLOW_TRESHOLD = 200;
    
      private int r;
      private int g;
      private int b;
    
      public RainBowColorGenerator()
      {
        this(0, NR255, 0);
      }
    
      /**
       * Extra constructor so you can start with your own color in stead of the default green.
       * @param red the red component
       * @param green the green component
       * @param blue the blue component
       * @see java.awt.Color
       */
      public RainBowColorGenerator(int red, int green, int blue)
      {
        this.r = red;
        this.g = green;
        this.b = blue;
      }
    
      /**
       * Makes the next Color.
       * @return a Color
       */
      public Color nextColor()
      {
        nextRGB();
        return makeColor();
      }
    
      /**
       * Makes the next Color with a  bigger change then the unparametrized nextColor method.
       * @param jump the number of steps that should be taken, use 256 if you only want the primary colors.
       * @return a Color , yes indeed a color.
       */
      public Color nextColor( int jump )
      {
        for (int i = 0; i < jump; i++)
          {
          nextRGB();
          }
        return makeColor();
      }
    
      /**
       * Makes the next Color with a bigger change then the unparametrized nextColor method.
       * @param jump the number of steps that should be taken, use 256 if you only want the primary colors.
       * @return a Color , that will be never be really close to yellow.
       */
      public Color nextNonYellowColor( int jump )
      {
        Color color =  nextColor(jump);
        while ( color.getRed() > YELLOW_TRESHOLD && color.getGreen() > YELLOW_TRESHOLD )
          {
          color = nextColor(jump);
          }
        return color;
      }
    
      private void nextRGB()
      {
        if ( r == NR255 && g < NR255 && b == 0 )
          {
          g++;
          }
        if ( g == NR255 && r > 0 && b == 0 )
          {
          r--;
          }
        if ( g == NR255 && b < NR255 && r == 0 )
          {
          b++;
          }
        if ( b == NR255 && g > 0 && r == 0 )
          {
          g--;
          }
        if ( b == NR255 && r < NR255 && g == 0 )
          {
          r++;
          }
        if ( r == NR255 && b > 0 && g == 0 )
          {
          b--;
          }
      }
    
      private Color makeColor()
      {
        return new Color(r, g, b);
      }
    
      public static void main( String[] args )
      {
        final RainBowColorGenerator bow = new RainBowColorGenerator();
        JFrame frame = new JFrame(RainBowColorGenerator.class.toString());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final JPanel mainpanel = new JPanel()
        {
          @Override
          public void paint( Graphics g )
          {
            super.paint(g);
            int w = getWidth();
            int h = getHeight();
    
            for (int i = 0; i< w; i++)
              {
              g.setColor(bow.nextColor(2));
              g.drawLine(i,0,i,h);
              }
    
          }
        };
    
        frame.getContentPane().add(mainpanel);
        //noinspection MagicNumber
        frame.setSize(800, 800);
        frame.setVisible(true);
      }
    }
    

    【讨论】:

    【解决方案3】:

    其实我想要的东西一模一样 图片。

    该图像不显示随机颜色。它显示了颜色的不同“色调”。由于您有 30 种颜色,它基本上每 12 度(360 度圆)显示一种不同的颜色。这些颜色更容易用非 RGB 配色方案表示。

    查看HSL Color 示例,了解轻松执行此操作的方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-14
      • 1970-01-01
      • 2019-12-02
      • 1970-01-01
      • 2021-01-02
      • 2011-01-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多