写一个代码,想主要实现以下功能
在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();
}
}
在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();
}
}
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();
}
}