package chess;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class ChessPanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
private JButton[][] board;
private ChessModel model;
private JButton quitButton;
private IChessPiece p;
private ButtonListener buttonListener = new ButtonListener();
private Player player;
private ImageIcon emptyIcon;
private JPanel middlePanel;
private ImageIcon bpawn;
private ImageIcon wpawn;
private ImageIcon bking;
private ImageIcon wking;
private ImageIcon bqueen;
private ImageIcon wqueen;
private ImageIcon bknight;
private ImageIcon wknight;
private ImageIcon brook;
private ImageIcon wrook;
private ImageIcon bbishop;
private ImageIcon wbishop;
public ChessPanel(Player player) {
model = new ChessModel(player);
player = Player.BLACK;
setLayout(new BorderLayout());
emptyIcon = new ImageIcon("empty.jpg");
bpawn = new ImageIcon("blackpawn.jpg");
wpawn = new ImageIcon("whitepawn.jpg");
bking = new ImageIcon("blackking.jpg");
wking = new ImageIcon("whiteking.jpg");
bqueen = new ImageIcon("blackqueen.jpg");
wqueen = new ImageIcon("whitequeen.jpg");
bknight = new ImageIcon("blackknight.jpg");
wknight = new ImageIcon("whiteknight.jpg");
brook = new ImageIcon("blackrook.jpg");
wrook = new ImageIcon("whiterook.jpg");
bbishop = new ImageIcon("blackbishop.jpg");
wbishop = new ImageIcon("whitebishop.jpg");
middlePanel = BoardPanel();
add(middlePanel);
setBackground(Color.cyan);
}
private JPanel BoardPanel() {
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(8, 8));
ButtonListener listener = new ButtonListener();
board = new JButton[8][8];
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
board[2][col] = new JButton("", emptyIcon);
board[3][col] = new JButton("", emptyIcon);
board[4][col] = new JButton("", emptyIcon);
board[5][col] = new JButton("", emptyIcon);
board[6][col] = new JButton("", emptyIcon);
board[6][col] = new JButton("", bpawn);
board[7][0] = new JButton("", brook);
board[7][1] = new JButton("", bknight);
board[7][2] = new JButton("", bbishop);
board[7][3] = new JButton("", bking);
board[7][4] = new JButton("", bqueen);
board[7][5] = new JButton("", bbishop);
board[7][6] = new JButton("", bknight);
board[7][7] = new JButton("", brook);
board[1][col] = new JButton("", wpawn);
board[0][0] = new JButton("", wrook);
board[0][1] = new JButton("", wknight);
board[0][2] = new JButton("", wbishop);
board[0][3] = new JButton("", wking);
board[0][4] = new JButton("", wqueen);
board[0][5] = new JButton("", wbishop);
board[0][6] = new JButton("", wknight);
board[0][7] = new JButton("", wrook);
board[row][col].addActionListener(listener);
panel.add(board[row][col]);
}
}
// for (int i = 1; i <= 2; i++) {
// row = p.player() == Player.BLACK ? 0 : 8;
//
// for (col = 1; col <= 8; col++) {
//
// switch (col) {
// case 1: case 8:
// Square[row][col]= new JLabel("Rook", JLabel.CENTER);
// break;
// case 2: case 7:
// Square[row][col]= new JLabel("Kinght", JLabel.CENTER);
// break;
// case 3: case 6:
// Square[row][col]= new JLabel("Bishop", JLabel.CENTER);;
// break;
// case 4:
// Square[row][col]= new JLabel("Queen", JLabel.CENTER);
// break;
// case 5:
// Square[row][col]= new JLabel("King", JLabel.CENTER);
// break;
// }
// }
// }
return panel;
}
private void displayBoard() {
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
if (player == null) {
board[row][col].setIcon(emptyIcon);
}
if (player == Player.BLACK) {
if (model.pieceAt(row, col) instanceof Pawn) {
board[row][col].setIcon(bpawn);
}
if (model.pieceAt(row, col) instanceof Knight) {
board[row][col].setIcon(bknight);
}
if (model.pieceAt(row, col) instanceof King) {
board[row][col].setIcon(bking);
}
if (model.pieceAt(row, col) instanceof Queen) {
board[row][col].setIcon(bqueen);
}
if (model.pieceAt(row, col) instanceof Rook) {
board[row][col].setIcon(brook);
}
if (model.pieceAt(row, col) instanceof Bishop) {
board[row][col].setIcon(bbishop);
}
}
if (player == Player.WHITE) {
if (model.pieceAt(row, col) instanceof Pawn) {
board[row][col].setIcon(wpawn);
}
if (model.pieceAt(row, col) instanceof Knight) {
board[row][col].setIcon(wknight);
}
if (model.pieceAt(row, col) instanceof King) {
board[row][col].setIcon(wking);
}
if (model.pieceAt(row, col) instanceof Queen) {
board[row][col].setIcon(wqueen);
}
if (model.pieceAt(row, col) instanceof Rook) {
board[row][col].setIcon(wrook);
}
if (model.pieceAt(row, col) instanceof Bishop) {
board[row][col].setIcon(wbishop);
}
}
}
}
}
public class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
if (board[row][col] == event.getSource()) {
}
}
displayBoard();
}
}
}