Commit cb2c3846 authored by hamed's avatar hamed

Add Fame GUI to Project Ver 1.

parent 2632294b
......@@ -4,6 +4,7 @@
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/Chess" type="java-resource" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
......
......@@ -6,7 +6,10 @@ import java.util.ArrayList;
import java.util.Queue;
public class Board {
private ArrayList<Piece> whiteRemovedList = new ArrayList<>();
private ArrayList<Piece> blackRemovedList = new ArrayList<>();
private Piece board[][] = new Piece[8][8];
private boolean whiteMove = true;
public Board(){
board[0][0] = new Rook(0,0,false,this);
board[7][0] = new Rook(7,0,false,this);
......@@ -33,17 +36,35 @@ public class Board {
board[4][7] = new King(4,7,true,this);
}
public ArrayList<Piece> getBlackRemovedList(){
return blackRemovedList;
}
public ArrayList<Piece> getWhiteRemovedList(){
return whiteRemovedList;
}
public boolean getMove(int x , int y , int destX , int destY){
if(board[x][y]==null) return false;
// System.out.println(x+","+y+"->"+destX+","+destY);
// System.out.println(board[x][y]);
if (board[x][y].isValidMove(destX,destY)){
// System.out.println("Reach");
board[x][y].setX(destX);
board[x][y].setY(destY);
board[destX][destY] = getCopy(board[x][y]);
board[x][y] = null;
return true;
if((getColor(x, y)==1 && whiteMove)|| (getColor(x, y)==2 && !whiteMove)) {
if (board[x][y].isValidMove(destX, destY)) {
// System.out.println("Reach");
board[x][y].setX(destX);
board[x][y].setY(destY);
if(board[destX][destY]!=null){
if(board[destX][destY].isWhite()){
whiteRemovedList.add(getCopy(board[destX][destY]));
System.out.println(whiteRemovedList.size());
}else {
blackRemovedList.add(getCopy(board[destX][destY]));
System.out.println(blackRemovedList.size());
}
}
board[destX][destY] = getCopy(board[x][y]);
board[x][y] = null;
whiteMove = !whiteMove;
return true;
} else return false;
}
else return false;
}
......
package Main;
import Pieces.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.File;
public class ChessFrame extends JFrame implements MouseListener, MouseMotionListener {
private static Board b = new Board();
private static int WIDTH = 750;
private static int WIDTH_2= 500;
private static int HEIGHT = 600;
private static int yBias = 50;
private int startX;
private int startY;
private int endX;
private int endY;
public static void main(String args[]){
new ChessFrame();
}
public ChessFrame(){
super();
addMouseListener(this);
setSize(WIDTH_2+WIDTH+12,yBias+HEIGHT+12);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
private Image getPieceImage(Piece p){
BufferedImage ret = null;
try {
if (p.isWhite()) {
if (p instanceof Bishop) {
ret = ImageIO.read(new File("D:/University/MyCodes/Intellij/ChessProject/Chess/Chess_blt60.png"));
} else if (p instanceof King) {
ret = ImageIO.read(new File("D:/University/MyCodes/Intellij/ChessProject/Chess/Chess_klt60.png"));
} else if (p instanceof Knight) {
ret = ImageIO.read(new File("D:/University/MyCodes/Intellij/ChessProject/Chess/Chess_nlt60.png"));
} else if (p instanceof Pawn) {
ret = ImageIO.read(new File("D:/University/MyCodes/Intellij/ChessProject/Chess/Chess_plt60.png"));
} else if (p instanceof Queen) {
ret = ImageIO.read(new File("D:/University/MyCodes/Intellij/ChessProject/Chess/Chess_qlt60.png"));
} else if (p instanceof Rook) {
ret = ImageIO.read(new File("D:/University/MyCodes/Intellij/ChessProject/Chess/Chess_rlt60.png"));
}
} else {
if (p instanceof Bishop) {
ret = ImageIO.read(new File("D:/University/MyCodes/Intellij/ChessProject/Chess/Chess_bdt60.png"));
} else if (p instanceof King) {
ret = ImageIO.read(new File("D:/University/MyCodes/Intellij/ChessProject/Chess/Chess_kdt60.png"));
} else if (p instanceof Knight) {
ret = ImageIO.read(new File("D:/University/MyCodes/Intellij/ChessProject/Chess/Chess_ndt60.png"));
} else if (p instanceof Pawn) {
ret = ImageIO.read(new File("D:/University/MyCodes/Intellij/ChessProject/Chess/Chess_pdt60.png"));
} else if (p instanceof Queen) {
ret = ImageIO.read(new File("D:/University/MyCodes/Intellij/ChessProject/Chess/Chess_qdt60.png"));
} else if (p instanceof Rook) {
ret = ImageIO.read(new File("D:/University/MyCodes/Intellij/ChessProject/Chess/Chess_rdt60.png"));
}
}
}
catch (Exception e){
}
return ret;
}
@Override
public void paint(Graphics g){
super.paint(g);
g.setColor(Color.WHITE);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.BLACK);
int dy = HEIGHT/8;
int dx = WIDTH/8;
for(int i = 0 ; i <= 8 ; i++){ // Pain Chess Board
int H=i*dy;
int W = i*dx;
g.drawLine(0, yBias+H, WIDTH, yBias+H);
g.drawLine(0, yBias+H+1, WIDTH, yBias+H+1);
g.drawLine(0, yBias+H+2, WIDTH, yBias+H+2);
//-----------------------------------------------
g.drawLine(W, yBias, W, getHeight());
g.drawLine(W+1, yBias, W+1, getHeight());
g.drawLine(W+2, yBias, W+2, getHeight());
}
g.setColor(Color.GRAY);
for (int i = 0 ; i < 8 ; i++){
for (int j = 0 ; j < 8 ; j++){
if((i+j)%2==0){
g.fillRect(i*dx,yBias+j*dy,dx,dy);
}
if(!b.isEmpty(i,j)){
Image image = getPieceImage(b.getBoard()[i][j]);
// SwingUtilities.
// g.dra
g.drawImage(image,i*dx,yBias+j*dy,dx,dy,this);
}
}
}
//--------------------------- Paint Extra Board...
g.setColor(Color.BLACK);
dx = WIDTH_2/8;
for (int i = 0 ; i <= 8 ;i++){
int W = i*dx+WIDTH;
g.drawLine(W, yBias, W, yBias+dy*2);
g.drawLine(W+1, yBias, W+1, yBias+dy*2);
g.drawLine(W+2, yBias, W+2, yBias+dy*2);
//--------------------------------
g.drawLine(W, HEIGHT+yBias-2*dy, W, getHeight());
g.drawLine(W+1, HEIGHT+yBias-2*dy, W+1, getHeight());
g.drawLine(W+2, HEIGHT+yBias-2*dy, W+2, getHeight());
}
for (int i = 0 ; i < 3 ; i++){
g.drawLine(WIDTH,i*dy+yBias,getWidth(),i*dy+yBias);
g.drawLine(WIDTH,i*dy+yBias+1,getWidth(),i*dy+yBias+1);
g.drawLine(WIDTH,i*dy+yBias+2,getWidth(),i*dy+yBias+2);
g.drawLine(WIDTH,HEIGHT-(i*dy)+yBias,getWidth(),HEIGHT-(i*dy)+yBias);
g.drawLine(WIDTH,HEIGHT-(i*dy-1)+yBias,getWidth(),HEIGHT-(i*dy-1)+yBias);
g.drawLine(WIDTH,HEIGHT-(i*dy-2)+yBias,getWidth()+yBias,HEIGHT-(i*dy-2)+yBias);
}
//----------------------
for(int i = 0 ; i < 8 ; i++){
for(int j = 0 ; j < 2 ;j++){
int ind= i+j*8;
if(ind < b.getBlackRemovedList().size()){
Image image = getPieceImage(b.getBlackRemovedList().get(ind));
g.drawImage(image,WIDTH+i*dx,yBias+j*dy,dx,dy,this);
}
//------------------------------------------
if(ind < b.getWhiteRemovedList().size()){
Image image = getPieceImage(b.getWhiteRemovedList().get(ind));
g.drawImage(image,WIDTH+i*dx,HEIGHT+yBias-(j+1)*dy,dx,dy,this);
}
}
}
}
@Override
public void mouseClicked(MouseEvent e) {
// if(SwingUtilities.isLeftMouseButton(e)){
// int x = e.getX();
// int y = e.getY()-yBias;
// int dx = WIDTH/8;
// int dy = HEIGHT/8;
// System.out.println(x/dx+","+y/dy);
// }
}
@Override
public void mousePressed(MouseEvent e) {
if(SwingUtilities.isLeftMouseButton(e)){
int x = e.getX();
int y = e.getY()-yBias;
if(x > WIDTH || y > HEIGHT) return;
int dx = WIDTH/8;
int dy = HEIGHT/8;
startX = x/dx;
startY = y/dy;
System.out.println(x/dx+","+y/dy);
}
}
@Override
public void mouseReleased(MouseEvent e) {
if(SwingUtilities.isLeftMouseButton(e)){
int x = e.getX();
int y = e.getY()-yBias;
if(x > WIDTH || y > HEIGHT) return;
int dx = WIDTH/8;
int dy = HEIGHT/8;
endX = x/dx;
endY = y/dy;
System.out.println(x/dx+","+y/dy);
b.getMove(startX,startY,endX,endY);
repaint();
}
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mouseDragged(MouseEvent e) {
}
@Override
public void mouseMoved(MouseEvent e) {
}
}
......@@ -57,4 +57,5 @@ public abstract class Piece implements Cloneable {
}
return null;
}
// public void dra
}
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