Commit 28c207d0 authored by hamed's avatar hamed

Fix ChessGUI

parent 54114b5f
package Main;
import Pieces.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
public class ChessGUI extends JFrame {
private Board b = new Board();
private JButton buttonBoard[][] = new JButton[8][8];
private JButton blackRemoveButton[] = new JButton[16];
private JButton whiteRemoveButton[] = new JButton[16];
private JLabel notificationLabel = new JLabel();
private boolean validMoves[][] = null;
private int startX;
private int startY;
private int endX;
private int endY;
private boolean turn = true;
public static void main(String args[]){
new ChessGUI();
}
public ChessGUI(){
super();
setSize(1200,600);
this.setLayout(new GridLayout(1,2));
JPanel chessBoard = new JPanel();
chessBoard.setLayout(new GridLayout(8,8));
initlizeChessBoardPanel(chessBoard);
JPanel slide2 = new JPanel();
JPanel blackRemoveBoard = new JPanel();
JPanel whiteRemoveBoard = new JPanel();
JPanel turnPanel = new JPanel();
turnPanel.add(notificationLabel);
notificationLabel.setFont(new Font("Arial MT Bold",Font.BOLD,24));
slide2.setLayout(new GridLayout(3,1));
blackRemoveBoard.setLayout(new GridLayout(2,8));
whiteRemoveBoard.setLayout(new GridLayout(2,8));
inilitzeRemoveBoardPanel(blackRemoveBoard,false);
inilitzeRemoveBoardPanel(whiteRemoveBoard,true);
add(chessBoard);
add(slide2);
slide2.add(blackRemoveBoard);
slide2.add(turnPanel);
slide2.add(whiteRemoveBoard);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
private void updateRemovePanel(){
int i = 0;
for (Piece p :b.getWhiteRemovedList()){
whiteRemoveButton[i++].setIcon(new ImageIcon(getPieceImage(p)));
}
i=0;
for (Piece p :b.getBlackRemovedList()){
blackRemoveButton[i++].setIcon(new ImageIcon(getPieceImage(p)));
}
}
private void inilitzeRemoveBoardPanel(JPanel removeBoard,boolean isWhite){
for (int i = 0 ; i < 2 ;i++){
for (int j = 0 ; j < 8 ;j++){
JButton button = new JButton();
button.setBackground(Color.pink);
removeBoard.add(button);
if(isWhite){
whiteRemoveButton[i+j] = button;
}
else {
blackRemoveButton[i+j] = button;
}
}
}
}
private void initlizeChessBoardPanel(JPanel chessBoard){
for (int j = 0 ; j < 8 ;j++){
for (int i = 0 ; i < 8 ; i++){
JButton button = new JButton();
if((i+j)%2==0){
button.setBackground(Color.WHITE);
}else {
button.setBackground(Color.lightGray);
}
if(b.getBoard()[i][j]!=null){
button.setIcon(new ImageIcon(getPieceImage(b.getBoard()[i][j])));
}
final int fI = i;
final int fJ = j;
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
startX = endX;
startY = endY;
endX = fI;
endY = fJ;
// System.out.println(startX+","+startY);
if(validMoves !=null && validMoves[endX][endY]){
b.getMove(startX,startY,endX,endY);
if(b.KishCheck(true)){
notificationLabel.setText("White Kish...");
}else if(b.KishCheck(false)){
notificationLabel.setText("Black Kish...");
}
else
if(turn){
notificationLabel.setText("White Turn...");
}
else if (!turn){
notificationLabel.setText("Black Turn...");
}
turn = !turn;
updateRemovePanel();
// System.out.println("Moves Approved");
}
validMoves = getValidMoves( b.getBoard()[endX][endY]);
updateButtons();
}
});
buttonBoard[i][j] = button;
chessBoard.add(button);
}
}
}
private void updateButtons(){
for (int j = 0 ; j < 8 ;j++){
for (int i = 0 ; i < 8 ; i++){
if((i+j)%2==0){
buttonBoard[i][j].setBackground(Color.WHITE);
}else {
buttonBoard[i][j].setBackground(Color.lightGray);
}
if(validMoves!=null && validMoves[i][j]){
buttonBoard[i][j].setBackground(Color.YELLOW);
}
if(b.getBoard()[i][j]!=null){
buttonBoard[i][j].setIcon(new ImageIcon(getPieceImage(b.getBoard()[i][j])));
}else{
buttonBoard[i][j].setIcon(new ImageIcon());
}
}
}
}
private boolean[][] getValidMoves(Piece p){
if (p==null) return null;
if(p.moves().isEmpty()) return null;
if(turn && p.isWhite() || (!turn)&& p.isBlack()) {
boolean validmoves[][] = new boolean[8][8];
for (int a[] : p.moves()) {
validmoves[a[0]][a[1]] = true;
}
return validmoves;
}
return null;
}
private Image getPieceImage(Piece p){
BufferedImage ret = null;
try {
if (p.isWhite()) {
if (p instanceof Bishop) {
ret = ImageIO.read(new File("./Chess/Chess_blt60.png"));
} else if (p instanceof King) {
ret = ImageIO.read(new File("./Chess/Chess_klt60.png"));
} else if (p instanceof Knight) {
ret = ImageIO.read(new File("./Chess/Chess_nlt60.png"));
} else if (p instanceof Pawn) {
ret = ImageIO.read(new File("./Chess/Chess_plt60.png"));
} else if (p instanceof Queen) {
ret = ImageIO.read(new File("./Chess/Chess_qlt60.png"));
} else if (p instanceof Rook) {
ret = ImageIO.read(new File("./Chess/Chess_rlt60.png"));
}
} else {
if (p instanceof Bishop) {
ret = ImageIO.read(new File("./Chess/Chess_bdt60.png"));
} else if (p instanceof King) {
ret = ImageIO.read(new File("./Chess/Chess_kdt60.png"));
} else if (p instanceof Knight) {
ret = ImageIO.read(new File("./Chess/Chess_ndt60.png"));
} else if (p instanceof Pawn) {
ret = ImageIO.read(new File("./Chess/Chess_pdt60.png"));
} else if (p instanceof Queen) {
ret = ImageIO.read(new File("./Chess/Chess_qdt60.png"));
} else if (p instanceof Rook) {
ret = ImageIO.read(new File("./Chess/Chess_rdt60.png"));
}
}
}
catch (Exception e){
}
return ret;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment