写一个代码,想主要实现以下功能
在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();
}
}
解决方案 »
- scjp的问题
- HTTP协议里面的get和POST方法
- JAVA初学者请教各位高手一个问题,非常感谢!100分
- 列表框,我提交的是什么
- Applet中图像异步加载的疑问
- 我在客户端选择一个文件,然后上传到服务器端,我现在用下面这个程序来做,为什么不行呢? 大家帮我看一下吧,小弟这里有礼了。
- struts 中怎样在action中获得ApplicationResourses.properties中国际化字符
- 谁能给一个java流分析文本内容的程序? 100分
- 想以指定的字符集输出TXT文件怎么做?
- 6. 怎么把从一个new data()得到的日期取得的字符串再转回data类型?(很急!!!!)
- myeclipse中 jsp连接mysql问题
- 请教各位高手:用Eclipse做图形界面能否实现对按钮、标签的拖动?
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();
}
}