learning how to create chessboard in java

this is my attempt to learn how to create a chess board, ill share my code here and add more as i learn more… 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 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);
}

}

You declare a 2d array (squares = new JPanel[8][8])

and you try to access it as a 1d array (squares[j] = new JPanel())

You should access it like this:
squares[ i ][ j ] = new JPanel()

wow, interesting , i swear my code has squares[i][j] = new JPanel();

i dont understand why it pasted with 1 d array… let try this again.



/*
 * 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);
    }
    
}



if u try to copy and paste the code into text doc, then copy and paste it back here without using the insert code icon ull get the same result as what happend in my first post… weird!!

peace

This is weird, I’m sure I canceled that post, because I realized what happened…

it is because [ i ] is BBB code for < i > which causes an italic font-style

In between the code-tags, this styling is ignored, so it remains valid code there.

im trying to add chess piece to my board, then ill attempt to move the piece around , and after that i can create the whole board with pieces in it … and that is it … very simple… as for rules on what is legal and what is not that is up to the logic code or the engine, in fact i might modify my code later on, to be Board.java that shows how to create simple Board , then ChessBoard extends Board that will show how to add pieces and allow moving them on the board. so that others who want to learn how to create a board for any other reasons can use the code . :slight_smile:

ok, im trying to figure out how to add chess piece, but this is not working… here is my code now…



/*
 * 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);
    }
    
}



ok , i did some reseach , looked at more code from google :), here is what i decided to do…



/*
 * 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"));
    }
}


i then created a board, that does nothing but paint a board for you…


/*
 * 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();
    }
    
}

finally i created board2 to use board and has a feature of adding pieces to it.


/*
 * 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);
    }
    
}


suggestions and comments appreciated

ok, now board2 can add, remove piece, so to move a pieace from one location to another u just call remove , then add to the new location.

im not sure i changed anything in board or piece , but here is the complete code again.


/*
 * 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);
    }
    
}


setColor should probebly be getColor as it gives a color based on the arguments and sets nothing.

I think you should be looking in using the command pattern for moving pieces. That way it’s easier to apply rules and probebly cleaner and a bunch of other advantages.

some other note, try avoiding differend codingstyles in one project, bla_bla vs blaBla I don’t know any java programmers who do use bla_bla but if it works for you who am I to disagree.

i havent looked or did anything extra on my code, im trying to learn jgl more “java gl using pure java” and also hacking it to understand how it is implemted and im using UDoc to get uml like diagrams from the jgl package , to understand it more. i will come back to my chess program “gui at this point” and maybe redo it using jgl and draw everything in 3D. but again this thread is my attempt at that and all i post here is my learning while im doing things …

please be specific about what bla_bla vs blaBla means?? where in my code ur exactly pointing?

again, any suggestions and advices r very wellcome… i learn from everyone. :slight_smile:

peace

ok … here is my new code, i ca now add all pieces to the chess board and show the board with those pieces, i can remove a piece and move it… but it only removes and i need to minimize then maximize the window to so that i will show the new piece moved… hmm., i need to look into that.

i used some nice chess pieces found on the internet and … my board looks nice. but again, i need to fix this drawing thing, then i can start writing mouse handling stuff in another class…

suggestions are very very wellcomed :slight_smile: and appriciated.

here is my code so far.

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 :slight_smile: .

so here it is :slight_smile: … 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);
    }
    
}


you don’t need to repaint, you are adding new components to an layout and since new components are added the container needs to layout again, try calling invalidate. I’m still not sure where your going with the whole board2 bit.

I see you already altered the create_squares() bit to createSquares().

im going to continue sharing my code on java forum at java.sun.com … under game development section… as it is easier to find that code if u google :slight_smile:

peace… i got the piece to move and repaint, now im trying to use mouse interaction for the move.

peace

here is my code so far… i love to hear from u guys… peace, Bilal El Uneis



/*
 * 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);
    }
    
}





there are processXxxEvent() 's on the Components you need to register ops if you want to couple your listeners with your components pending on what you want them to do you might want to externalise (use a separate class) for the listeners.

also you need to evaluate upon the board and it turning into a farcade/mediatior for BoardSquares, pieces and the likes.

the coupling of UI / View with Model and Controll/Logic should perhaps take attention

these design decitions are bound to arguments like scope and intent of the program, which I know little about.

ok im trying to understand what u mean… do u mean that i need to create a sperate class for event handeling and have a methode that determinds where the event came from and take action according to that ?

im looking into my design AGAIN. and i can see somthing interesting … im using board squars as object in board, that means that board squares is stand alone, but board requaires boardsquare as part of it , it is declared as an object withen the class. but if i use inheretince instead of createing object , then the code may look cleaner, and easy transition to see what new features where added to class board.

im going to look again into those issues, i got the even handelign and all i need to know to get this working, but im looking into best design that is easy to follow and resue and maintain or modify by others who want to create their own chess board or checkers board or what ever board.

i enjoy ur openions :slight_smile: … thanks…

Bilal El Uneis

please keep checking my thread at java.sun.com forum… just search my name Bilal El Uneis :slight_smile: . i have an update there and ill keep my new code there .

peace …