ChessBoard , my attempt and learning to develope chess board
this is my attempt to learn how to create simple chessboard in java.. ill post my code as i progress, any suggestions is greate.
i use netbeans :) and jdk1.6rc. peace
/*
* ChessBoard.java
*
* Created on November 26, 2006, 10:06 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
/**
*
* @author Bilal El Uneis
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ChessBoard extends JFrame implements MouseListener
{
JPanel [][] squares;
/** Creates a new instance of ChessBoard */
public ChessBoard()
{
Container c = getContentPane();
c.setLayout(new GridLayout(8,8, 1 , 1));
squares = new JPanel[8][8];
for(int i=0; i<8; i++)
{
for(int j=0; j<8; j++)
{
squares[j] = new JPanel();
if((i+j)%2 == 0)
squares[j].setBackground(Color.white);
else
squares[j].setBackground(Color.black);
squares[j].addMouseListener(this);
c.add(squares[j]);
}
}
}
public void mouseClicked(MouseEvent e){ }
public void mouseEntered(MouseEvent e){ }
public void mouseExited(MouseEvent e) { }
public void mousePressed(MouseEvent e) { }
public void mouseReleased(MouseEvent e) { }
public static void main(String args[])
{
ChessBoard test = new ChessBoard();
test.setSize(300,300);
test.setResizable(false);
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test.setVisible(true);
}
}
squares = new JPanel[8][8];
Instead of hard-coding in the numbers, why dont you declare a variable that keeps track of it for you.
private static final int BOARD_SIZE = 8;
That way, if you ever needed to modify the size of the board, you could just change that one variable.
yeah had the same problem in javagaming.org where i shared this code too.. the problem is when i copy and paste my code without using the code icon u get this effect.. a bug in the forum editing system ?
any way im trying this again with using the code icon.. peace
/*
* ChessBoard.java
*
* Created on November 26, 2006, 10:06 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
/**
*
* @author Bilal El Uneis
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ChessBoard extends JFrame implements MouseListener
{
JPanel [][] squares;
/** Creates a new instance of ChessBoard */
public ChessBoard()
{
Container c = getContentPane();
c.setLayout(new GridLayout(8,8, 1 , 1));
squares = new JPanel[8][8];
for(int i=0; i<8; i++)
{
for(int j=0; j<8; j++)
{
squares[i][j] = new JPanel();
if((i+j)%2 == 0)
squares[i][j].setBackground(Color.white);
else
squares[i][j].setBackground(Color.black);
squares[i][j].addMouseListener(this);
c.add(squares[i][j]);
}
}
}
public void addPiece(String piece_name,int i, int j)
{
Icon piece_icon = new ImageIcon(getClass().getResource(piece_name));
squares[i][j].add((JComponent)piece_icon);
}
public void mouseClicked(MouseEvent e){ }
public void mouseEntered(MouseEvent e){ }
public void mouseExited(MouseEvent e) { }
public void mousePressed(MouseEvent e) { }
public void mouseReleased(MouseEvent e) { }
public static void main(String args[])
{
ChessBoard test = new ChessBoard();
test.addPiece("king.bmp",0,0);
test.setSize(300,300);
test.setResizable(false);
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test.setVisible(true);
}
}
im trying to add image of king piece into my sqaure.. here is my code, but i dont see the image displayed.. any ideas?
/*
* ChessBoard.java
*
* Created on November 26, 2006, 10:06 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
/**
*
* @author Bilal El Uneis
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ChessBoard extends JFrame implements MouseListener
{
JPanel [][] squares;
/** Creates a new instance of ChessBoard */
public ChessBoard()
{
Container c = getContentPane();
c.setLayout(new GridLayout(8,8, 1 , 1));
squares = new JPanel[8][8];
for(int i=0; i<8; i++)
{
for(int j=0; j<8; j++)
{
squares[i][j] = new JPanel();
if((i+j)%2 == 0)
squares[i][j].setBackground(Color.white);
else
squares[i][j].setBackground(Color.black);
squares[i][j].addMouseListener(this);
c.add(squares[i][j]);
}
}
}
public void addPiece(String piece_name,int i, int j)
{
JLabel piece_icon = new JLabel();
piece_icon.setIcon(new ImageIcon(piece_name));
squares[i][j].add(piece_icon);
squares[i][j].repaint();
}
public void mouseClicked(MouseEvent e){ }
public void mouseEntered(MouseEvent e){ }
public void mouseExited(MouseEvent e) { }
public void mousePressed(MouseEvent e) { }
public void mouseReleased(MouseEvent e) { }
public static void main(String args[])
{
ChessBoard test = new ChessBoard();
test.addPiece("king.bmp",0,1);
test.setSize(300,300);
test.setResizable(false);
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test.setVisible(true);
}
}
> im trying to add image of king piece into my sqaure..
> here is my code, but i dont see the image displayed..
> any ideas?
I would advise against using bitmap images. Try a GIF or PNG instead. These formats support transparency. Also make sure the path to your images are correct. You may also want to use ImageIcon(URL location) instead of ImageIcon(String filename), which could be a problem with unsigned applets.
If I was developing a chess GUI, I wouldn't use JLabels. Instead, I'd paint the board and pieces by overriding the paintComponent(Graphics) method.
Check out http://www.okoware.com/applications/maverick_checkers/demo for ideas. They used to have some code somewhere on the site.
ok , after some readings and research and google, here is what i did..
this is class for the chess pieces, right now just to test one piece.
/*
* Piece.java
*
* Created on November 29, 2006, 11:35 AM
*
*/
/**
*
* @author Bilal El Uneis
*/
import javax.swing.*;
public class Piece extends JLabel
{
/** Creates a new instance of Piece */
public Piece()
{
super(new ImageIcon("king.gif"));
}
}
here ill created a plain board to extend later on...
/*
* Board.java
*
* Created on November 29, 2006, 11:04 AM
*
*/
/**
*
* @author Bilal El Uneis
*/
import java.awt.*;
import javax.swing.*;
public class Board
{
protected JPanel[][] squares;
protected JFrame boardFrame;
protected Container container;
/** Creates a new instance of Board */
public Board()
{
boardFrame = new JFrame();
boardFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
container = boardFrame.getContentPane();
container.setLayout(new GridLayout(8,8));
create_squares();
boardFrame.setSize(400,450);
boardFrame.setVisible(true);
}
private void create_squares()
{
squares = new JPanel[8][8];
for(int i=0;i<8;i++)
{
for(int j=0;j<8;j++)
{
JPanel p = new JPanel();
p.setBackground(setColor(i,j));
squares[i][j]=p;
container.add(p);
}
}
}
private Color setColor(int x, int y)
{
if((x+y)%2 == 0)
return Color.WHITE;
else
return Color.BLACK;
}
public static void main(String args[])
{
new Board();
}
}
here i created board2 to extend board and use Piece class to place king.gif in the board
/*
* Board2.java
*
* Created on November 29, 2006, 11:32 AM
*
*/
/**
*
* @author Bilal El Uneis
*/
import javax.swing.*;
public class Board2 extends Board
{
/** Creates a new instance of Board2 */
public Board2()
{
super(); //call the super contstructor
}
public void setPiece(Piece p, int x, int y)
{
JPanel panel = squares[x][y];
if(panel.getComponentCount()>0) panel.remove(0);
panel.add(p);
panel.revalidate();
}
public static void main(String args[])
{
Piece p = new Piece();
Board2 b2 = new Board2();
b2.setPiece(p,0,0);
}
}
what im trying to do is make it as simple as possilbe for any one to pick how to start with chessboard.. im not here to show u how to do it, im learning the easiest way to do it.. later on ill write engine in C/C++/Java and C# to use this board, so ill have to add sockets communication later on for the board..
suggestions :) appreciated.
peace
now i can move a piece on the board using remove and add methodes in board2.. here is my full code again. peace
/*
* Piece.java
*
* Created on November 29, 2006, 11:35 AM
*
*/
/**
*
* @author Bilal El Uneis
*/
import javax.swing.*;
public class Piece extends JLabel
{
/** Creates a new instance of Piece */
public Piece()
{
super(new ImageIcon("king.gif"));
}
}
/*
* Board.java
*
* Created on November 29, 2006, 11:04 AM
*
*/
/**
*
* @author Bilal El Uneis
*/
import java.awt.*;
import javax.swing.*;
public class Board
{
protected JPanel[][] squares;
protected JFrame boardFrame;
protected Container container;
/** Creates a new instance of Board */
public Board()
{
boardFrame = new JFrame();
boardFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
container = boardFrame.getContentPane();
container.setLayout(new GridLayout(8,8));
create_squares();
boardFrame.setSize(400,450);
boardFrame.setVisible(true);
}
private void create_squares()
{
squares = new JPanel[8][8];
for(int i=0;i<8;i++)
{
for(int j=0;j<8;j++)
{
JPanel p = new JPanel();
p.setBackground(setColor(i,j));
squares[i][j]=p;
container.add(p);
}
}
}
private Color setColor(int x, int y)
{
if((x+y)%2 == 0)
return Color.WHITE;
else
return Color.BLACK;
}
public static void main(String args[])
{
new Board();
}
}
/*
* Board2.java
*
* Created on November 29, 2006, 11:32 AM
*
*/
/**
*
* @author Bilal El Uneis
*/
import javax.swing.*;
public class Board2 extends Board
{
/** Creates a new instance of Board2 */
public Board2()
{
super(); //call the super contstructor
}
public void addPiece(Piece p, int x, int y)
{
JPanel panel = squares[x][y];
panel.add(p);
panel.repaint();
}
public void removePiece(Piece p, int x, int y)
{
JPanel panel = squares[x][y];
panel.remove(p);
panel.repaint();
}
public static void main(String args[])
{
Piece p = new Piece();
Board2 b2 = new Board2();
b2.addPiece(p,0,0);
JOptionPane.showInputDialog("put anything here");
b2.removePiece(p,0,0);
b2.addPiece(p,0,3);
}
}
here is my new code, now board2 can add and remove a piece so , i can move a pieace by removing then adding it to another coordinate.
/*
* Piece.java
*
* Created on November 29, 2006, 11:35 AM
*
*/
/**
*
* @author Bilal El Uneis
*/
import javax.swing.*;
public class Piece extends JLabel
{
/** Creates a new instance of Piece */
public Piece()
{
super(new ImageIcon("king.gif"));
}
}
/*
* Board.java
*
* Created on November 29, 2006, 11:04 AM
*
*/
/**
*
* @author Bilal El Uneis
*/
import java.awt.*;
import javax.swing.*;
public class Board
{
protected JPanel[][] squares;
protected JFrame boardFrame;
protected Container container;
/** Creates a new instance of Board */
public Board()
{
boardFrame = new JFrame();
boardFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
container = boardFrame.getContentPane();
container.setLayout(new GridLayout(8,8));
create_squares();
boardFrame.setSize(400,450);
boardFrame.setVisible(true);
}
private void create_squares()
{
squares = new JPanel[8][8];
for(int i=0;i<8;i++)
{
for(int j=0;j<8;j++)
{
JPanel p = new JPanel();
p.setBackground(setColor(i,j));
squares[i][j]=p;
container.add(p);
}
}
}
private Color setColor(int x, int y)
{
if((x+y)%2 == 0)
return Color.WHITE;
else
return Color.BLACK;
}
public static void main(String args[])
{
new Board();
}
}
/*
* Board2.java
*
* Created on November 29, 2006, 11:32 AM
*
*/
/**
*
* @author Bilal El Uneis
*/
import javax.swing.*;
public class Board2 extends Board
{
/** Creates a new instance of Board2 */
public Board2()
{
super(); //call the super contstructor
}
public void addPiece(Piece p, int x, int y)
{
JPanel panel = squares[x][y];
panel.add(p);
panel.repaint();
}
public void removePiece(Piece p, int x, int y)
{
JPanel panel = squares[x][y];
panel.remove(p);
panel.repaint();
}
public static void main(String args[])
{
Piece p = new Piece();
Board2 b2 = new Board2();
b2.addPiece(p,0,0);
JOptionPane.showInputDialog("put anything here");
b2.removePiece(p,0,0);
b2.addPiece(p,0,3);
}
}
it has being a while since i did any addition to my code or changes.. im trying to learn jgl library .. then i might actually redraw the whole board using jgl instead of using jpanels..
also jgl will allow my to use open gl calls that r implemented using pure java. so all u need it jdk to compile and jre to run nothing extra, as i can package the jgl with my program in one jar file :) .
look into jgl if u have time.. interesting stuff.
peace..
here is my code so far, the board shows with chess pieces showing .. when i move a pieace, the piece disapear from where it moved from but doesnt appear where it moved to.. i have minimize and then restore the window so that i can see the piece.. that means that java is redrawing the frame and that is why i see the piece now.
here is my code for now, did some changes.. any suggestions are very very appreciated..
peace
/*
* Piece.java
*
* Created on November 29, 2006, 11:35 AM
*
*/
/**
*
* @author Bilal El Uneis
*/
import javax.swing.*;
public class Piece extends JLabel
{
/** Creates a new instance of Piece */
public Piece()
{
super(new ImageIcon("Bking.gif"));
}
public Piece(String image_file)
{
super(new ImageIcon(image_file));
}
}
/*
* Board.java
*
* Created on November 29, 2006, 11:04 AM
*
*/
/**
*
* @author Bilal El Uneis
*/
import java.awt.*;
import javax.swing.*;
public class Board
{
protected JPanel[][] squares;
protected JFrame boardFrame;
protected Container container;
/** Creates a new instance of Board */
public Board()
{
boardFrame = new JFrame();
boardFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
boardFrame.setSize(800,700);
boardFrame.setResizable(false);
}
public void showBoard()
{
boardFrame.setVisible(true);
}
public void createSquares()
{
container = boardFrame.getContentPane();
container.setLayout(new GridLayout(8,8));
squares = new JPanel[8][8];
for(int i=0;i<8;i++)
{
for(int j=0;j<8;j++)
{
JPanel panel = new JPanel();
panel.setBackground(getColor(i,j));
container.add(panel);
squares[i][j] = panel;
}
}
}
private Color getColor(int x, int y)
{
if((x+y)%2 == 0)
return Color.WHITE;
else
return Color.BLACK;
}
public static void main(String args[])
{
Board t = new Board();
t.createSquares();
t.showBoard();
}
}
/*
* Board2.java
*
* Created on November 29, 2006, 11:32 AM
*
*/
/**
*
* @author Bilal El Uneis
*/
import javax.swing.*;
public class Board2 extends Board
{
/** Creates a new instance of Board2 */
public Board2()
{
super(); //call the super contstructor
}
public void addPiece(Piece p, int x, int y)
{
squares[x][y].add(p);
squares[x][y].repaint();
}
public void removePiece(int x, int y)
{
squares[x][y].remove(0);
squares[x][y].repaint();
}
public static void main(String args[])
{
Board2 t = new Board2();
t.createSquares();
//add pawns to board
for(int i=0; i<8; i++)
{
t.addPiece(new Piece("Rpawn.gif"),1,i);
t.addPiece(new Piece("Bpawn.gif"),6,i);
}
//add castles
t.addPiece(new Piece("Rrook.gif"),0,7);
t.addPiece(new Piece("Brook.gif"),7,7);
t.addPiece(new Piece("Rrook.gif"),0,0);
t.addPiece(new Piece("Brook.gif"),7,0);
//add horses
t.addPiece(new Piece("Rknight.gif"),0,6);
t.addPiece(new Piece("Bknight.gif"),7,6);
t.addPiece(new Piece("Rknight.gif"),0,1);
t.addPiece(new Piece("Bknight.gif"),7,1);
//add pishops
t.addPiece(new Piece("Rbishop.gif"),0,5);
t.addPiece(new Piece("Bbishop.gif"),7,5);
t.addPiece(new Piece("Rbishop.gif"),0,2);
t.addPiece(new Piece("Bbishop.gif"),7,2);
//add queen
t.addPiece(new Piece("Rqueen.gif"),0,4);
t.addPiece(new Piece("Bqueen.gif"),7,4);
//add king
t.addPiece(new Piece("Rking.gif"),0,3);
t.addPiece(new Piece("Bking.gif"),7,3);
//show board
t.showBoard();
//test moving one piece to see if i can
JOptionPane.showInputDialog("testing pawn move");
t.removePiece(1,0);
t.addPiece(new Piece("Rpawn.gif"),2,0);
}
}
once again i modyfied my solution .. made better use of inheritence and use JPanel instead of JFrame and then added panels to it.. now my code does update and move, as i used paintAll(graphics g) .. also i think my code looks more simple now, even a real biggener can follow. i also added main methode to Piece.java so that u can test it as a stand alone application, and see if it adds the chess piece .
so here it is .. peace
/*
* Piece.java
*
* Created on November 29, 2006, 11:35 AM
*
*/
/**
*
* @author Bilal El Uneis
*/
import javax.swing.*;
public class Piece extends JLabel
{
/** Creates a new instance of Piece */
public Piece()
{
super(new ImageIcon("Bking.gif"));
}
public Piece(String image_file)
{
super(new ImageIcon(image_file));
}
public static void main(String args[])
{
JFrame frame = new JFrame();
frame.setSize(200,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new Piece("Rking.gif"));
frame.setVisible(true);
}
}
/*
* Board.java
*
* Created on November 29, 2006, 11:04 AM
*
*/
/**
*
* @author Bilal El Uneis
*/
import java.awt.*;
import javax.swing.*;
public class Board extends JPanel
{
protected JPanel[][] squares;
/** Creates a new instance of Board */
public Board()
{
setSize(800,700);
setLayout(new GridLayout(8,8));
squares = new JPanel[8][8];
}
public Board(int h, int w)
{
setSize(h,w);
setLayout(new GridLayout(8,8));
squares = new JPanel[8][8];
}
public void createSquares()
{
for(int i=0;i<8;i++)
{
for(int j=0;j<8;j++)
{
JPanel panel = new JPanel();
panel.setBackground(getColor(i,j));
add(panel);
squares[i][j] = panel;
}
}
}
protected Color getColor(int x, int y)
{
if((x+y)%2 == 0)
return Color.WHITE;
else
return Color.BLACK;
}
public static void main(String args[])
{
Board t = new Board(800,700);
t.createSquares();
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800,700);
frame.setResizable(false);
frame.add(t);
frame.setVisible(true);
}
}
/*
* Board2.java
*
* Created on November 29, 2006, 11:32 AM
*
*/
/**
*
* @author Bilal El Uneis
*/
import javax.swing.*;
public class Board2 extends Board
{
/** Creates a new instance of Board2 */
public Board2()
{
super(); //call the super contstructor
}
public Board2(int h, int w)
{
super(h,w);
}
public void addPiece(Piece p, int x, int y)
{
squares[x][y].add(p);
paintAll(getGraphics());
}
public void removePiece(int x, int y)
{
squares[x][y].remove(0);
paintAll(getGraphics());
}
public static void main(String args[])
{
Board2 t = new Board2();
t.createSquares();
//add pawns to board
for(int i=0; i<8; i++)
{
t.addPiece(new Piece("Rpawn.gif"),1,i);
t.addPiece(new Piece("Bpawn.gif"),6,i);
}
//add castles
t.addPiece(new Piece("Rrook.gif"),0,7);
t.addPiece(new Piece("Brook.gif"),7,7);
t.addPiece(new Piece("Rrook.gif"),0,0);
t.addPiece(new Piece("Brook.gif"),7,0);
//add horses
t.addPiece(new Piece("Rknight.gif"),0,6);
t.addPiece(new Piece("Bknight.gif"),7,6);
t.addPiece(new Piece("Rknight.gif"),0,1);
t.addPiece(new Piece("Bknight.gif"),7,1);
//add pishops
t.addPiece(new Piece("Rbishop.gif"),0,5);
t.addPiece(new Piece("Bbishop.gif"),7,5);
t.addPiece(new Piece("Rbishop.gif"),0,2);
t.addPiece(new Piece("Bbishop.gif"),7,2);
//add queen
t.addPiece(new Piece("Rqueen.gif"),0,4);
t.addPiece(new Piece("Bqueen.gif"),7,4);
//add king
t.addPiece(new Piece("Rking.gif"),0,3);
t.addPiece(new Piece("Bking.gif"),7,3);
//create jframe and add the board to it :)
JFrame frame = new JFrame();
frame.setSize(800,700);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(t);
frame.setVisible(true);
//test moving one piece to see if i can
JOptionPane.showInputDialog("testing pawn move");
t.removePiece(1,0);
t.addPiece(new Piece("Rpawn.gif"),2,0);
}
}
im going to try and get the pice to interact with mouse .. so that i can move the piece on the board .. ill call the class MovablePiece, then ill create the board with movable pieces on one end and non movable on the other ..
im not using the best programming practises here.. im trying to keep code simple and easy to read.. then when u decide to create ur chess board u can add all exception handeling and error checkign stuff ..
i love to hear openions and suggestions :)
after im done with this ill try and create board using java2D then jgl .. google jgl to know what im talking about ..
peace..
ok while playing with code that should move a chess piece from on panel to another using mouse, i realized a better way to design the board.. im going to redew some things again :) .. this time ill make every panel that represent a sqaure in a class by it self and make each one aware of mouse interactions and give each a name using setName() methode..
peace ..
im trying to learn java3d too .. so lets see.. also i was just hired for perm position so i dont know if ill have time to play with code :), but i will try to find time for my hobby :D ..
peace
i was able to get feedback from the piece and the board.. i had to do some redesigning and stuff.. im reading about design patterns so that i can get an idea on how to make my code clean and make it more easy to modify without having to touch other classes..
ill post code later today or tomorrow ..
peace.
ok .. still trying to find time to play with my gui thing.. i got my board to basically give me info when my mouse is on piece or square.. also created a class called movablePiece that listen to mouse events too.
changes made to board and board2 disapears ..
here is the code ..
/*
* BoardSquare.java
*
* Created on December 8, 2006, 11:08 PM
*/
/**
*
* @author Bilal El Uneis
* alexbohemia@yahoo.com
*/
import java.awt.event.*;
import javax.swing.*;
public class BoardSquare extends JPanel implements MouseListener
{
public BoardSquare()
{
super();
addMouseListener(this);
}
public BoardSquare(String name)
{
super();
setName(name);
addMouseListener(this);
}
public void mouseClicked(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
System.out.println("wellcome to square " + getName());
}
public void mouseExited(MouseEvent e)
{
}
public static void main(String args[])
{
JFrame frame = new JFrame();
frame.setSize(200,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new BoardSquare("test square"));
frame.setVisible(true);
}
}
/*
* Board.java
*
* Created on November 29, 2006, 11:04 AM
*
*/
/**
*
* @author Bilal El Uneis
* alexbohemia@yahoo.com
*/
import java.awt.*;
import javax.swing.*;
public class Board extends JPanel
{
protected BoardSquare[][] squares;
/** Creates a new instance of Board */
public Board()
{
setSize(800,700);
setLayout(new GridLayout(8,8));
squares = new BoardSquare[8][8];
createSquares();
}
public Board(int h, int w)
{
setSize(h,w);
setLayout(new GridLayout(8,8));
squares = new BoardSquare[8][8];
createSquares();
}
private void createSquares()
{
for(int i=0;i<8;i++)
{
for(int j=0;j<8;j++)
{
BoardSquare panel = new BoardSquare(""+i+""+j);
panel.setBackground(getColor(i,j));
add(panel);
squares[i][j] = panel;
}
}
}
private Color getColor(int x, int y)
{
if((x+y)%2 == 0)
return Color.WHITE;
else
return Color.BLACK;
}
public void addPiece(Piece p, int x, int y)
{
squares[x][y].add(p);
paintAll(getGraphics());
}
public void removePiece(int x, int y)
{
if(squares[x][y].getComponentCount() > 0)
{
squares[x][y].remove(0);
paintAll(getGraphics());
}
}
public static void main(String args[])
{
Board t = new Board();
//add pawns to board
for(int i=0; i<8; i++)
{
t.addPiece(new Piece("Rpawn.gif"),1,i);
t.addPiece(new Piece("Bpawn.gif"),6,i);
}
//add castles
t.addPiece(new Piece("Rrook.gif"),0,7);
t.addPiece(new Piece("Brook.gif"),7,7);
t.addPiece(new MoveablePiece("Rrook.gif"),0,0);
t.addPiece(new Piece("Brook.gif"),7,0);
//add horses
t.addPiece(new Piece("Rknight.gif"),0,6);
t.addPiece(new Piece("Bknight.gif"),7,6);
t.addPiece(new Piece("Rknight.gif"),0,1);
t.addPiece(new Piece("Bknight.gif"),7,1);
//add pishops
t.addPiece(new Piece("Rbishop.gif"),0,5);
t.addPiece(new Piece("Bbishop.gif"),7,5);
t.addPiece(new Piece("Rbishop.gif"),0,2);
t.addPiece(new Piece("Bbishop.gif"),7,2);
//add queen
t.addPiece(new Piece("Rqueen.gif"),0,4);
t.addPiece(new Piece("Bqueen.gif"),7,4);
//add king
t.addPiece(new Piece("Rking.gif"),0,3);
t.addPiece(new Piece("Bking.gif"),7,3);
//create jframe and add the board to it :)
JFrame frame = new JFrame();
frame.setSize(800,700);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(t);
frame.setVisible(true);
//test moving one piece to see if i can
JOptionPane.showInputDialog("testing pawn move");
t.removePiece(1,0);
t.addPiece(new Piece("Rpawn.gif"),2,0);
}
}
/*
* Piece.java
*
* Created on November 29, 2006, 11:35 AM
*
*/
/**
*
* @author Bilal El Uneis
* alexbohemia@yahoo.com
*/
import javax.swing.*;
public class Piece extends JLabel
{
/** Creates a new instance of Piece */
public Piece()
{
super(new ImageIcon("Bking.gif"));
setName("Bking.gif");
}
public Piece(String image_file)
{
super(new ImageIcon(image_file));
setName(image_file);
}
public static void main(String args[])
{
JFrame frame = new JFrame();
frame.setSize(200,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Piece piece = new Piece("Rking.gif");
frame.add(piece);
frame.setVisible(true);
System.out.println(piece.getName());
}
}
/*
* MoveablePiece.java
*
* Created on December 8, 2006, 4:36 PM
*/
/**
*
* @author Bilal El Uneis
* alexbohemia@yahoo.com
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MoveablePiece extends Piece implements MouseListener
{
/** Creates a new instance of MoveablePiece */
public MoveablePiece()
{
super();
addMouseListener(this);
}
public MoveablePiece(String piece_name)
{
super(piece_name);
addMouseListener(this);
}
public void mouseClicked(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
System.out.println(getName() + " pressed..");
}
public void mouseReleased(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
System.out.println("u enterd " + getName());
System.out.println("Parent is " + e.getComponent().getParent().getName());
}
public void mouseExited(MouseEvent e)
{
}
public static void main(String args[])
{
MoveablePiece p = new MoveablePiece();
JPanel panel = new JPanel();
panel.setName("black panel");
panel.setSize(50,50);
panel.setBackground(Color.black);
JFrame frame = new JFrame();
frame.setName("frame");
frame.setLayout(new GridLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.add(p);
frame.add(panel);
frame.setVisible(true);
}
}
i know i can go with the easy way and just hardwire the code so that i can move the piece is square was clicked or piece.. but im trying to make every class able to function on its own as standalone.. that way u can use what ever class u want and not have to be tied for using everything because every class needs the other in a way that is hard to understand..
any suggestions will be greate, im just learning here :) . peace
Bilal El Uneis
> i know i can go with the easy way and just hardwire
> the code so that i can move the piece is square was
> clicked or piece.. but im trying to make every class
> able to function on its own as standalone.. that way
> u can use what ever class u want and not have to be
> tied for using everything because every class needs
> the other in a way that is hard to understand..
That sounds like a good idea.
Can I ask? Why do you have Piece and MovablePiece? Isn't every Piece movable?
How and where do you plan on defining the rules for each piece? Like you have the different icon for each piece but they all have different rules on where and how they can move.
im just developing gui for now .. in actual chess board u can place the piece in any square u want , u can move them how u want right? what makes u use these pieces and play with rules "chess" is ur logic ..
so im keeping the logic part seprat class that ill use for A.I to determind legal moves and so on.
a board an pieces are just that u can do what ever with them.. but when u say i want to play chess then u call upon ur logic to determine what chess rules r.
regarding piece and movable piece.. well maybe not the best design .. still thinking about it.. but lets say im playing against A.I .. then i want the A.I to control pieces that will not respond to mouse events. so they r only moved using addPiece and removePiece using program logic and r not aware of mouse events.
movablePiece r aware of mouse events and can register if they r clicked , draged and so on.. so they r ment for the user to move them instead of A.I.
Peace,
Bilal El Uneis
/*
* Piece.java
*
* Created on November 29, 2006, 11:35 AM
* piece that can be used to present chess or checkers or any board game piece
* uses jlabel and so best if used on jpanel
*/
/**
*
* @author Bilal M El Uneis
* alexbohemia@yahoo.com
*/
import javax.swing.*;
public class Piece extends JLabel
{
private boolean movable; //is piece movable by mouse
public Piece()
{
super(new ImageIcon("Bking.gif"));
setName("Bking.gif");
movable = false;
}
public Piece(String image_file)
{
super(new ImageIcon(image_file));
setName(image_file);
movable = false;
}
public Piece(String image_file, boolean m)
{
super(new ImageIcon(image_file));
setName(image_file);
movable = m;
}
public void setMovable(boolean m)
{
movable = m;
}
public boolean getIfMovable()
{
return movable;
}
}
/*
* BoardSquare.java
*
* Created on December 8, 2006, 11:08 PM
* a panel that can be used to represent a square on chess
* or checkers board, can add and remove piece
* which is a Jlable or Component.
*/
/**
*
* @author Bilal M El Uneis
* alexbohemia@yahoo.com
*/
import javax.swing.*;
public class BoardSquare extends JPanel
{
public BoardSquare()
{
super();
}
public BoardSquare(String name)
{
super();
setName(name);
}
public void addPiece(Piece p)
{
add(p);
paintAll(getGraphics());
}
public void removePiece()
{
if(getComponentCount() > 0)
{
remove(0);
paintAll(getGraphics());
}
}
}
/*
* MouseEventHandeler.java
*
* Created on December 17, 2006, 11:37 pm
* this class will recive mouse events from
* BoardSquare and Piece and deal with them
* in a lgical way :)
*/
/**
*
* @author Bilal M El Uneis
* alexbohemia@yahoo.com
*/
import java.awt.event.*;
public class MouseEventHandeler extends MouseAdapter
{
static int numOfClicks;
static String pieceName;
static BoardSquare bsOrigin;
static BoardSquare bsDist;
public MouseEventHandeler()
{
reset();
}
public void mouseClicked(MouseEvent e)
{
if(e.getSource() instanceof BoardSquare)
{
boardHandeler(e);
}
else if(e.getSource() instanceof Piece)
{
pieceHandeler(e);
}
}
private void boardHandeler(MouseEvent boardEvent)
{
if((pieceName != null) && (numOfClicks == 1))
{
bsDist = (BoardSquare)boardEvent.getComponent();
bsOrigin.removePiece();
bsDist.removePiece();
Piece p = new Piece(pieceName,true);
p.addMouseListener(this);
bsDist.addPiece(p);
reset();
}
}
private void pieceHandeler(MouseEvent pieceEvent)
{
Piece temp = (Piece)pieceEvent.getComponent();
if((pieceName == null) && temp.getIfMovable() && (numOfClicks == 0))
{
pieceName= temp.getName();
bsOrigin = (BoardSquare)temp.getParent();
numOfClicks = 1;
}
}
private void reset()
{
numOfClicks = 0;
bsDist = null;
bsOrigin = null;
pieceName = null;
}
}
/*
* Board.java
*
* Created on November 29, 2006, 11:04 AM
*
*/
/**
*
* @author Bilal El Uneis
* alexbohemia@yahoo.com
*/
import java.awt.*;
import javax.swing.*;
public class Board extends JPanel
{
protected static BoardSquare[][] squares;
/** Creates a new instance of Board */
public Board()
{
setSize(800,700);
setLayout(new GridLayout(8,8));
squares = new BoardSquare[8][8];
createSquares();
}
public Board(int h, int w)
{
setSize(h,w);
setLayout(new GridLayout(8,8));
squares = new BoardSquare[8][8];
createSquares();
}
private void createSquares()
{
for(int i=0;i<8;i++)
{
for(int j=0;j<8;j++)
{
BoardSquare panel = new BoardSquare(""+i+""+j);
panel.setBackground(getColor(i,j));
panel.addMouseListener(new MouseEventHandeler());
add(panel);
squares[i][j] = panel;
}
}
}
private Color getColor(int x, int y)
{
if((x+y)%2 == 0)
return Color.WHITE;
else
return Color.BLACK;
}
public static void main(String args[])
{
Board t = new Board();
//add pawns to board
for(int i=0; i<8; i++)
{
squares[1][i].addPiece(new Piece("Rpawn.gif"));
Piece p = new Piece("Bpawn.gif",true);
p.addMouseListener(new MouseEventHandeler());// movable pawns
squares[6][i].addPiece(p);
}
//add castles
squares[0][7].addPiece(new Piece("Rrook.gif"));
Piece p1 = new Piece("Brook.gif",true);
p1.addMouseListener(new MouseEventHandeler());
squares[7][7].addPiece(p1);
squares[0][0].addPiece(new Piece("Rrook.gif"));
Piece p2 = new Piece("Brook.gif",true);
p2.addMouseListener(new MouseEventHandeler());
squares[7][0].addPiece(p2);
//add horses
squares[0][6].addPiece(new Piece("Rknight.gif"));
Piece p3 = new Piece("Bknight.gif",true);
p3.addMouseListener(new MouseEventHandeler());
squares[7][6].addPiece(p3);
squares[0][1].addPiece(new Piece("Rknight.gif"));
Piece p4 = new Piece("Bknight.gif",true);
p4.addMouseListener(new MouseEventHandeler());
squares[7][1].addPiece(p4);
//add pishops
squares[0][5].addPiece(new Piece("Rbishop.gif"));
Piece p5 = new Piece("Bbishop.gif",true);
p5.addMouseListener(new MouseEventHandeler());
squares[7][5].addPiece(p5);
squares[0][2].addPiece(new Piece("Rbishop.gif"));
Piece p6 = new Piece("Bbishop.gif",true);
p6.addMouseListener(new MouseEventHandeler());
squares[7][2].addPiece(p6);
//add queen
squares[0][4].addPiece(new Piece("Rqueen.gif"));
Piece p7 = new Piece("Bqueen.gif",true);
p7.addMouseListener(new MouseEventHandeler());
squares[7][4].addPiece(p7);
//add king
squares[0][3].addPiece(new Piece("Rking.gif"));
Piece p8 = new Piece("Bking.gif",true);
p8.addMouseListener(new MouseEventHandeler());
squares[7][3].addPiece(p8);
//create jframe and add the board to it :)
JFrame frame = new JFrame();
frame.setSize(800,700);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(t);
frame.setVisible(true);
//test moving one piece to see if i can
JOptionPane.showInputDialog("testing pawn move");
squares[1][0].removePiece();
squares[2][0].addPiece(new Piece("Rpawn.gif"));
}
}
the first 3 classes , Piece/BoardSquare and MouseEventHandeler r what u need to really do any of the gui stuff for any board , chess or checkers .. board class is an example how those 3 classes r used .
in MouseEventHandeler i didnt need to handle case if a piece was clicked then another piece was clicked which means that a piece was taken , because the event handler checks for boardSquare event first before piece event.. so during my testing the handeler never really executed the part that deals with another piece getting clicked after initial click...
test it and comment .. i believe my code is simple..
board will show u that the board can be used by user and computer code to move pieces..
Bilal El Uneis
im making some changes to my classes, add some features .. also im using polymorphism with Piece so that i can create pawn, king and so on and later can pass Piece object to a methode that can use instanceof to findout what kind of piece it is , and use that to determind legal moves for that piece.
i need to figure algorithem to determind if game is over "check mate"
+ create class to determind valid moves. then ill create Dummy A.I that all it does is generate random moves but all legal and use that to debug then ill write decision tree and so on..
sounds like a time consuming stuff ha.
suggestions r wellcomed.
Peace
/*
* Piece.java
*
* Created on November 29, 2006, 11:35 AM
* piece that can be used to present chess or checkers or any board game piece
* uses jlabel and so best if used on jpanel
*/
/**
*
* @author Bilal M El Uneis
* alexbohemia@yahoo.com
*/
import javax.swing.*;
public class Piece extends JLabel
{
private boolean movable; //is piece movable by mouse
private char color; // piece color w or b
private int x; //x coordinate of 2D array
private int y; //y coord of 2D array
public Piece()
{
movable = false;
color = 'n';
x = -1;
y = -1;
}
public Piece(String image_file)
{
super(new ImageIcon(image_file));
setName(image_file);
movable = false;
resetXY();
}
public Piece(String image_file, boolean m)
{
super(new ImageIcon(image_file));
setName(image_file);
movable = m;
resetXY();
}
public Piece(String image_file, boolean m, char c)
{
this(image_file,m);
color = c;
movable = m;
resetXY();
}
public void resetXY()
{
x = -1;
y = -1;
}
public void setPieceImage(String image_file)
{
setIcon(new ImageIcon(image_file));
setName(image_file);
}
public void setMovable(boolean m)
{
movable = m;
}
public void setPieceColor(char c)
{
color = c;
}
public boolean getIfMovable()
{
return movable;
}
public char getPieceColor()
{
return color;
}
public int getXlocation()
{
return x;
}
public int getYlocation()
{
return y;
}
public void locatePieceXY()
{
String temp = getParent().getName(); // this will return somting like 01 so location is [0,1]
x = new Integer(temp.charAt(0));
y = new Integer(temp.charAt(1));
}
}
/*
* Pawn.java
*
* Created on December 24, 2006, 1:41 PM
*/
/**
*
* @author Bilal M El Uneis
* alexbohemia@yahoo.com
*/
public class Pawn extends Piece
{
public Pawn()
{
super();
}
public Pawn(char clr, boolean mvbl)
{
this();
if(clr == 'w' || clr == 'W')
{
setPieceImage("Rpawn.gif");
setMovable(mvbl);
setPieceColor(clr);
}
else //black pawn
{
setPieceImage("Bpawn.gif");
setMovable(mvbl);
setPieceColor(clr);
}
}
}
/*
* Rook.java
*
* Created on December 24, 2006, 2:18 PM
*/
/**
*
* @author Bilal M El Uneis
* alexbohemia@yahoo.com
*/
public class Rook extends Piece
{
public Rook()
{
super();
}
public Rook(char clr, boolean mvbl)
{
this();
if(clr == 'w' || clr == 'W')
{
setPieceImage("Rrook.gif");
setMovable(mvbl);
setPieceColor(clr);
}
else //black pawn
{
setPieceImage("Brook.gif");
setMovable(mvbl);
setPieceColor(clr);
}
}
}
/*
* Knight.java
*
* Created on December 24, 2006, 2:25 PM
*/
/**
*
* @author Bilal El Uneis
* alexbohemia@yahoo.com
*/
public class Knight extends Piece
{
public Knight()
{
super();
}
public Knight(char clr, boolean mvbl)
{
this();
if(clr == 'w' || clr == 'W')
{
setPieceImage("Rknight.gif");
setMovable(mvbl);
setPieceColor(clr);
}
else //black pawn
{
setPieceImage("Bknight.gif");
setMovable(mvbl);
setPieceColor(clr);
}
}
}
/*
* Bishop.java
*
* Created on December 24, 2006, 2:49 PM
*/
/**
*
* @author Bilal El Uneis
* alexbohemia@yahoo.com
*/
public class Bishop extends Piece
{
public Bishop()
{
super();
}
public Bishop(char clr, boolean mvbl)
{
this();
if(clr == 'w' || clr == 'W')
{
setPieceImage("Rbishop.gif");
setMovable(mvbl);
setPieceColor(clr);
}
else //black pawn
{
setPieceImage("Bbishop.gif");
setMovable(mvbl);
setPieceColor(clr);
}
}
}
/*
* King.java
* Created on December 24, 2006, 2:25 PM
*/
/**
*
* @author Bilal El Uneis
* alexbohemia@yahoo.com
*/
public class King extends Piece
{
public King()
{
super();
}
public King(char clr, boolean mvbl)
{
this();
if(clr == 'w' || clr == 'W')
{
setPieceImage("Rking.gif");
setMovable(mvbl);
setPieceColor(clr);
}
else //black pawn
{
setPieceImage("Bking.gif");
setMovable(mvbl);
setPieceColor(clr);
}
}
}
/*
* Queen.java
*
* Created on December 24, 2006, 2:56 PM
*
*/
/**
*
* @author Bilal M El Uneis
* alexbohemia@yahoo.com
*/
public class Queen extends Piece
{
/** Creates a new instance of Queen */
public Queen()
{
super();
}
public Queen(char clr, boolean mvbl)
{
this();
if(clr == 'w' || clr == 'W')
{
setPieceImage("Rqueen.gif");
setMovable(mvbl);
setPieceColor(clr);
}
else //black pawn
{
setPieceImage("Bqueen.gif");
setMovable(mvbl);
setPieceColor(clr);
}
}
}
/*
* BoardSquare.java
*
* Created on December 8, 2006, 11:08 PM
* a panel that can be used to represent a square on chess
* or checkers board, can add and remove piece
* which is a Jlable or Component.
*/
/**
*
* @author Bilal M El Uneis
* alexbohemia@yahoo.com
*/
import java.awt.*;
import javax.swing.*;
public class BoardSquare extends JPanel
{
public BoardSquare()
{
super();
}
public BoardSquare(String name)
{
this();
setName(name);
}
public void setSquareName(String name)
{
setName(name);
}
public void addPiece(Piece p)
{
add(p);
paintAll(getGraphics());
}
public void removePiece()
{
if(getComponentCount() > 0)
{
remove(0);
paintAll(getGraphics());
}
}
public void setSquareColor(Color c)
{
setBackground(c);
paintAll(getGraphics());
}
public boolean isEmpty()
{
if(getComponentCount() == 0)
return true;
else
return false;
}
public int getNumOfComponents()
{
return getComponentCount();
}
public Piece getPieceOnSquare()
{
if(getComponentCount() > 0)
return (Piece)getComponent(0);
else
return null;
}
}
/*
* MouseEventHandeler.java
*
* Created on December 17, 2006, 11:37 pm
* this class will recive mouse events from
* BoardSquare and Piece and deal with them
* in a lgical way :)
*/
/**
*
* @author Bilal M El Uneis
* alexbohemia@yahoo.com
*/
import java.awt.event.*;
public class MouseEventHandeler extends MouseAdapter
{
static int numOfClicks;
static String pieceName;
static BoardSquare bsOrigin;
static BoardSquare bsDist;
public MouseEventHandeler()
{
reset();
}
public void mouseClicked(MouseEvent e)
{
if(e.getSource() instanceof BoardSquare)
{
boardHandeler(e);
}
else if(e.getSource() instanceof Piece)
{
pieceHandeler(e);
}
}
private void boardHandeler(MouseEvent boardEvent)
{
if((pieceName != null) && (numOfClicks == 1))
{
bsDist = (BoardSquare)boardEvent.getComponent();
bsOrigin.removePiece();
bsDist.removePiece();
Piece p = new Piece(pieceName,true);
p.addMouseListener(this);
bsDist.addPiece(p);
reset();
}
}
private void pieceHandeler(MouseEvent pieceEvent)
{
Piece temp = (Piece)pieceEvent.getComponent();
if((pieceName == null) && temp.getIfMovable() && (numOfClicks == 0))
{
pieceName= temp.getName();
bsOrigin = (BoardSquare)temp.getParent();
numOfClicks = 1;
}
}
private void reset()
{
numOfClicks = 0;
bsDist = null;
bsOrigin = null;
pieceName = null;
}
}
/*
* Board.java
*
* Created on November 29, 2006, 11:04 AM
*
*/
/**
*
* @author Bilal El Uneis
* alexbohemia@yahoo.com
*/
import java.awt.*;
import javax.swing.*;
public class Board extends JPanel
{
protected static BoardSquare[][] squares;
/** Creates a new instance of Board */
public Board()
{
setSize(800,700);
setLayout(new GridLayout(8,8));
squares = new BoardSquare[8][8];
createSquares();
}
public Board(int h, int w)
{
setSize(h,w);
setLayout(new GridLayout(8,8));
squares = new BoardSquare[8][8];
createSquares();
}
private void createSquares()
{
for(int i=0;i<8;i++)
{
for(int j=0;j<8;j++)
{
BoardSquare panel = new BoardSquare(""+i+""+j);
panel.setSquareColor(getColor(i,j));
panel.addMouseListener(new MouseEventHandeler());
add(panel);
squares[i][j] = panel;
}
}
}
private Color getColor(int x, int y)
{
if((x+y)%2 == 0)
return Color.WHITE;
else
return Color.BLACK;
}
public static void main(String args[])
{
Board t = new Board();
//add pawns to board
for(int i=0; i<8; i++)
{
squares[1][i].addPiece(new Pawn('w',false));
Piece p = new Pawn('b',true);
p.addMouseListener(new MouseEventHandeler());// movable pawns
squares[6][i].addPiece(p);
}
//add castles
squares[0][7].addPiece(new Rook('w',false));
Piece p1 = new Rook('b',true);
p1.addMouseListener(new MouseEventHandeler());
squares[7][7].addPiece(p1);
squares[0][0].addPiece(new Rook('w',false));
Piece p2 = new Rook('b',true);
p2.addMouseListener(new MouseEventHandeler());
squares[7][0].addPiece(p2);
//add horses
squares[0][6].addPiece(new Knight('w',false));
Piece p3 = new Knight('b',true);
p3.addMouseListener(new MouseEventHandeler());
squares[7][6].addPiece(p3);
squares[0][1].addPiece(new Knight('w',false));
Piece p4 = new Knight('b',true);
p4.addMouseListener(new MouseEventHandeler());
squares[7][1].addPiece(p4);
//add pishops
squares[0][5].addPiece(new Bishop('w',false));
Piece p5 = new Bishop('b',true);
p5.addMouseListener(new MouseEventHandeler());
squares[7][5].addPiece(p5);
squares[0][2].addPiece(new Bishop('w',false));
Piece p6 = new Bishop('b',true);
p6.addMouseListener(new MouseEventHandeler());
squares[7][2].addPiece(p6);
//add queen
squares[0][4].addPiece(new Queen('w',false));
Piece p7 = new Queen('b',true);
p7.addMouseListener(new MouseEventHandeler());
squares[7][4].addPiece(p7);
//add king
squares[0][3].addPiece(new King('w',false));
Piece p8 = new King('b',true);
p8.addMouseListener(new MouseEventHandeler());
squares[7][3].addPiece(p8);
//create jframe and add the board to it :)
JFrame frame = new JFrame();
frame.setSize(800,700);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(t);
frame.setVisible(true);
//test moving one piece to see if i can
JOptionPane.showInputDialog("testing pawn move");
squares[1][0].removePiece();
squares[2][0].addPiece(new Piece("Rpawn.gif"));
}
}
/*
* MouseEventHandeler.java
*
* Created on December 17, 2006, 11:37 pm
* this class will recive mouse events from
* BoardSquare and Piece and deal with them
* in a lgical way :)
*/
/**
*
* @author Bilal M El Uneis
* alexbohemia@yahoo.com
*/
import java.awt.event.*;
public class MouseEventHandeler extends MouseAdapter
{
static int numOfClicks;
static String pieceName;
static BoardSquare bsOrigin;
static BoardSquare bsDist;
public MouseEventHandeler()
{
reset();
}
public void mouseClicked(MouseEvent e)
{
if(e.getSource() instanceof BoardSquare)
{
boardHandeler(e);
}
else if(e.getSource() instanceof Piece)
{
pieceHandeler(e);
}
}
private void boardHandeler(MouseEvent boardEvent)
{
if((pieceName != null) && (numOfClicks == 1))
{
bsDist = (BoardSquare)boardEvent.getComponent();
bsOrigin.removePiece();
bsDist.removePiece();
Piece p = new Piece(pieceName,true);
p.addMouseListener(this);
bsDist.addPiece(p);
reset();
}
}
private void pieceHandeler(MouseEvent pieceEvent)
{
/*
* the reset() is to prevent the case where
* a piece is clicked then the user decide to
* move another instead so the other piece
* is clicked, but what will happen is
* the older piece will move to the new
* square.
*/
reset();
Piece temp = (Piece)pieceEvent.getComponent();
if((pieceName == null) && temp.getIfMovable() && (numOfClicks == 0))
{
pieceName= temp.getName();
bsOrigin = (BoardSquare)temp.getParent();
numOfClicks = 1;
}
}
private void reset()
{
numOfClicks = 0;
bsDist = null;
bsOrigin = null;
pieceName = null;
}
}
to make things faster , i have decided for now to focus on computer against human.. so as far as i know this gui is working well for tht purpose, now ill focuse on a.i and vaild moves and checkmate detection parts..
commnets and modifications and suggestions r wellcomed :) .
also ur free to use my code in anything u find fit .. if u think it is fit for somthing of course ;)
