Return to Snippet

Revision: 48631
at July 6, 2011 22:16 by shreyharia


Initial Code
package mychessgui;

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

public class Main {

    static final char BLACK_PAWN = '\u265F';
    static final char BLACK_ROOK = '\u265C';
    static final char BLACK_KNIGHT = '\u265E';
    static final char BLACK_BISHOP = '\u265D';
    static final char BLACK_QUEEN = '\u265B';
    static final char BLACK_KING = '\u265A';
    static final char WHITE_PAWN = '\u2659';
    static final char WHITE_ROOK = '\u2656';
    static final char WHITE_KNIGHT = '\u2658';
    static final char WHITE_BISHOP = '\u2657';
    static final char WHITE_QUEEN = '\u2655';
    static final char WHITE_KING = '\u2654';

    /**
     * @ShreyHaria args the command line arguments
     */
    public static void main(String[] args) {
        GUI gui = new GUI();
        ImageIcon WK = new ImageIcon("E:\\WHITE_KNIGHT.jpg");
        Tile[] board = new Tile[64];
        int count = 0;
        for (int rank = 1; rank <= 8; rank++, count++) {
            for (char file = 'a'; file <= 'h'; file++) {


                if ((file + rank) % 2 == 0) {
                    board[count] = new Tile("", Color.GRAY);
                } else {
                    board[count] = new Tile("", Color.WHITE);
                }


                gui.mainframe.add(board[count].lbl);
                if (rank == 2) {
                    board[count].lbl.setText("" + WHITE_PAWN);
                } else if (rank == 7) {
                    board[count].lbl.setText("" + BLACK_PAWN);
                } else if (rank == 1) {
                    switch (file) {
                        case 'a':
                        case 'h':
                            board[count].lbl.setText("" + WHITE_ROOK);
                            break;
                        case 'b':
                        case 'g':
                           //board[count].lbl.setIcon(WK);/**/
/*heree got it*///                     board[count].lbl.set
                            board[count].lbl.setText("" + WHITE_KNIGHT);
                            break;
                        case 'c':
                        case 'f':
                            board[count].lbl.setText("" + WHITE_BISHOP);
                            break;
                        case 'd':
                            board[count].lbl.setText("" + WHITE_QUEEN);
                            break;
                        case 'e':
                            board[count].lbl.setText("" + WHITE_KING);
                            break;
                            
                    }
                } else if (rank == 8) {
                    switch (file) {
                        case 'a':
                        case 'h':
                            board[count].lbl.setText("" + BLACK_ROOK);
                            break;
                        case 'b':
                        case 'g':
                            board[count].lbl.setText("" + BLACK_KNIGHT);
                            break;
                        case 'c':
                        case 'f':
                            board[count].lbl.setText("" + BLACK_BISHOP);
                            break;
                        case 'd':
                            board[count].lbl.setText("" + BLACK_QUEEN);
                            break;
                        case 'e':
                            board[count].lbl.setText("" + BLACK_KING);
                            break;
                    }
                }
            }
        }
        gui.mainframe.setVisible(true);
    }
}

class GUI {

    static JFrame mainframe = new JFrame();
    GridLayout grid = new GridLayout(8, 8);

    GUI() {
        mainframe.setSize(700, 700);
        mainframe.setLayout(grid);
        mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

class Tile {

    JLabel lbl = new JLabel();
    static Font unicode = new Font("Arial Unicode MS", Font.BOLD, 50);
    static LineBorder border = new LineBorder(Color.BLACK, 2);

    Tile(String lblname, Color tc) {
        lbl.setFont(unicode);
        lbl.setHorizontalAlignment(JLabel.CENTER);
        lbl.setText(lblname);
        lbl.setBackground(tc);
        lbl.setBorder(border);
        lbl.setOpaque(true);
    }
}

Initial URL

                                

Initial Description

                                

Initial Title
Chess Board GUI

Initial Tags
java

Initial Language
Java