• 棋盘覆盖


    第一次在博客园写博客,用来分享我的学习之路。

    以前写的小东西不是丢了,就是没有认真对待。这次虽然也很简单,但却是自己认真完成的,一个动态的swing图形化界面的棋盘覆盖。可惜没使用监听和自定义界面,提高自由度。

    棋盘覆盖

      1 package 棋盘覆盖;
      2 
      3 
      4 import java.awt.Color;
      5 
      6 public class QiPan extends Dmain{ 
      7       
      8     /**
      9      * 
     10      */
     11     private static final long serialVersionUID = 1L;
     12 
     13     /** 
     14      * @param tr表示棋盘左上角行号 
     15      * @param tc表示棋盘左上角列号 
     16      * @param dr表示特殊棋盘的行号 
     17      * @param dc表示特殊棋盘的列号 
     18      * @param SIZE =2^k。棋盘的规格为2^k*2^k 
     19      *            
     20      * */
     21     static final int SIZE = 4; 
     22 
     23     static int title = 1; // title表示L型骨牌的编号 
     24 
     25     public static void ChessBoard(int tr, int tc, int dr, int dc, int size) { 
     26        for(int i=0;i<999999999;i++)
     27        {
     28            
     29        }
     30         
     31         
     32         if (size == 1) { 
     33             return; 
     34         } 
     35         int t = title++; // t表示L型骨牌的编号 
     36         int s = size / 2; // 分割棋盘 
     37   
     38         // 覆盖左上角棋盘 
     39         if (dr < tr + s && dc < tc + s) { 
     40             // 说明特殊方格在此小棋盘中 
     41             ChessBoard(tr, tc, dr, dc, s); 
     42         } else { 
     43             // 说明特殊方格不在此小棋盘中 
     44             // 用t号L型棋盘覆盖这个小棋盘的右下角 
     45         
     46             setColor(tr + s - 1,tc + s - 1,t);
     47            
     48             // 覆盖其余棋盘 
     49             ChessBoard(tr, tc, tr + s - 1, tc + s - 1, s); 
     50         } 
     51   
     52         // 覆盖右上角棋盘 
     53         if (dr < tr + s && dc >= tc + s) { 
     54             ChessBoard(tr, tc + s, dr, dc, s); 
     55         } else { 
     56             setColor(tr + s - 1,tc + s ,t);
     57             ChessBoard(tr, tc + s, tr + s - 1, tc + s, s); 
     58         } 
     59         // 覆盖左下角棋盘 
     60         if (dr >= tr + s && dc < tc + s) { 
     61             ChessBoard(tr + s, tc, dr, dc, s); 
     62         } else { 
     63             setColor(tr + s ,tc + s - 1,t);
     64        
     65             ChessBoard(tr + s, tc, tr + s, tc + s - 1, s); 
     66         } 
     67         // 覆盖右下角棋盘 
     68         if (dr >= tr + s && dc >= tc + s) { 
     69             ChessBoard(tr + s, tc + s, dr, dc, s); 
     70         } else { 
     71             setColor(tr + s ,tc + s ,t);
     72 
     73             ChessBoard(tr + s, tc + s, tr + s, tc + s, s); 
     74         } 
     75   
     76     } 
     77     
     78     /**
     79     * 设置标签颜色
     80     * @param i , j ,t
     81     */
     82     public static void setColor(int i, int j, int t){
     83     switch(t){
     84     case 1:
     85        text[i][j].setBackground(Color.yellow);
     86     break;
     87     case 2:
     88         text[i][j].setBackground(Color.red);
     89     break;
     90     case 3:
     91         text[i][j].setBackground(Color.CYAN);
     92     break;
     93     case 4:
     94         text[i][j].setBackground(Color.orange);
     95     break;
     96     default:
     97         text[i][j].setBackground(Color.BLACK);
     98    
     99 } 
    100  
    101     }}
     1 package 棋盘覆盖;
     2 
     3 
     4 import java.awt.Color;
     5 
     6 import java.awt.Font;
     7 import java.util.Random;
     8 import javax.swing.BorderFactory;
     9 import javax.swing.JFrame;
    10 import javax.swing.JLabel;
    11 import javax.swing.JPanel;
    12 import javax.swing.SwingConstants;
    13 
    14 
    15 public class Dmain extends JFrame{
    16 
    17 /**
    18      * 
    19      */
    20     private static final long serialVersionUID = 1L;
    21 private JPanel mainPane;
    22 
    23 
    24 protected static JLabel[][] text;
    25 
    26 //Font font = new Font("", Font.BOLD,14);//设置字体类型和大小
    27 
    28 
    29 
    30 
    31 public static void main(String[] args){
    32 {
    33     
    34 Dmain frame = new Dmain();
    35 frame.setVisible(true);
    36 
    37 int a=new Random().nextInt(4);//生成随机数用来生成特殊棋盘位置
    38 int b=new Random().nextInt(4);
    39 text[a][b].setBackground(Color.MAGENTA);
    40     QiPan.ChessBoard(0,0,a,b,4);
    41 }
    42 
    43 
    44 
    45 }
    46 
    47 
    48 
    49 
    50 /**
    51 * 构造方法
    52 */
    53 public Dmain(){
    54 super();
    55 
    56 setResizable(false);//禁止调整窗体大小
    57 getContentPane().setLayout(null);//设置空布局
    58 setBounds(500, 50, 500, 615);
    59 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    60 setTitle("棋盘覆盖");//设置窗体标题
    61 
    62 mainPane = new JPanel();//创建游戏主面板
    63 mainPane.setBounds(20, 70, 460, 500);//设置主面板位置尺寸
    64 this.getContentPane().add(mainPane);//this.getContentPane()初始化一个容器
    65 mainPane.setLayout(null);//设置空布局
    66 
    67 
    68 text = new JLabel[4][4];//创建文本框二维数组
    69 for(int i = 0; i < 4; i++){//遍历数组
    70 for(int j = 0; j < 4; j++){
    71 text[i][j] = new JLabel();//创建标签
    72 text[i][j].setHorizontalAlignment(SwingConstants.CENTER);//设置文本水平对齐,居中
    73 text[i][j].setText("");
    74 text[i][j].setBounds(120 * j, 120 * i, 100, 100);//设置方块的大小位置
    75 text[i][j].setOpaque(true);//透明度不透明   false 透明,透明将导致文本颜色内容无法显示。
    76 text[i][j].setBorder(BorderFactory.createMatteBorder(2, 2, 2, 2, Color.green));//设置方块边框颜色
    77 mainPane.add(text[i][j]);//将创建的文本框放在mainpane面板上
    78 
    79 }}}}
    View Code
  • 相关阅读:
    字符串-串的最大表示-后缀数组-1163. 按字典序排在最后的子串
    动态规划-买卖股票的最佳时机 V
    贪心-到达终点数字
    贪心-优先队列-模拟-任务调度器
    滑动窗口-区间统计
    快速排序-无序数组K小元素
    POJ 2299 Ultra-QuickSort【树状数组 ,逆序数】
    POJ 2976 Dropping tests【二分 最大化平均值】
    POJ 1064 Cable master 【二分答案】
    POJ 3190 Stall Reservations 【贪心 优先队列】
  • 原文地址:https://www.cnblogs.com/yuhanghzsd/p/4402598.html
Copyright © 2020-2023  润新知