the chessBoard Blacks Perspective I'm making a chess GUI in Java Swing and I have two JPanel classes that have the chess board in both white (A-H at the bottom) and blacks (H-A at the bottom) perspective; however, after white makes a move by first clicking on the the first JButton that has the image of the piece that he's wanting to move and then clicking a second time on a new JButton to move the piece to that second JButton, I'm trying to call the new JPanel that has blacks perspective but all of my JButtons are disappearing. Is there a way to fix this? Is there a way that I can keep my JButtons and only change the content pane? Is it a layering issue?
public class Chess implements ActionListener{
JButton[][] theChessBoard;
static JFrame frame;
Pieces piecesBoard[][];
boolean firstClick;
boolean secondClick;
Pieces temp;
Position p;
int row;
int col;
static boolean blackMove;
public Chess() throws IOException {
temp = null;
firstClick = false;
//creates my JButtons and locates them so that go on top of my chessBoard background correctly
theChessBoard = new JButton[8][8];
for(int i = 0; i < 8; i++){
for(int j = 0; j < 8; j++){
theChessBoard[i][j] = new JButton();
frame.add(theChessBoard[i][j]);
theChessBoard[i][j].setBounds(j*74+30,i*77+30,60,60);
theChessBoard[i][j].addActionListener(this);
theChessBoard[i][j].setContentAreaFilled(false);
theChessBoard[i][j].setBorder(null);
}
}
/*a corresponding array so you can move the pieces in this array and then populate the jbuttons
* using the updateBoard and flipBoard method
*/
piecesBoard = new Pieces[8][8];
for(int a = 0; a < 8; a++){
for(int b = 0; b < 8; b++){
piecesBoard[a][b] = new Spaces();
}
}
public static void main(String[] args) throws IOException{
blackMove = true;
frame = new JFrame("The Chess Game");
frame.setContentPane(new ChessBoardWhitePerspective());
frame.setLayout(null);
frame.setSize(651,700);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
new Chess();
}
public void flipBoard() throws IOException{
if(blackMove){
blackMove = false;
// wanting to flip the board back to the other JPanel that holds whites Perspective
}else {
blackMove =true;
// wanting to flip the board back to the other JPanel that holds blacks Perspective
}
Pieces[][] temp = new Pieces[8][8];
for(int i = 0; i < 8; i++){
for(int j = 0; j<8; j++){
temp[i][j] = piecesBoard[7-i][7-j];
}
}
piecesBoard = temp;
updateBoard();
}
this is the JPanel code:
public class ChessBoardWhitePerspective extends JPanel{
BufferedImage chessBoardImage;
public ChessBoardWhitePerspective() throws IOException{
String ChessBoard = "chessBoard.jpg";
File ChessBoardFile = new File(ChessBoard);
chessBoardImage = ImageIO.read(ChessBoardFile);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(chessBoardImage, 0, 0, 1007, 660, this); // 0,0,1007,660,this for the standard chess picture(chessBoard.jpg) -405,-265,1495,155,this for chessBoardBlack.jpg
}