Hi,

I am trying to develop a chess game using textual interface. I am using console read to capture moves and update the board accordingly. I am unable to remove Jlabel from the JPanel, i have called sthng lyk panel.remoove(label).......Can any one help me to sort it out plz or give a better idea.

here's my code,

public class Board_tyr extends JFrame{

    JFrame frame;

    static int [] board = new int [64];

static JPanel squares[][] = new JPanel[8][8];

    //the images
    static JLabel [] pieces = new JLabel [18];

    static int s_y;
    static int d_y;
    static int start_index;
    static int des_index;


    public Board_tyr(){

        frame = new JFrame("Simplified Chess");
        frame.setSize(500, 500);
        frame.setLayout(new GridLayout(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.blue);
                } else {
                    squares[i][j].setBackground(Color.white);
                }   
                frame.add(squares[i][j]);
            }
        }


        pieces [0] =  new JLabel("R");
        pieces [1] =  new JLabel("N");
        pieces [2] =  new JLabel("B");
        pieces [3] =  new JLabel("Q");
        pieces [4] =  new JLabel("K");
        pieces [5] =  new JLabel("B");
        pieces [6] =  new JLabel("N");
        pieces [7] =  new JLabel("R");

        pieces [9] =  new JLabel("r");
        pieces [10] =  new JLabel("n");
        pieces [11] =  new JLabel("b");
        pieces [12] =  new JLabel("q");
        pieces [13] =  new JLabel("k");
        pieces [14] =  new JLabel("b");
        pieces [15] =  new JLabel("n");
        pieces [16] =  new JLabel("r");

        pieces [8] =  new JLabel("P");
        pieces [17] =  new JLabel("p");


        squares[0][0].add(pieces [0]);
        squares[0][1].add(pieces [1]);
        squares[0][2].add(pieces [2]);
        squares[0][3].add(pieces [3]);
        squares[0][4].add(pieces [4]);
        squares[0][5].add(pieces [5]);
        squares[0][6].add(pieces [6]);
        squares[0][7].add(pieces [7]);

        squares[7][0].add(pieces [9]);
        squares[7][1].add(pieces [10]);
        squares[7][2].add(pieces [11]);
        squares[7][3].add(pieces [12]);
        squares[7][4].add(pieces [13]);
        squares[7][5].add(pieces [14]);
        squares[7][6].add(pieces [15]);
        squares[7][7].add(pieces [16]);


        for (int i = 0; i < 8; i++) {
    //        squares[1][i].add(pieces[8]);
      //     squares[6][i].add(pieces[17]);
              squares[1][i].add(new JLabel("P"));
              squares[6][i].add(new JLabel("p"));


        }

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

    }


    public static void test(){
        String name; 
        while(true){
            System.out.print("Enter the name");

            Scanner in = new Scanner(System.in);
            // Reads a single line from the console 
            // and stores into name variable
            name = in.nextLine();
            char b = name.charAt(0);
            char start_y = name.charAt(1);
            char s_2 = name.charAt(2);
            int start_x=Character.digit(s_2, 10);

            char des_y = name.charAt(3);
            char d_2 = name.charAt(4);
            int des_x=Character.digit(d_2, 10);

            char a = 'a';
            char B = 'b';
            char c = 'c';
            char d = 'd';
            char e = 'e';
            char f = 'f';
            char g = 'g';
            char h = 'h';
            // System.out.println(b);
            //  String token = name;
            //   char[] tokens = token.split("[A-Za-z]");
            //   System.err.print(tokens[1]);
            if(start_y==a){
                s_y = 0;
            }

            if(start_y==B){
                s_y = 1;
            }

            if(start_y==c){
                s_y = 2;
            }

            if(start_y==d){
                s_y = 3;
            }

            if(start_y==e){
                s_y = 4;
            }

            if(start_y==f){
                s_y = 5;
            }

            if(start_y==g){
                s_y = 6;
            }

            if(start_y==h){
                s_y = 7;
            }

            if(des_y==a){
                d_y = 0;
            }

            if(des_y==B){
                d_y = 1;
            }

            if(des_y==c){
                d_y = 2;
            }

            if(des_y==d){
                d_y = 3;
            }

            if(des_y==e){
                d_y = 4;
            }

            if(des_y==f){
                d_y = 5;
            }

            if(des_y==g){
                d_y = 6;
            }

            if(des_y==h){               
                d_y = 7;
            }


            start_index =  Math.abs(8*s_y+start_x);
            des_index = Math.abs(8*d_y+des_x);


            int [] org = {
                       0, 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,};


    for(int i=0;i<64;i++){
                board[i] = org[i];
            }
            for(int i=0;i<64;i++){

                if(start_index==(board[i])){

//System.err.print(start_index+","+board[i]);
            squares[start_x][s_y].remove(pieces[i]);
//                  pieces[i].setVisible(false);
                //  squares[1][0].remove(pieces[7]);

                    squares[des_x][d_y].add(pieces[7]);
                    /*      panel = (JPanel)chessBoard.getComponent(start_index);

                panel;

}
                    piece = new JLabel("b");            
                panel = (JPanel)chessBoard.getComponent(des_index);
                piece.setHorizontalAlignment(piece.CENTER);
                panel.add(piece);*/

            }
            }
        }       
    }

        }       
    }




public static void main(String[] args) {
        new Board_tyr();
        test();
    }
}

What is the purpose of removing the label? Do you want all the components to shift to fill in the hole where the component has been removed?

Please use code tags around posted code to make it more readable. Use icon above to right. Info here: http://www.java-forums.org/misc.php?do=bbcode#code

Please fix your code so it will compile

Hi,

I am develooping a human vs computer chess game. I have to update the board after each move. Thats the reason i am removing the label from one position of the board and adding it to the other. It somehow doesn't work.

The posted code does NOT compile.
Please use code tags around posted code to make it more readable. Use icon above to right. Info here: http://www.java-forums.org/misc.php?do=bbcode#code

Instead of removing the labels and having to mess with a layout, why not change the label's contents?

Hi,

I am trying to remove piece(JLabel) from the JPanel(Squares[][]) but it doesnt removes it. It just adds. Can anyone provide any suggestions plz.....

for(int k=0;k<12;k++){
    String z =pieces[k].getText();
    char c1 = z.charAt(0);
        if(c1==piece){

            squares[s_y][start_x].remove(pieces[k]);
            pieces[k].setVisible(false);


            squares[d_y][des_x].add(pieces[k]);
        pieces[k].setVisible(true);

Please use code tags around posted code to make it more readable.

Without code that will compile and execute, it is hard to recommend anything.

Hi,

This is my code that compiles. It has 2 classes....Board_tyr and Move...

import java.awt.*;
import java.util.Scanner;

import javax.swing.*;

public class Board_tyr extends JFrame{

    JFrame frame;

    static int [] board = new int [64];

static JPanel squares[][] = new JPanel[8][8];


    //the images
    static JLabel [] pieces = new JLabel [64];

    static int s_y;
    static int d_y;
    static int start_index;
    static int des_index;
    static int des_x;
    static int start_x;
    static char piece;

    public Board_tyr(){

        frame = new JFrame("Simplified Chess");
        frame.setSize(500, 500);
        frame.setLayout(new GridLayout(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.blue);
                } else {
                    squares[i][j].setBackground(Color.white);
                }   
                frame.add(squares[i][j]);
            }
        }


        pieces [0] =  new JLabel("R");
        pieces [1] =  new JLabel("N");
        pieces [2] =  new JLabel("B");
        pieces [3] =  new JLabel("Q");
        pieces [4] =  new JLabel("K");
    //    pieces [5] =  new JLabel("B");
     //   pieces [6] =  new JLabel("N");
      //  pieces [7] =  new JLabel("R");

        pieces [6] =  new JLabel("r");
        pieces [7] =  new JLabel("n");
        pieces [8] =  new JLabel("b");
        pieces [9] =  new JLabel("q");
        pieces [10] =  new JLabel("k");
     //   pieces [14] =  new JLabel("b");
     //   pieces [15] =  new JLabel("n");
     //   pieces [16] =  new JLabel("r");

        pieces [5] =  new JLabel("P");
    /*    pieces [9] =  new JLabel("P");
        pieces [10] =  new JLabel("P");
        pieces [11] =  new JLabel("P");
        pieces [12] =  new JLabel("P");
        pieces [13] =  new JLabel("P");
        pieces [14] =  new JLabel("P");
        pieces [15] =  new JLabel("P");*/

        pieces [11] =  new JLabel("p");
/*      pieces [49] =  new JLabel("p");
        pieces [50] =  new JLabel("p");
        pieces [51] =  new JLabel("p");
        pieces [52] =  new JLabel("p");
        pieces [53] =  new JLabel("p");
        pieces [54] =  new JLabel("p");
        pieces [55] =  new JLabel("p");*/


        squares[0][0].add(new JLabel("R"));
        squares[0][1].add(new JLabel("N"));
        squares[0][2].add(new JLabel("B"));
        squares[0][3].add(new JLabel("Q"));
        squares[0][4].add(new JLabel("K"));
        squares[0][5].add(new JLabel("B"));
        squares[0][6].add(new JLabel("N"));
        squares[0][7].add(new JLabel("R"));

        squares[7][0].add(new JLabel("r"));
        squares[7][1].add(new JLabel("n"));
        squares[7][2].add(new JLabel("b"));
        squares[7][3].add(new JLabel("q"));
        squares[7][4].add(new JLabel("k"));
        squares[7][5].add(new JLabel("b"));
        squares[7][6].add(new JLabel("n"));
        squares[7][7].add(new JLabel("r"));


        for (int i = 0; i < 8; i++) {
         squares[1][i].add(new JLabel("P"));
         squares[6][i].add(new JLabel("p"));
          //    squares[1][i].add(new JLabel("P"));
            //  squares[6][i].add(new JLabel("p"));


        }

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

    }


    public static void test(){
        String name; 

        int [] org = {
                   0, 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,};


        for(int i=0;i<64;i++){
            board[i] = org[i];
        }

        while(true){
            System.out.print("Enter the name");

            Scanner in = new Scanner(System.in);
            // Reads a single line from the console
            // and stores into name variable
            name = in.nextLine();
            piece = name.charAt(0);
            char start_y = name.charAt(1);
            char s_2 = name.charAt(2);
            start_x=Character.digit(s_2, 10);

            char des_y = name.charAt(3);
            char d_2 = name.charAt(4);
            des_x=Character.digit(d_2, 10);

            char a = 'a';
            char b = 'b';
            char c = 'c';
            char d = 'd';
            char e = 'e';
            char f = 'f';
            char g = 'g';
            char h = 'h';
            // System.out.println(b);
            //  String token = name;
            //   char[] tokens = token.split("[A-Za-z]");
            //   System.err.print(tokens[1]);                   
            if(start_y==a){
                s_y = 0;
            }

            if(start_y==b){
                s_y = 1;
            }

            if(start_y==c){
                s_y = 2;
            }

            if(start_y==d){
                s_y = 3;
            }

            if(start_y==e){
                s_y = 4;
            }

            if(start_y==f){
                s_y = 5;
            }

            if(start_y==g){
                s_y = 6;
            }

            if(start_y==h){
                s_y = 7;
            }

            if(des_y==a){
                d_y = 0;
            }

            if(des_y==b){
                d_y = 1;
            }

            if(des_y==c){
                d_y = 2;
            }

            if(des_y==d){
                d_y = 3;
            }

            if(des_y==e){
                d_y = 4;
            }

            if(des_y==f){
                d_y = 5;
            }

            if(des_y==g){
                d_y = 6;
            }

            if(des_y==h){               
                d_y = 7;
            }


            start_index =  Math.abs(8*s_y+start_x);
            des_index = Math.abs(8*d_y+des_x);

            Move m = new Move(start_index,des_index);
    //      int a_move =  start_index*100+des_index;
            int s=m.move();

            update(s);
        }           

    }

    public static void update(int m){

        System.err.print("ye hai asal obj"+" "+m);

        for(int i=0;i<64;i++){

            if(m/100==(board[i])){



//System.err.print(start_index+","+board[i]);


        for(int k=0;k<12;k++){
        String z =pieces[k].getText();
        char c1 = z.charAt(0);
            if(c1==piece){

                squares[s_y][start_x].remove(pieces[k]);
                pieces[k].setVisible(false);


                squares[d_y][des_x].add(pieces[k]);
            pieces[k].setVisible(true);

            }
        }
            }
            }   
    }





    public static void main(String[] args) {
        new Board_tyr();
        test();
    }
}





public class Move {

    //Board_tyr bb = new Board_tyr();


    int start_ind;
    int end_ind;


    public Move(int start,int end){

        start_ind=start;
        end_ind=end;        
    }

    public int move(){

        int a_move =  start_ind*100+end_ind;

        return a_move;

    }

}

plz help asap....

You keep missing this:
Please use code tags around posted code to make it more readable.

Hi,

Can you atleast tell me a way to remove Jlabel from Jpanel. This my help me out to sort out the problem.

Thnx

I would use the container's remove method and then tell the container's parent that there has been a change.

Thnx.....I wil try 2 implment mine with the container.

Panel.remove(label) ...Isn't this a good approach??

Did you read the API doc for the remove method? It has recommendations.

Well...Let me see that.

Thnx 4 d help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.