1. 程式人生 > >棋盤覆蓋問題簡單動畫演示JAVA

棋盤覆蓋問題簡單動畫演示JAVA

opera ane temp span tcl edi ddt mov name

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