【问题标题】:How to fill color in grid boxes randomly如何在网格框中随机填充颜色
【发布时间】:2010-03-03 19:27:21
【问题描述】:

如何在网格框上随机填充颜色?

这里不是如图所示有序:

Grid http://www.freeimagehosting.net/uploads/4ed76557de.jpg

public class grid extends JPanel{
    Label one = new Label();
    Label two = new Label();
    Label three = new Label();
    Label four = new Label();

    public static void main(String[] args){
        JFrame jf=new JFrame();
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.add(new YAnswers());
        jf.pack();
        jf.setVisible(true);
    }

    grid (){
       int rows=10; int cols=10;
       setLayout(new GridLayout(rows,cols));
       add(one); one.setBackground(Color.red);
       add(two); two.setBackground(Color.orange);
       add(three); three.setBackground(Color.green);
       add(four); four.setBackground(Color.black);
       boxes[] bx=new boxes[rows*cols];

        for(int i=0;i<rows*cols;i++){
            System.out.println("i"+i);
            bx[i]=new boxes();
            if(i%2<1)
                bx[i].setColor(1);
            add(bx[i]);
        }
    } //end grid()
}

【问题讨论】:

  • @Jessy:我已经编辑了您的问题以直接显示图像。您想要随机颜色,还是想要随机字段中的预定义颜色?
  • 其实我的意思是随机的,就是填满颜色的盒子的位置,很抱歉造成混乱。
  • 我之前编辑了您的问题以在问题中显示图像。为什么您再次删除了图片并提供了链接?
  • 非常抱歉,我只是在图片上添加了标签,我的意思是有序和随机。
  • 请检查我的更新答案是否能解决您的问题。

标签: java grid image


【解决方案1】:

您可以使用Math.random获得随机颜色:

new Color( (float)Math.random(), (float)Math.random(), (float)Math.random() );

顺便说一句:Java 中的类名以大写字母开头,因此请使用Grid 而不是grid


编辑

以下代码使用GridBagLayout 产生此结果:

alt text http://img214.imageshack.us/img214/5426/so2374295.png

public Grid ()
{
    final Color BACKGROUND = Color.LIGHT_GRAY;
    final Color[] colors = new Color[]
        {Color.BLACK, Color.BLACK, Color.BLUE, Color.BLUE};

    final int ROWS=10;
    final int COLS=10;

    setBackground(Color.BLACK);
    setLayout(new GridBagLayout());

    Label[][] label = new Label[ROWS][COLS];

    GridBagConstraints gc = new GridBagConstraints();
    gc.weightx = 1d;
    gc.weighty = 1d;
    gc.insets = new Insets(0, 0, 1, 1);
    gc.fill = GridBagConstraints.BOTH;

    // fill the whole panel with labels
    for( int r=0 ; r<ROWS ; r++) {
        for( int c=0 ; c<COLS ; c++) {
            Label l = new Label();
            l.setBackground(BACKGROUND);
            gc.gridx = r;
            gc.gridy = c;
            add(l, gc);
            label[r][c] = l;
        }
    }

    // now find random fields for the colors defined in BACKGROUND
    for(Color col : colors) {
        int r, c;
        do { // make sure to find unique fields
            r = (int)Math.floor(Math.random() * ROWS);
            c = (int)Math.floor(Math.random() * COLS);
        } while(!label[r][c].getBackground().equals(BACKGROUND));
        label[r][c].setBackground(col);
    }
}

【讨论】:

    【解决方案2】:

    您可以将Colors 添加到List&lt;Color&gt; clut = new ArrayList&lt;Color&gt;() 并使用shuffle() 方法之一。另一个技巧是使用Queue&lt;Color&gt; clut = new LinkedList&lt;Color&gt;() 并根据需要使用clut.add(clut.remove()) 在它们之间循环。

    【讨论】:

    • 我猜几百个随机数生成并不重要,对于合理的 ROWS 和 COLS 选择,@Peter Lang 的解决方案永远运行的概率很低。无论如何,我更喜欢这种保证完成的方法。随机播放然后迭代!
    • 感谢您的评论。仔细观察,我发现改组 List&lt;Point&gt; 会排除这种情况下的重复。
    【解决方案3】:

    创建一个包含所需颜色的 Color[] 数组,然后使用 java.util.Random 选择从 0 到数组长度 - 1 的索引。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-20
      • 2020-07-01
      • 2014-05-21
      • 2013-06-27
      • 2020-01-11
      • 2011-07-09
      • 1970-01-01
      • 2022-08-18
      相关资源
      最近更新 更多