码迷,mamicode.com
首页 > 编程语言 > 详细

棋盘覆盖问题简单动画演示JAVA

时间:2018-10-11 21:46:36      阅读:313      评论:0      收藏:0      [点我收藏+]

标签:length   图片   bool   pac   you   png   raw   演示   etc   

import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;

public class ChessTableModel implements TableModel {
    private String  data[][];

    ChessTableModel(String [][]data){
        this.data=data;
    }
    @Override
    public int getRowCount() {
        return data.length;
    }

    @Override
    public int getColumnCount() {
        return data[0].length;
    }

    @Override
    public String getColumnName(int columnIndex) {
        return String.valueOf(columnIndex);
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return String.class;
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        return data[rowIndex][columnIndex];
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        data[rowIndex][columnIndex]=(String) aValue;
    }

    @Override
    public void addTableModelListener(TableModelListener l) {

    }

    @Override
    public void removeTableModelListener(TableModelListener l) {

    }
}
public class ChessBroad {

    private static int SIZE = 8;
    private static String arr[][] = new String[SIZE][SIZE];
    private static Table table;
    static {
        for (int i = 0; i < SIZE; i++) {
            for (int j = 0; j < SIZE; j++) {
                arr[i][j] = "0";
            }
        }
        table=new Table(arr);
    }

     private static void draw() {
        try {
            Thread.sleep(100);
            table.jTable.validate();
            table.jTable.updateUI();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        for (int i = 0; i < SIZE; i++) {
            for (int j = 0; j < SIZE; j++) {
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();
        }
        System.out.println("--------------");






    }


    /**
     * @param b_x  the abscissa at the top left
     * @param b_y  the  top left ordinate
     * @param p_x  the abscissa of the particular location
     * @param p_y  the ordinate of the particular location
     * @param size the SIZE of the broad
     */
 private static void broad(int b_x, int b_y, int p_x, int p_y, int size) {


      draw();
        if (size == 1) return;

        int temp_size = size / 2;
        if (p_x < b_x + temp_size && p_y < b_y + temp_size) {//特殊点在左上角
            broad(b_x, b_y, p_x, p_y, size / 2);

            B(b_x, b_y, temp_size, "a");
            C(b_x, b_y, temp_size, "a");
            D(b_x, b_y, temp_size, "a");
        } else if (p_x >= b_x + temp_size && p_y < b_y + temp_size) {//特殊点在左下角
            broad(b_x + temp_size, b_y, p_x, p_y, size / 2);
            A(b_x, b_y, temp_size, "b");
            C(b_x, b_y, temp_size, "b");
            D(b_x, b_y, temp_size, "b");

        } else if (p_x < b_x + temp_size && p_y >= b_y + temp_size) {//特殊点在右上角
            broad(b_x, b_y + temp_size, p_x, p_y, size / 2);
            A(b_x, b_y, temp_size, "c");
            B(b_x, b_y, temp_size, "c");
            D(b_x, b_y, temp_size, "c");
        } else {//特殊点在右下角
            broad(b_x + temp_size, b_y + temp_size, p_x, p_y, size / 2);
            A(b_x, b_y, temp_size, "d");
            B(b_x, b_y, temp_size, "d");
            C(b_x, b_y, temp_size, "d");
        }


    }

 private static void A(int b_x, int b_y, int size, String symbol) {//左上角

        arr[b_x + size - 1][b_y + size - 1] = symbol;//左上角
        broad(b_x, b_y, b_x + size - 1, b_y + size - 1, size);//左上角

    }


    private  static void B(int b_x, int b_y, int size, String symbol) {//左下角

        arr[b_x + size][b_y + size - 1] = symbol;//左下角
        broad(b_x + size, b_y, b_x + size, b_y + size - 1, size);//左下角

    }

   private static void C(int b_x, int b_y, int size, String symbol) {//右上角

        arr[b_x + size - 1][b_y + size] = symbol;//右上角
        broad(b_x, b_y + size, b_x + size - 1, b_y + size, size);//右上角

    }

    private  static void D(int b_x, int b_y, int size, String symbol) {//右下角
        arr[b_x + size][b_y + size] = symbol;//右下角
        broad(b_x + size, b_y + size, b_x + size, b_y + size, size);//右下角
    }

    public static void main(String[] args) {

        broad(0, 0, 2, 3, SIZE);
        draw();


        Table table = new Table(arr);

        table.setVisible(true);
    }
}
package chess;

import javax.swing.*;
import java.awt.*;

 class Table extends JFrame {
   JTable jTable;
    Table(String[][] arr) {
        super();
        setTitle("棋盘覆盖演示");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jTable = new JTable();
        ChessTableModel chessTableModel=new ChessTableModel(arr);
        jTable.setModel(chessTableModel);
        getContentPane().add(jTable, BorderLayout.CENTER);
        this.setVisible(true);
        this.pack();
    }

}

效果:技术分享图片

技术分享图片

 

棋盘覆盖问题简单动画演示JAVA

标签:length   图片   bool   pac   you   png   raw   演示   etc   

原文地址:https://www.cnblogs.com/zezeze/p/9774788.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!