棋盘覆盖 java实现

Code:
  1. import java.util.*;   
  2. import java.awt.*;   
  3. import java.awt.event.*;   
  4. import javax.swing.*;   
  5.   
  6. public class TestChess {   
  7.     public static void main(String[] args) {   
  8.         MyChessBoard mc = new MyChessBoard();  //构建实例   
  9.     }   
  10. }   
  11.   
  12. class MyChessBoard extends JFrame implements ActionListener {   
  13.     int dimen;    //棋盘规模   
  14.     int x_pos;    //特殊点横坐标   
  15.     int y_pos;    //特殊点竖坐标   
  16.     Container p;   
  17.        
  18.     public MyChessBoard() {   
  19.         super();   
  20.         x_pos = 0;   
  21.         y_pos = 0;   
  22.         dimen = 0;   
  23.         setTitle("棋盘覆盖");   
  24.         setBackground(Color.YELLOW);   
  25.         setBounds(300,150,510,480);   
  26.         centreOnScreen();   
  27.         //setResizable(false);   
  28.         p = getContentPane();   
  29.            
  30.         JMenuBar jmb = new JMenuBar();   
  31.         JMenu jm = new JMenu("操作");   
  32.         JMenuItem jitem1 = new JMenuItem("开始");   
  33.         JMenuItem jitem2 = new JMenuItem("退出");   
  34.         jm.add(jitem1);   
  35.         jm.add(jitem2);   
  36.         jmb.add(jm);   
  37.         setJMenuBar(jmb);   
  38.            
  39.         jitem1.addActionListener(this);   
  40.         jitem2.addActionListener(this);   
  41.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
  42.            
  43.         setVisible(true);   
  44.     }   
  45.        
  46.     public void centreOnScreen() {    //使窗体显示在屏幕中央   
  47.         Dimension displaySize = getToolkit().getScreenSize();   
  48.         Dimension winSize = getSize();   
  49.         int x = (displaySize.width - winSize.width) / 2;   
  50.         int y = (displaySize.height - winSize.height) / 2;   
  51.         if(x < 0) {   
  52.             x = 0;   
  53.         }   
  54.         if(y < 0) {   
  55.             y = 0;   
  56.         }   
  57.         setLocation(x, y);   
  58.     }   
  59.        
  60.     public void actionPerformed(ActionEvent e) {   
  61.         if(e.getActionCommand()=="退出") {   
  62.             System.exit(0);   
  63.         } else if(e.getActionCommand()=="开始") {   
  64.             //在此输入方阵大小   
  65.           JOptionPane jop = new JOptionPane();   
  66.           jop.setVisible(true);   
  67.           dimen = Integer.parseInt(JOptionPane.showInputDialog(null,"输入方阵的行数:"));   
  68.                
  69.           x_pos = (int)(dimen*Math.random());  //随机生成特殊点位置   
  70.           y_pos = (int)(dimen*Math.random());   
  71.              
  72.           p.setLayout(new GridLayout(dimen, dimen));   
  73.             System.out.println(x_pos+","+y_pos);   
  74.             ChessBoard cb = new ChessBoard(dimen);   
  75.             cb.coverMethod(00, x_pos, y_pos, dimen);   
  76.             int[][] board = cb.getBoard();   
  77.             JButton btn;   
  78.             for(int i=0; i<board.length; i++) {   
  79.                 for(int j=0; j<board[i].length; j++) {   
  80.                     if(board[i][j] == 0) {   
  81.                         btn = new JButton("@");   
  82.                         btn.setFont(new Font("", Font.BOLD, 24));   
  83.                         btn.setForeground(Color.red);   
  84.                         btn.setBackground(new Color(000));   
  85.                     } else {   
  86.                         btn = new JButton();   
  87.                         btn.setBackground(new Color((board[i][j]*134)%255//生成0-255的值,并与board[i][j]建立关系   
  88.                                                     (board[i][j]*145)%255,    
  89.                                                     (board[i][j]*178)%255));   
  90.                     }   
  91.                     p.add(btn);   
  92.                 }   
  93.             }   
  94.             p.setVisible(true);   
  95.           }   
  96.     }   
  97. }   
  98.   
  99. class ChessBoard {   
  100.     int [][] board;   
  101.     int tile;   
  102.     ChessBoard(int k) {   
  103.         board = new int [k][k];  //k*k的数组,保存最终结果   
  104.         tile = 0;   
  105.     }   
  106.     void coverMethod(int tr,int tc,int dr,int dc,int size) {   
  107.                                                         //棋盘覆盖   
  108.         if(size == 1return;   
  109.         int t = ++tile;   
  110.         int s = size/2;   
  111.            
  112.         if(dr<tr+s && dc<tc+s) {   
  113.             coverMethod(tr,tc,dr,dc,s);   
  114.         }   else {   
  115.             board[tr+s-1][tc+s-1] = t;   
  116.             coverMethod(tr,tc,tr+s-1,tc+s-1,s);   
  117.         }   
  118.            
  119.         if(dr<tr+s && dc>=tc+s) {   
  120.             coverMethod(tr,tc+s,dr,dc,s);   
  121.         }   else {   
  122.             board[tr+s-1][tc+s] = t;   
  123.             coverMethod(tr,tc+s,tr+s-1,tc+s,s);   
  124.         }   
  125.            
  126.         if(dr>=tr+s && dc<tc+s) {   
  127.             coverMethod(tr+s,tc,dr,dc,s);   
  128.         }   else {   
  129.             board[tr+s][tc+s-1] = t;   
  130.             coverMethod(tr+s,tc,tr+s,tc+s-1,s);   
  131.         }    
  132.            
  133.         if(dr>=tr+s && dc>=tc+s) {   
  134.             coverMethod(tr+s,tc+s,dr,dc,s);   
  135.         }   else {   
  136.             board[tr+s][tc+s] = t;   
  137.             coverMethod(tr+s,tc+s,tr+s,tc+s,s);   
  138.         }   
  139.     }   
  140.        
  141.     public int[][] getBoard() {   //返回覆盖结果   
  142.         return board;   
  143.     }   
  144. }  

随机产生不同颜色进行覆盖

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值