如何随机填充网格框中的颜色 [英] How to fill color in grid boxes randomly
问题描述
而不是按照图中所示顺序排列:
网格http://www.freeimagehosting.net/uploads/4ed76557de.jpg
public class grid extends JPanel {
Label one = new Label();
Label two = new Label();
Label three = 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.setBackground(Color.black);
boxes [] bx =新盒子[rows * cols];
for(int i = 0; i System.out.println(i+ i);
bx [i] = new boxes();
if(i%2< 1)
bx [i] .setColor(1);
add(bx [i]);
}
} // end grid()
}
您可以使用 Math.random
获取随机颜色:
<$ p (float)Math.random(),(float)Math.random(),(float)Math.random()); $ p>
BTW:类名以大写字母开头,所以使用 Grid
而不是 grid
。
编辑
以下代码使用 GridBagLayout
以产生此结果:
替代文字http://img214.imageshack.us/img214/ 5426 / so2374295.png
public Grid()
{
final颜色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());
标签[] []标签=新标签[ROWS] [COLS];
GridBagConstraints gc = new GridBagConstraints();
gc.weightx = 1d;
gc.weighty = 1d;
gc.insets = new Insets(0,0,1,1);
gc.fill = GridBagConstraints.BOTH;
//为整个面板填充标签
for(int r = 0; r for(int c = 0; c< COLS; c ++ ){
标签l =新标签();
l.setBackground(BACKGROUND);
gc.gridx = r;
gc.gridy = c;
add(l,gc);
label [r] [c] = l;
//现在在背景
中为(Color col:colors){
int r,c ;
do {//确保找到唯一字段
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);
}
}
How can I fill color on the grid boxes randomly?
Rather than orderly as shown in picture here:
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()
}
You can get a random color by using Math.random
:
new Color( (float)Math.random(), (float)Math.random(), (float)Math.random() );
BTW: Class-names start with upper-case in Java, so use Grid
instead of grid
.
EDIT
The following code uses GridBagLayout
to produce this result:
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);
}
}
这篇关于如何随机填充网格框中的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!