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