tags:

views:

62

answers:

3

I want to make a Java program that will create some dynamic color grids. For say I want to make 10 grids, then the color grid will be widely spaced. and it is 30, then it will be closely spaced. Here is an image : http://i.imgur.com/D2yba.png

and every grid should be responsible for making an event. oh yes, there will be a range of color. Something like 10 t0 100.

I first tried with random function. We know rgb color range are 0 to 255. So I tried to make some random color, but one color can come again and then it will mess up my whole things. I want some unique color grids with no repetition. Actually I want something exactly like the image.

Can anyone help me on these!

A: 

You might begin by looking at JColorChooser, as discussed in How to Use Color Choosers. One easy way to produce a spectral series of colors uses the HSB model; choose hues in the range 0 to 315 with constant saturation and brightness. Your question isn't very specific about events, but here's an example of displaying information as the mouse moves over each color.

trashgod
A: 

An example of how to manipulate colors to make a lovely rainbow.

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);
  }
}
Peter
"Swing programs should override `paintComponent()` instead of overriding `paint()`." http://java.sun.com/products/jfc/tsc/articles/painting/index.html
trashgod
Thank you very much, let me try with your example..
Bazlur Rahman Rokon
A: 

Actually I want something exactly like the image.

That image does not show random colors. It shows the different "hues" of color. Since you have 30 colors it basically shows a different color every 12 degrees (of a 360 degree circle). These colors are more easily represented in a non RGB color scheme.

Check out the HSL Color example for methods to do this easily.

camickr