import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.JOptionPane;
class MyCanvas extends Canvas {
int size, flag, count;
Color c[];
MyCanvas() {
setBackground(Color.lightGray);
flag = 0;
c = new Color[512];
}
public void getBoard(int s) {
size = s;
flag = 1;
}
public void flushColor() {
count = 0;
Random rnd = new Random();
for(int i = 0; i < c.length; i++) {
Color co = new Color(rnd.nextInt(255), rnd.nextInt(255), rnd.nextInt(255));
c[count++] = co;
}
}
public void paint(Graphics g) {
if(flag == 0) {
Window.board = new int[32][32];
size = 16;
g.setColor(Color.black);
for(int i = 0; i < size; i++) {
for(int j = 0; j < size; j++) {
g.drawRect(30 + i * 34, 20 + j * 34, 34, 34);
}
}
}
else {
int td = 550 / size;
for(int i = 0; i < size; i++) {
for(int j = 0; j < size; j++) {
int temp = Window.board[i][j];
if(temp == 0) {
g.setColor(Color.black);
}
else {
g.setColor(c[temp]);
}
g.fillRect(30 + i * td, 20 + j * td, td, td);
}
}
}
}
}
class Window extends Frame implements ActionListener {
public static int board[][];
int t;
MyCanvas canvas;
TextField inputSize, inputDR, inputDC;
Button b1, b2, b3;
Window() {
super("棋盘填充");
canvas = new MyCanvas();
inputSize = new TextField("16");
inputDR = new TextField("1");
inputDC = new TextField("1");
Panel pSouth = new Panel();
pSouth.add(new Label("棋盘大小:"));
pSouth.add(inputSize);
pSouth.add(new Label(" 特殊格所在行:"));
pSouth.add(inputDR);
pSouth.add(new Label(" 特殊格所在列:"));
pSouth.add(inputDC);
b1 = new Button("刷新棋盘");
b1.addActionListener(this);
b2 = new Button("关 于");
b2.addActionListener(this);
b3 = new Button("退 出");
b3.addActionListener(this);
pSouth.add(b1);
pSouth.add(b2);
pSouth.add(b3);
add(canvas, BorderLayout.CENTER);
add(pSouth, BorderLayout.SOUTH);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setBounds(100, 10, 620, 650);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == b3)System.exit(0);
if(e.getSource() == b2) {
String message = "作者:曹 柯\r\nQQ:113939969\r\nEmail:ck_2036@yahoo.com.cn";
JOptionPane.showMessageDialog(this, message, "关 于", JOptionPane.INFORMATION_MESSAGE);
return;
}
canvas.flushColor();
int dr, dc, size;
try {
size = Integer.parseInt(inputSize.getText());
dr = Integer.parseInt(inputDR.getText()) - 1;
dc = Integer.parseInt(inputDC.getText()) - 1;
if(size != 2 && size != 4 && size != 8 && size != 16 && size != 32 || dr >= size || dc >= size || dr < 0 || dc < 0)
throw (new Exception());
t = 1;
fillBoard(0, 0, dr, dc, size);
board[dr][dc] = 0;
canvas.getBoard(size);
canvas.repaint();
}
catch(Exception ee) {
String message = "输入参数有误!\r\n棋盘大小只能取2、4、8、16、32,特殊格所在行列不能大于棋盘大小";
JOptionPane.showMessageDialog(this, message, "警告对话框", JOptionPane.WARNING_MESSAGE);
inputSize.setText("16");
inputDC.setText("1");
inputDR.setText("1");
}
}
public void fillBoard(int tr,int tc,int dr,int dc,int ss){
if(ss == 1) return;
int s = ss / 2,
f = t++;
//左上
if(dr < tr + s && dc < tc + s)
fillBoard(tr, tc, dr, dc, s);
else {
board[tr + s - 1][tc + s - 1] = f;
fillBoard(tr, tc, tr + s - 1, tc + s - 1, s);
}
//右上
if(dr < tr + s && dc >= tc + s)
fillBoard(tr, tc + s, dr, dc, s);
else {
board[tr + s - 1][tc + s] = f;
fillBoard(tr, tc + s, tr + s - 1,tc + s, s);
}
//左下
if(dr >= tr + s && dc < tc + s)
fillBoard(tr + s, tc, dr, dc, s);
else {
board[tr + s][tc + s - 1] = f;
fillBoard(tr + s, tc, tr + s, tc + s - 1, s);
}
//右下
if(dr >= tr + s && dc >= tc + s)
fillBoard(tr + s, tc + s, dr, dc, s);
else {
board[tr + s][tc + s] = f;
fillBoard(tr + s, tc + s, tr + s, tc + s, s);
}
}
}
public class ChessBoard_gui {
public static void main(String[] args) {
// TODO 自动生成方法存根
new Window();
}
}