写一个代码,想主要实现以下功能
在10*10的格子里,默认颜色为红色,用for循环来使100个格子其中的30个格子成黑色,我用了随机函数来做,但是最后一个应该怎么写,就是最下面的代码要怎么样把黑色作为二维数组的背景色(或前景色?)。谢谢
import javax.swing.*;
import java.awt.*;
import java.util.*;class MyWin extends JFrame {
    JPanel p = new JPanel();

public void paint (Graphics g) {
int num = 10; 
int[][] grille = new int [num][num];
int top = 50,left = 30;
int width = 50,height = 50; 
int i , j;

for( i = 0; i < num; i++){
  for( j = 0;j < num; j++){
    g.setColor(Color.RED);
    g.fillRect(i*(width+1)+left, j*(height+1)+top, width, height);
  }
}

            //here I want to add the color black for 30 cases with random
for(int x = 0; x <= 30; x++){
  i=(int)(Math.random()%10);
  j=(int)(Math.random()%10);
  ????????????????????????
}
}
public MyWin(){
     setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setBounds(0,0,1024,768);
        setVisible(true);
        Container con = getContentPane();
        con.setLayout(new BorderLayout());
        setBackground(Color.YELLOW);  
        con.validate();  
        setResizable(true);
    
  }


}public class Grille1{
 public static void main(String args[]){
 MyWin tc = new MyWin();
     }
}

解决方案 »

  1.   

    将你的程序改了一下,可以运行,画格子的地方正常。你再按需要改改就行。import javax.swing.*;
    import java.awt.*;public class MyWin extends JFrame {
        JPanel p = new JPanel();
        @Override
        public void paint(Graphics g) {
            int num = 10;
            int[][] grille = new int[num][num];
            int top = 50, left = 30;
            int width = 50, height = 50;
            int i, j;
            for (i = 0; i < num; i++) {
                for (j = 0; j < num; j++) {
                    g.setColor(Color.RED);
                    g.fillRect(i * (width + 1) + left, j * (height + 1) + top, width, height);
                }
            }        //here I want to add the color black for 30 cases with random
            g.setColor(Color.BLACK);
            for (int x = 0; x < 30; x++) {
                i = (int) (Math.random() * 10);
                j = (int) (Math.random() * 10);
                if(grille[i][j]==1){
                    x--;
                    continue;
                }
                else{
                    grille[i][j]=1;
                    g.fillRect(i * (width + 1) + left, j * (height + 1) + top, width, height);
                }
            }
        }    public MyWin() {
            setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            setBounds(0, 0, 1024, 768);
            setVisible(true);
            Container con = getContentPane();
            con.setLayout(new BorderLayout());
            setBackground(Color.YELLOW);
            con.validate();
            setResizable(true);
        }
        public static void main(String args[]) {
            MyWin tc = new MyWin();
        }
    }
      

  2.   

    这里我定义了一个空的数组,那么在这里是不是grille[i][j]!=1,也就是说先实现else里的部分,然后当x=1的时候再计算if语句,呵呵,谢谢1楼的朋友,不过还是不太明白random是怎么实现的,呵呵。
      

  3.   

    整型数组定义后元素的值都为0,random()返回一个[0.0,1.0)的随机数,你可以查api。