import javax.swing.*;
import java.util.ArrayList;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Mitch
*/
import javax.swing.*;
import java.awt.event.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Mitch
*/
class JButtonExt extends JButton {
PieceColour holdsColour;
PieceColour defaultColour;
int[] location;
public JButtonExt(PieceColour newColour, int[] newLocation) {
this.location = newLocation;
this.defaultColour = newColour;
this.holdsColour = null;
}
}
enum PieceType {PAWN, ROOK, KNIGHT, BISHOP, QUEEN, KING};
enum PieceColour {BLANK, BLACK, WHITE};
class ChessBoard extends JFrame {
private Container c;
private JButtonExt[][] chessBoard = new JButtonExt[8][8];
public static void main(String[] args) {
ChessBoard window = new ChessBoard();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
}
class buttonClick implements ActionListener { // Listen class to launch function for close menu item
@Override
public void actionPerformed(ActionEvent e) {
//System.out.println("click");
}
}
public ChessBoard() {
c = getContentPane();
//create buttons for the game board
//buttons will be able to drag and drop images
for (int x=0; x<8; x++) {
for (int y=0; y<8; y++) {
int g = x+y;
JButtonExt label;
if (g%2==0) {//white squares
label = new JButtonExt(PieceColour.WHITE, new int[]{x,y});
}
else {//black square
label = new JButtonExt(PieceColour.BLACK, new int[]{x,y});
}
//System.out.println(label.getIcon());
TransferHandler transfer = new TransferHandler("icon");
if (g % 2 == 0) { //white square
label.setFont(new java.awt.Font("Dialog", Font.BOLD, 14));
}
else { //black squares
}
//set the location and size of each button depending on where they are located on the board
label.setLocation((y*60)+60, (x*60)+60);
label.setSize(60, 60);
label.addActionListener(new ChessBoard.buttonClick()); //allow the user to click on a square and show available moves
label.setTransferHandler(transfer); //allow the button to accept drag and drop functions
label.addMouseListener(new MouseAdapter(){ //function that drags and drops the icons
@Override
public void mousePressed(MouseEvent e){
JButtonExt previousSquare = (JButtonExt)e.getSource();
TransferHandler handle = previousSquare.getTransferHandler();
handle.exportAsDrag(previousSquare, e, TransferHandler.COPY);
System.out.println(previousSquare.location[0] + " " + previousSquare.location[1]);
}
@Override
public void mouseReleased(MouseEvent e) {
System.out.println("test");
}
});
chessBoard[x][y] = label;
}
}
for(JButtonExt a[] : chessBoard) {
for (JButtonExt button: a) {
int x = button.location[0];
int y = button.location[1];
String col;
if (button.defaultColour == PieceColour.BLACK) {
col = "Dark";
} else {
col = "Light";
}
if (x == 1) {
ImageIcon icon = new ImageIcon("images/whitePawn" + col + ".jpg");
button.setIcon(icon);
}
else if (x == 6) {
ImageIcon icon = new ImageIcon("images/blackPawn" + col + ".jpg");
button.setIcon(icon);
}
else if (x == 0) {
if (y == 0 || y == 7) {
ImageIcon icon = new ImageIcon("images/whiteRook" + col + ".jpg");
button.setIcon(icon);
}
else if (y == 1 || y == 6) {
ImageIcon icon = new ImageIcon("images/whiteBishop" + col + ".jpg");
button.setIcon(icon);
}
else if (y == 2 || y == 5) {
ImageIcon icon = new ImageIcon("images/whiteKnight" + col + ".jpg");
button.setIcon(icon);
}
else if (y == 3) {
ImageIcon icon = new ImageIcon("images/whiteQueen" + col + ".jpg");
button.setIcon(icon);
}
else {
ImageIcon icon = new ImageIcon("images/whiteKing" + col + ".jpg");
button.setIcon(icon);
}
}
else if (x == 7) {
if (y == 0 || y == 7) {
ImageIcon icon = new ImageIcon("images/blackRook" + col + ".jpg");
button.setIcon(icon);
}
else if (y == 1 || y == 6) {
ImageIcon icon = new ImageIcon("images/blackBishop" + col + ".jpg");
button.setIcon(icon);
}
else if (y == 2 || y == 5) {
ImageIcon icon = new ImageIcon("images/blackKnight" + col + ".jpg");
button.setIcon(icon);
}
else if (y == 3) {
ImageIcon icon = new ImageIcon("images/blackQueen" + col + ".jpg");
button.setIcon(icon);
}
else {
ImageIcon icon = new ImageIcon("images/blackKing" + col + ".jpg");
button.setIcon(icon);
}
}
else {
ImageIcon icon = new ImageIcon("images/"+ col + ".jpg");
button.setIcon(icon);
}
c.add(button);
}
}
JLabel label = new JLabel("");
c.add(label);
/*for (int h=0; h<2;h++) {
for (int i=0;i<9;i++) {
if (h==1) {
players[h][i] = new Piece(PieceType.PAWN, PieceColour.WHITE, {h,i}, "whitePawn");
}
}
}*/
this.setTitle("Chess");
this.setContentPane(c);
this.setBounds(0, 0, 650, 650);
this.setLocationRelativeTo(null);
}
}