Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  learning how to create chessboard in java  (Read 6077 times)
0 Members and 1 Guest are viewing this topic.
Offline Bilal

Senior Newbie





« Posted 2006-11-27 05:48:53 »

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[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);
    }
   
}
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 783
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2006-11-27 08:00:36 »

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()

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Bilal

Senior Newbie





« Reply #2 - Posted 2006-11-27 14:24:38 »

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

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
/*
 * 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);
    }
   
}
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Bilal

Senior Newbie





« Reply #3 - Posted 2006-11-27 14:26:50 »

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
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 783
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #4 - Posted 2006-11-27 14:29:59 »

This is weird, I'm sure I canceled that post, because I realized what happened....

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 783
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2006-11-27 14:31:54 »

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.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Bilal

Senior Newbie





« Reply #6 - Posted 2006-11-27 15:00:12 »

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 . Smiley

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
/*
 * 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);
    }
   
}
Offline Bilal

Senior Newbie





« Reply #7 - Posted 2006-11-29 18:23:55 »

ok , i did some reseach , looked at more code from google Smiley, here is what i decided to do..

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
/*
 * 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..

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
/*
 * 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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
/*
 * 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
Offline Bilal

Senior Newbie





« Reply #8 - Posted 2006-11-30 22:26:45 »

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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
/*
 * 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"));
    }
   
}



1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
/*
 * 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();
    }
   
}



1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
/*
 * 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);
    }
   
}
Offline Mr_Light

Senior Member




shiny.


« Reply #9 - Posted 2006-12-04 07:43:31 »

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.


It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Bilal

Senior Newbie





« Reply #10 - Posted 2006-12-04 18:42:54 »

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. Smiley

peace
Offline Bilal

Senior Newbie





« Reply #11 - Posted 2006-12-05 10:11:53 »

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 Smiley and appriciated.

here is my code so far.

peace
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
/*
 * 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));
    }
   
}



1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
/*
 * 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();
    }
   
}




1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
/*
 * 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);
    }
   
}
Offline Bilal

Senior Newbie





« Reply #12 - Posted 2006-12-06 01:48:51 »

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 Smiley .

so here it is Smiley .. peace

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
/*
 * 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);
    }
   
}


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
/*
 * 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);
    }
   
}



1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
/*
 * 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);
    }
   
}
Offline Mr_Light

Senior Member




shiny.


« Reply #13 - Posted 2006-12-06 21:13:24 »

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().

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Offline Bilal

Senior Newbie





« Reply #14 - Posted 2006-12-08 16:58:49 »

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 Smiley ..

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

peace
Offline Bilal

Senior Newbie





« Reply #15 - Posted 2006-12-16 02:44:57 »

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
226  
227  
228  
229  
230  
231  
232  
233  
234  
235  
236  
237  
238  
239  
240  
241  
242  
243  
244  
245  
246  
247  
248  
249  
250  
251  
252  
253  
254  
255  
256  
257  
258  
259  
260  
261  
262  
263  
264  
265  
266  
267  
268  
269  
270  
271  
272  
273  
274  
275  
276  
277  
278  
279  
280  
281  
282  
283  
284  
285  
286  
287  
288  
289  
290  
291  
292  
293  
294  
295  
296  
297  
298  
299  
300  
301  
302  
303  
304  
305  
306  
307  
/*
 * 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);
    }
   
}
Offline Mr_Light

Senior Member




shiny.


« Reply #16 - Posted 2006-12-17 01:39:12 »

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.

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Offline Bilal

Senior Newbie





« Reply #17 - Posted 2006-12-17 18:33:24 »

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 Smiley .. thanks..

Bilal El Uneis
Offline Bilal

Senior Newbie





« Reply #18 - Posted 2006-12-20 16:28:49 »

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

peace ..
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (16 views)
2014-08-28 18:26:30

CopyableCougar4 (25 views)
2014-08-22 19:31:30

atombrot (38 views)
2014-08-19 09:29:53

Tekkerue (35 views)
2014-08-16 06:45:27

Tekkerue (32 views)
2014-08-16 06:22:17

Tekkerue (20 views)
2014-08-16 06:20:21

Tekkerue (31 views)
2014-08-16 06:12:11

Rayexar (66 views)
2014-08-11 02:49:23

BurntPizza (44 views)
2014-08-09 21:09:32

BurntPizza (34 views)
2014-08-08 02:01:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!