Commit 37d719c1 authored by Omid Sayfun's avatar Omid Sayfun

Final Looks Checked

parent 2a18e034
Pipeline #617 canceled with stages
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.util.*;
import lab.game.*;
......@@ -13,6 +15,10 @@ public class Main{
Chess newChess = new Chess();
newChess.run();
}
public static void rerun(){
Chess newChess = new Chess();
newChess.run();
}
}
/**
......@@ -87,6 +93,7 @@ class Chess implements MouseListener{
private String color = "W";
private boolean isSelected = false;
private newJButton inMove = null;
private JFrame frame;
private ArrayList<newJButton> btns = new ArrayList<newJButton>();
private ArrayList<newJButton> whiteLost = new ArrayList<>();
private ArrayList<newJButton> blackLost = new ArrayList<>();
......@@ -178,7 +185,7 @@ class Chess implements MouseListener{
if( (i % 2 == 1 && j % 2 == 1) || (i % 2 == 0 && j % 2 == 0) ){
btn.setBackground(new Color(219,217,164));
}else{
btn.setBackground(new Color(4,51,106));
btn.setBackground(new Color(46, 83,106));
}
boardPanel.add(btn);
}
......@@ -187,34 +194,40 @@ class Chess implements MouseListener{
JPanel leftDivider = new JPanel(new GridLayout(3, 1));
// Top Left
JPanel topLeft = new JPanel(new GridLayout(2, 8));
topLeft.setBorder(new EmptyBorder(50, 10, 50, 0));
for(char i = 0; i < 2; i++){
for(int j = 0; j < 8; j++){
newJButton btn = new newJButton();
ImageIcon icon = new ImageIcon(
new BufferedImage(40, 40, BufferedImage.TYPE_INT_ARGB));
btn.setIcon(icon);
btn.setPreferredSize(new Dimension(70, 70));
btn.setFocusable(false);
btn.setBackground(new Color(0,255,0));
btn.setBackground(new Color(46, 83,106));
topLeft.add(btn);
whiteLost.add(btn);
}
}
// Middle Left
JPanel middleLeft = new JPanel();
middleLeft.setBorder(new EmptyBorder(100, 0, 0, 0));
JLabel playing = new JLabel("White Player is playing");
playing.setFont(new Font("Segoe UI", Font.BOLD, 32));
caption = playing;
middleLeft.add(playing);
leftDivider.add(middleLeft);
// Bottom Left
JPanel bottomLeft = new JPanel(new GridLayout(2, 8));
bottomLeft.setBorder(new EmptyBorder(50, 10, 50, 0));
for(int i = 0; i < 2; i++){
for(int j = 0; j < 8; j++){
newJButton btn = new newJButton();
ImageIcon icon = new ImageIcon(
new BufferedImage(40, 40, BufferedImage.TYPE_INT_ARGB));
btn.setPreferredSize(new Dimension(70, 70));
btn.setIcon(icon);
btn.setFocusable(false);
btn.setBackground(new Color(0,255,0));
btn.setBackground(new Color(219,217,164));
bottomLeft.add(btn);
blackLost.add(btn);
}
......@@ -232,6 +245,8 @@ class Chess implements MouseListener{
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setVisible(true);
this.frame = frame;
}
/**
......@@ -306,14 +321,14 @@ class Chess implements MouseListener{
if( (btn.getNewY() % 2 == 1 && btn.getNewX() % 2 == 1) || (btn.getNewY() % 2 == 0 && btn.getNewX() % 2 == 0) ){
btn.setBackground(new Color(219,217,164));
}else{
btn.setBackground(new Color(4,51,106));
btn.setBackground(new Color(46, 83,106));
}
this.isSelected = false;
this.inMove = null;
}
for(newJButton btn : this.whiteLost) {
if( btn.getPiece() != null ){
Image img = new BufferedImage(40, 40, BufferedImage.TYPE_INT_ARGB);
Image img = new BufferedImage(50, 50, BufferedImage.TYPE_INT_ARGB);
try {
img = ImageIO.read(Chess.class.getResource("resources/images/" + btn.getPiece().getName().toLowerCase() + ".png"));
} catch (IOException e1) {
......@@ -353,6 +368,23 @@ class Chess implements MouseListener{
// this.isSelected = false;
// this.inMove = null;
}
if( this.board.checkMate() != null ){
String prompt = "";
if( this.board.checkMate().equals("W") ){
prompt = prompt.concat("Black Player Won!");
}else{
prompt = prompt.concat("White Player Won!");
}
prompt = prompt.concat(" \nDo you want to start a new game?");
int result = JOptionPane.showConfirmDialog(this.frame, prompt, "Finished", JOptionPane.YES_NO_OPTION);
if( result == JOptionPane.YES_OPTION ){
Main.rerun();
}
this.frame.dispatchEvent(new WindowEvent(this.frame, WindowEvent.WINDOW_CLOSING));
}
}
}
......
This diff is collapsed.
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