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

Posted vs2015

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了棋盘覆盖问题简单动画演示JAVA相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

前端每日实战:157# 视频演示如何用纯 CSS 创作一个棋盘错觉动画(实际上每一行都是平行的)

棋盘覆盖问题——分治法——代码清晰易懂

分治(详解残缺棋盘 —— Java代码实现)

棋盘覆盖问题&循环赛日程表问题

用python代码编写象棋界面,棋盘覆盖问题

C++经典算法问题:棋盘覆盖问题(分治算法)!含源码示例