import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
public class PromotionFrame extends JFrame implements MouseListener {
private JFrame master;
private String piece;
public PromotionFrame(JFrame master, int color) {
super();
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
Container con = this.getContentPane();
setSize(400,200);
this.master = master;
setLocation(master.getX()+master.getWidth()/2-200,master.getY()+master.getHeight()/2-100);
con.setLayout(new GridLayout(1,4));
ImageIcon imgicon1;
Image img1;
Image newimg;
ImageIcon img;
ImageLabel label;
piece = null;
if(color == Colors.WHITE)
imgicon1 = new ImageIcon("wq.png");
else
imgicon1 = new ImageIcon("bq.png");
img1 = imgicon1.getImage();
newimg = img1.getScaledInstance(100, 100, java.awt.Image.SCALE_SMOOTH);
img = new ImageIcon(newimg);
label = new ImageLabel(img, "queen");
label.addMouseListener(this);
con.add(label);
if(color == Colors.WHITE)
imgicon1 = new ImageIcon("wr.png");
else
imgicon1 = new ImageIcon("br.png");
img1 = imgicon1.getImage();
newimg = img1.getScaledInstance(100, 100, java.awt.Image.SCALE_SMOOTH);
img = new ImageIcon(newimg);
label = new ImageLabel(img, "rook");
label.addMouseListener(this);
con.add(label);
if(color == Colors.WHITE)
imgicon1 = new ImageIcon("wb.png");
else
imgicon1 = new ImageIcon("bb.png");
img1 = imgicon1.getImage();
newimg = img1.getScaledInstance(100, 100, java.awt.Image.SCALE_SMOOTH);
img = new ImageIcon(newimg);
label = new ImageLabel(img, "bishop");
label.addMouseListener(this);
con.add(label);
if(color == Colors.WHITE)
imgicon1 = new ImageIcon("wn.png");
else
imgicon1 = new ImageIcon("bn.png");
img1 = imgicon1.getImage();
newimg = img1.getScaledInstance(100, 100, java.awt.Image.SCALE_SMOOTH);
img = new ImageIcon(newimg);
label = new ImageLabel(img, "knight");
label.addMouseListener(this);
con.add(label);
this.show();
}
public String getPiece() {
return piece;
}
/* Empty method definition. */
public void mousePressed(MouseEvent e) {
ImageLabel label = (ImageLabel)e.getSource();
piece = label.getPiece();
}
/* Empty method definition. */
public void mouseReleased(MouseEvent e) {
}
/* Empty method definition. */
public void mouseEntered(MouseEvent e) {
}
/* Empty method definition. */
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
private class ImageLabel extends JLabel {
private String piece;
ImageLabel(ImageIcon img, String piece) {
super(img);
this.piece = piece;
}
public String getPiece() {
return piece;
}
}
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class GameFrame extends JFrame {
private SquarePanel[][] squares;
public GameFrame(Game game) {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container con = this.getContentPane();
setSize(800,800);
setLocation(0,0);
con.setLayout(new GridLayout(8,8));
SquarePanel pane = null;
SquarePanel lastpanel = null;
squares = new SquarePanel[8][8];
int color = Colors.WHITE;
for(int i = 7; i >= 0; --i) {
if(color == Colors.WHITE)
color = Colors.BLACK;
else
color = Colors.WHITE;
for(int j = 0; j <= 7; ++j) {
if(color == Colors.WHITE)
color = Colors.BLACK;
else
color = Colors.WHITE;
lastpanel = new SquarePanel(color,j,i);
pane = lastpanel;
con.add(pane);
squares[j][i] = pane;
}
}
Board board = game.getBoard();
for(int i = 0; i < 8; ++i) {
for(int j = 0; j < 8; ++j) {
squares[j][i].setResident(board.getSquare(j,i).getResident());
//System.out.println(squares[j][i].getResident());
//squares[j][i].repaint();
}
}
MouseListener listener = new MoveListener(game, this, squares);
for(int i = 0; i < 8; ++i)
for(int j = 0; j < 8; ++j)
squares[i][j].addMouseListener(listener);
setVisible(true);
}
public static void main(String[] args) {
GameFrame g = new GameFrame(new Game());
PromotionFrame pframe = new PromotionFrame(g, 1);
String piece;
Piece p2;
do {
piece = pframe.getPiece();
}
while(piece == null);
pframe.dispose();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MoveListener implements MouseListener {
private int first_click;
private SquarePanel first_source;
private Game game;
private JFrame frame;
private int turn;
private SquarePanel[][] squares;
public MoveListener(Game game, JFrame f, SquarePanel[][] s) {
first_click = 0;
this.game = game;
this.frame = f;
turn = Colors.WHITE;
squares = s;
}
/* Empty method definition. */
public void mousePressed(MouseEvent e) {
SquarePanel p = (SquarePanel)e.getSource();
ImageIcon content = null;
if(first_click == 0) {
first_source = p;
++first_click;
p.setBorder(BorderFactory.createLineBorder(Color.cyan,4));
}
else {
first_click = 0;
first_source.setBorder(BorderFactory.createLineBorder(Color.black));
//System.out.println(turn == Colors.WHITE ? "White" : "Black");
if(first_source.getResident() == null) {
new ErrorDialog(frame,"Not a Move");
}
else if(first_source.getResident().getColor() != turn) {
new ErrorDialog(frame,"Not your turn");
}
else if(game.move(first_source.getXCoordinate(), first_source.getYCoordinate(),p.getXCoordinate(), p.getYCoordinate())) {
p.setResident(null);
p.setResident(first_source.getResident());
first_source.setResident(null);
//System.out.println(p.getXCoordinate()+" "+p.getY());
//check to see if this was a promotion
if(p.getResident() instanceof Pawn && p.getYCoordinate() == 7) {
PromotionFrame pframe = new PromotionFrame(frame, p.getResident().getColor());
String piece;
Piece p2;
do {
piece = pframe.getPiece();
}
while(piece == null);
pframe.dispose();
p2 = game.promote((Pawn)p.getResident(),piece);
p.setResident(p2);
}
//check if this was a castle. game.move() returns true only if we can castle, so no need to check stuff here
if(p.getResident() instanceof King && Math.abs(first_source.getXCoordinate()-p.getXCoordinate()) == 2) {
if(p.getResident().getColor() == Colors.WHITE) {
if(p.getXCoordinate() == 6) {
//System.out.println("here castle");
squares[5][0].setResident(squares[7][0].getResident());
squares[7][0].setResident(null);
squares[7][0].repaint();
}
if(p.getXCoordinate() == 2) {
squares[3][0].setResident(squares[0][0].getResident());
squares[0][0].setResident(null);
squares[0][0].repaint();
}
}
if(p.getResident().getColor() == Colors.BLACK) {
if(p.getXCoordinate() == 6) {
squares[5][7].setResident(squares[7][7].getResident());
squares[7][7].setResident(null);
squares[7][7].repaint();
}
if(p.getXCoordinate() == 2) {
squares[3][7].setResident(squares[0][7].getResident());
squares[0][7].setResident(null);
squares[0][7].repaint();
}
}
}
//check to see if this was an en passant move
if(p.getResident() instanceof Pawn) {
if(((Pawn)p.getResident()).getEnPassant()) {
squares[p.getXCoordinate()][first_source.getYCoordinate()].setResident(null);
squares[p.getXCoordinate()][first_source.getYCoordinate()].repaint();
}
}
turn = (turn == Colors.WHITE ? Colors.BLACK : Colors.WHITE);
Piece piece;
// this is necessary here for en passant. after someones turn, we need to cycle through all the pawns and reset the just_moved and en_passant variables
for(int i = 0; i < 8; ++i) {
for(int j = 0; j < 8; ++j) {
piece = squares[i][j].getResident();
if(piece instanceof Pawn && piece.getColor() == turn)
((Pawn)piece).setJustMoved(false);
if(piece instanceof Pawn && piece.getColor() != turn)
((Pawn)piece).setEnPassant(false);
}
}
if(game.checkMate(turn)) {
if(turn == Colors.WHITE)
new ErrorDialog(frame, "Game over: Black wins");
else
new ErrorDialog(frame, "Game over: White wins");
}
game.dump();
}
else {
new ErrorDialog(frame,"Illegal Move");
}
//game.displayAttacks();
}
}
/* Empty method definition. */
public void mouseReleased(MouseEvent e) {
}
/* Empty method definition. */
public void mouseEntered(MouseEvent e) {
}
/* Empty method definition. */
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
}