你想怎样setBackground呢? (我的意思是,要做个方法setBackground(int row, int column, Color wantedColor), 来对某方块设颜色吗?)有g2.fill(new RectAngle(...))如果能用Swing, 用JLabel来排列也可以啊.没达到你的要求, 不要分了
借用一下LS的代码final static int MAX_ROW = 30; final static int MAX_COL = 30; final static int CELL_DIV = 10; private Color[][] color = new Color[MAX_ROW][MAX_COL]; //定义一个颜色数组成员 { for (int i=0; i<MAX_ROW; i++) for (int j=0; j<MAX_CELL; j++) //color[i][j] = your_panel.getBackground(); //缺省颜色是你的Panel的背景色 color[i][j] = new Color((int)(Math.random()*256), (int)(Math.random()*256), (int)(Math.random()*256)); //测试颜色 }以上代码可以放到构造函数或者静态代码块//设置格子的颜色 public void setCellColor(int row, int col, Color c) { if (row<0 || row>MAX_ROW-1) return; if (col<0 || col>MAX_COL-1) return; color[row][col] = c; }public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; int penW = 1; g2.setStroke(new BasicStroke((float)penW)); //笔宽 g2.setColor(Color.black); //黑色画边框 int orgX = 10; int orgY = 10; int lenX = MAX_ROW * CELL_DIV; int lenY = MAX_COL * CELL_DIV;
//水平线31条 for(int i = 0; i <MAX_ROW+1; i++) { g2.draw(new Line2D.Double(orgX, orgY+i*CELL_DIV, orgX + lenX, orgY+i*CELL_DIV)); } //垂直线31条 for(int i = 0; i <MAX_COL+1; i++) { g2.draw(new Line2D.Double(orgX+i*CELL_DIV, orgY, orgX+i*CELL_DIV , orgY+ lenY)); } // 填充格子颜色 for (int i=0; i<MAX_ROW; i++) { for (int j=0; j<MAX_COL; j++) { g2.setColor(color[i][j]); //设置格子颜色 g2.fillRect(orgX+j*CELL_DIV+1, orgY+i*CELL_DIV, CELL_DIV-penW*2, CELL_DIV-penW*2); } }
import java.awt.*;public class ColorfulGrid extends Frame {
private int size = 30; private int unit = 10; private int gap = 1; private Panel[] colorSpots = new Panel[size*size]; private Color[] builtinColors = { Color.RED, Color.GREEN, Color.BLUE, Color.CYAN, Color.MAGENTA, Color.ORANGE, Color.WHITE, Color.BLACK, Color.GRAY, Color.LIGHT_GRAY }; private java.util.Random random = new java.util.Random();
public ColorfulGrid() { setSize(size*(unit+gap), size*(unit+gap)); setResizable(false); setLayout(new GridLayout(size, size, gap, gap)); for (int i = 0; i < colorSpots.length; i++) { colorSpots[i] = new Panel(); add(colorSpots[i]); colorSpots[i].setBackground(builtinColors[random.nextInt(builtinColors.length)]); } }
/* * 为指定位置的色块设置颜色 */ public void setColorFor(int row, int col, Color c) { if (row < 0 || row >= size || col < 0 || col >= size) return; colorSpots[row*size+col].setBackground(c); } public static void main(String[] args) { new ColorfulGrid().setVisible(true); }}
public void paint(Graphics g) { super.paint(g);
Graphics2D g2 = (Graphics2D) g; g2.setStroke(new BasicStroke(1.0f)); //笔宽 GradientPaint redtowhite = new GradientPaint(x,y,Color.RED,x+rectWidth-5, y,Color.white);
g2.setPaint(redtowhite); //画渐变的 int orgX = 10;
int orgY = 10;
//水平线31条
for(int i = 0; i <31; i++)
{
g2.draw(new Line2D.Double(orgX, orgY+i*10, orgX + 30*10, orgY+i*10));
}
//垂直线31条
for(int i = 0; i <31; i++)
{
g2.draw(new Line2D.Double(orgX+i*10, orgY, orgX+i*10 , orgY+ 30*10));
}
肯定给分的~!!
final static int MAX_COL = 30;
final static int CELL_DIV = 10;
private Color[][] color = new Color[MAX_ROW][MAX_COL]; //定义一个颜色数组成员
{ for (int i=0; i<MAX_ROW; i++)
for (int j=0; j<MAX_CELL; j++)
//color[i][j] = your_panel.getBackground(); //缺省颜色是你的Panel的背景色
color[i][j] = new Color((int)(Math.random()*256), (int)(Math.random()*256), (int)(Math.random()*256)); //测试颜色
}以上代码可以放到构造函数或者静态代码块//设置格子的颜色
public void setCellColor(int row, int col, Color c) {
if (row<0 || row>MAX_ROW-1) return;
if (col<0 || col>MAX_COL-1) return;
color[row][col] = c;
}public void paintComponent(Graphics g) { super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g; int penW = 1;
g2.setStroke(new BasicStroke((float)penW)); //笔宽 g2.setColor(Color.black); //黑色画边框 int orgX = 10;
int orgY = 10;
int lenX = MAX_ROW * CELL_DIV;
int lenY = MAX_COL * CELL_DIV;
//水平线31条
for(int i = 0; i <MAX_ROW+1; i++)
{
g2.draw(new Line2D.Double(orgX, orgY+i*CELL_DIV, orgX + lenX, orgY+i*CELL_DIV));
} //垂直线31条
for(int i = 0; i <MAX_COL+1; i++)
{
g2.draw(new Line2D.Double(orgX+i*CELL_DIV, orgY, orgX+i*CELL_DIV , orgY+ lenY));
}
// 填充格子颜色
for (int i=0; i<MAX_ROW; i++) {
for (int j=0; j<MAX_COL; j++) {
g2.setColor(color[i][j]); //设置格子颜色
g2.fillRect(orgX+j*CELL_DIV+1, orgY+i*CELL_DIV, CELL_DIV-penW*2, CELL_DIV-penW*2);
}
}
private int size = 30;
private int unit = 10;
private int gap = 1;
private Panel[] colorSpots = new Panel[size*size];
private Color[] builtinColors = {
Color.RED, Color.GREEN, Color.BLUE,
Color.CYAN, Color.MAGENTA, Color.ORANGE,
Color.WHITE, Color.BLACK, Color.GRAY, Color.LIGHT_GRAY };
private java.util.Random random = new java.util.Random();
public ColorfulGrid() {
setSize(size*(unit+gap), size*(unit+gap));
setResizable(false);
setLayout(new GridLayout(size, size, gap, gap));
for (int i = 0; i < colorSpots.length; i++) {
colorSpots[i] = new Panel();
add(colorSpots[i]);
colorSpots[i].setBackground(builtinColors[random.nextInt(builtinColors.length)]);
}
}
/*
* 为指定位置的色块设置颜色
*/
public void setColorFor(int row, int col, Color c) {
if (row < 0 || row >= size || col < 0 || col >= size) return;
colorSpots[row*size+col].setBackground(c);
} public static void main(String[] args) {
new ColorfulGrid().setVisible(true);
}}