once again i modyfied my solution … made better use of inheritence and use JPanel instead of JFrame and then added panels to it… now my code does update and move, as i used paintAll(graphics g) … also i think my code looks more simple now, even a real biggener can follow. i also added main methode to Piece.java so that u can test it as a stand alone application, and see if it adds the chess piece .
so here it is … peace
/*
* Piece.java
*
* Created on November 29, 2006, 11:35 AM
*
*/
/**
*
* @author Bilal El Uneis
*/
import javax.swing.*;
public class Piece extends JLabel
{
/** Creates a new instance of Piece */
public Piece()
{
super(new ImageIcon("Bking.gif"));
}
public Piece(String image_file)
{
super(new ImageIcon(image_file));
}
public static void main(String args[])
{
JFrame frame = new JFrame();
frame.setSize(200,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new Piece("Rking.gif"));
frame.setVisible(true);
}
}
/*
* Board.java
*
* Created on November 29, 2006, 11:04 AM
*
*/
/**
*
* @author Bilal El Uneis
*/
import java.awt.*;
import javax.swing.*;
public class Board extends JPanel
{
protected JPanel[][] squares;
/** Creates a new instance of Board */
public Board()
{
setSize(800,700);
setLayout(new GridLayout(8,8));
squares = new JPanel[8][8];
}
public Board(int h, int w)
{
setSize(h,w);
setLayout(new GridLayout(8,8));
squares = new JPanel[8][8];
}
public void createSquares()
{
for(int i=0;i<8;i++)
{
for(int j=0;j<8;j++)
{
JPanel panel = new JPanel();
panel.setBackground(getColor(i,j));
add(panel);
squares[i][j] = panel;
}
}
}
protected Color getColor(int x, int y)
{
if((x+y)%2 == 0)
return Color.WHITE;
else
return Color.BLACK;
}
public static void main(String args[])
{
Board t = new Board(800,700);
t.createSquares();
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800,700);
frame.setResizable(false);
frame.add(t);
frame.setVisible(true);
}
}
/*
* Board2.java
*
* Created on November 29, 2006, 11:32 AM
*
*/
/**
*
* @author Bilal El Uneis
*/
import javax.swing.*;
public class Board2 extends Board
{
/** Creates a new instance of Board2 */
public Board2()
{
super(); //call the super contstructor
}
public Board2(int h, int w)
{
super(h,w);
}
public void addPiece(Piece p, int x, int y)
{
squares[x][y].add(p);
paintAll(getGraphics());
}
public void removePiece(int x, int y)
{
squares[x][y].remove(0);
paintAll(getGraphics());
}
public static void main(String args[])
{
Board2 t = new Board2();
t.createSquares();
//add pawns to board
for(int i=0; i<8; i++)
{
t.addPiece(new Piece("Rpawn.gif"),1,i);
t.addPiece(new Piece("Bpawn.gif"),6,i);
}
//add castles
t.addPiece(new Piece("Rrook.gif"),0,7);
t.addPiece(new Piece("Brook.gif"),7,7);
t.addPiece(new Piece("Rrook.gif"),0,0);
t.addPiece(new Piece("Brook.gif"),7,0);
//add horses
t.addPiece(new Piece("Rknight.gif"),0,6);
t.addPiece(new Piece("Bknight.gif"),7,6);
t.addPiece(new Piece("Rknight.gif"),0,1);
t.addPiece(new Piece("Bknight.gif"),7,1);
//add pishops
t.addPiece(new Piece("Rbishop.gif"),0,5);
t.addPiece(new Piece("Bbishop.gif"),7,5);
t.addPiece(new Piece("Rbishop.gif"),0,2);
t.addPiece(new Piece("Bbishop.gif"),7,2);
//add queen
t.addPiece(new Piece("Rqueen.gif"),0,4);
t.addPiece(new Piece("Bqueen.gif"),7,4);
//add king
t.addPiece(new Piece("Rking.gif"),0,3);
t.addPiece(new Piece("Bking.gif"),7,3);
//create jframe and add the board to it :)
JFrame frame = new JFrame();
frame.setSize(800,700);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(t);
frame.setVisible(true);
//test moving one piece to see if i can
JOptionPane.showInputDialog("testing pawn move");
t.removePiece(1,0);
t.addPiece(new Piece("Rpawn.gif"),2,0);
}
}