- import java.util.*;
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class TestChess {
- public static void main(String[] args) {
- MyChessBoard mc = new MyChessBoard(); //构建实例
- }
- }
- class MyChessBoard extends JFrame implements ActionListener {
- int dimen; //棋盘规模
- int x_pos; //特殊点横坐标
- int y_pos; //特殊点竖坐标
- Container p;
- public MyChessBoard() {
- super();
- x_pos = 0;
- y_pos = 0;
- dimen = 0;
- setTitle("棋盘覆盖");
- setBackground(Color.YELLOW);
- setBounds(300,150,510,480);
- centreOnScreen();
- //setResizable(false);
- p = getContentPane();
- JMenuBar jmb = new JMenuBar();
- JMenu jm = new JMenu("操作");
- JMenuItem jitem1 = new JMenuItem("开始");
- JMenuItem jitem2 = new JMenuItem("退出");
- jm.add(jitem1);
- jm.add(jitem2);
- jmb.add(jm);
- setJMenuBar(jmb);
- jitem1.addActionListener(this);
- jitem2.addActionListener(this);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setVisible(true);
- }
- public void centreOnScreen() { //使窗体显示在屏幕中央
- Dimension displaySize = getToolkit().getScreenSize();
- Dimension winSize = getSize();
- int x = (displaySize.width - winSize.width) / 2;
- int y = (displaySize.height - winSize.height) / 2;
- if(x < 0) {
- x = 0;
- }
- if(y < 0) {
- y = 0;
- }
- setLocation(x, y);
- }
- public void actionPerformed(ActionEvent e) {
- if(e.getActionCommand()=="退出") {
- System.exit(0);
- } else if(e.getActionCommand()=="开始") {
- //在此输入方阵大小
- JOptionPane jop = new JOptionPane();
- jop.setVisible(true);
- dimen = Integer.parseInt(JOptionPane.showInputDialog(null,"输入方阵的行数:"));
- x_pos = (int)(dimen*Math.random()); //随机生成特殊点位置
- y_pos = (int)(dimen*Math.random());
- p.setLayout(new GridLayout(dimen, dimen));
- System.out.println(x_pos+","+y_pos);
- ChessBoard cb = new ChessBoard(dimen);
- cb.coverMethod(0, 0, x_pos, y_pos, dimen);
- int[][] board = cb.getBoard();
- JButton btn;
- for(int i=0; i<board.length; i++) {
- for(int j=0; j<board[i].length; j++) {
- if(board[i][j] == 0) {
- btn = new JButton("@");
- btn.setFont(new Font("", Font.BOLD, 24));
- btn.setForeground(Color.red);
- btn.setBackground(new Color(0, 0, 0));
- } else {
- btn = new JButton();
- btn.setBackground(new Color((board[i][j]*134)%255, //生成0-255的值,并与board[i][j]建立关系
- (board[i][j]*145)%255,
- (board[i][j]*178)%255));
- }
- p.add(btn);
- }
- }
- p.setVisible(true);
- }
- }
- }
- class ChessBoard {
- int [][] board;
- int tile;
- ChessBoard(int k) {
- board = new int [k][k]; //k*k的数组,保存最终结果
- tile = 0;
- }
- void coverMethod(int tr,int tc,int dr,int dc,int size) {
- //棋盘覆盖
- if(size == 1) return;
- int t = ++tile;
- int s = size/2;
- if(dr<tr+s && dc<tc+s) {
- coverMethod(tr,tc,dr,dc,s);
- } else {
- board[tr+s-1][tc+s-1] = t;
- coverMethod(tr,tc,tr+s-1,tc+s-1,s);
- }
- if(dr<tr+s && dc>=tc+s) {
- coverMethod(tr,tc+s,dr,dc,s);
- } else {
- board[tr+s-1][tc+s] = t;
- coverMethod(tr,tc+s,tr+s-1,tc+s,s);
- }
- if(dr>=tr+s && dc<tc+s) {
- coverMethod(tr+s,tc,dr,dc,s);
- } else {
- board[tr+s][tc+s-1] = t;
- coverMethod(tr+s,tc,tr+s,tc+s-1,s);
- }
- if(dr>=tr+s && dc>=tc+s) {
- coverMethod(tr+s,tc+s,dr,dc,s);
- } else {
- board[tr+s][tc+s] = t;
- coverMethod(tr+s,tc+s,tr+s,tc+s,s);
- }
- }
- public int[][] getBoard() { //返回覆盖结果
- return board;
- }
- }
随机产生不同颜色进行覆盖