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

        /* The basis for a Noughts and Crosses (TicTacToe) game
         * using a GridLayout of Buttons.
         * This class simply creates a grid of buttons
         * as a View of a TicTacToe game.
         * All of the underlying game functionality is provided
         * by an TTTGame object.
         */
class TTTDisplay extends JPanel {
    public TTTDisplay(int boardSize){
        final int width = 200, height = 200;
        setPreferredSize(new Dimension(width,height));

        final int rows = boardSize, cols = boardSize;
        // Use a Grid Layout
        setLayout(new GridLayout(rows,cols));

        // Create the font to use with the buttons.
        final Font font = new Font("Serif",Font.BOLD,20);

        // Add each button to the frame and the board.
        board = new JButton[rows][cols];
        for(int r = 0; r < rows; r++){
            for(int c = 0; c < cols; c++){
                JButton square = new JButton(" ");
                square.setBackground(Color.white);
                square.setFont(font);
                board[r][c] = square;
                add(square);
            }
        }

        ComponentFrame frame = new ComponentFrame("Tic Tac Toe");
        frame.addComponent(this);
        frame.pack();
    }

        // Select the given squares and set the appropriate symbol.
        // Return true if the square was free.
    public void showSquare(int row,int col,char symbol){
        // Pause before showing the move.
        delay();
        final JButton[][] board = getBoard();
        board[row][col].setText(symbol+"");
        repaint();
    }

    public void clear(){
        // Pause before clearing.
        delay();
        final JButton[][] board = getBoard();
        final int rows = board.length, cols = board[0].length;
        for(int r = 0; r < rows; r++){
            for(int c = 0; c < cols; c++){
                board[r][c].setText(" ");
            }
        }
        repaint();
    }

    protected void delay(){
        try{
            final int delayTime = 3000;
            Thread.sleep(delayTime);
        }
        catch(Exception e){
        }
    }

    protected JButton[][] getBoard(){
        return board;
    }

    // The board of buttons.
    private final JButton[][] board;
}
