0

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
        }
  • Are you saying you have 2 boards and you want to switch them after every move ? – UDKOX May 22 '16 at 19:21
  • edit your post, dont put that code in comments. Also, put the corresponding code on the post, not all your code. – UDKOX May 22 '16 at 19:22
  • yeah that's the idea, I have two images of the same board in two different JPanels for both black and white when its their move and i'm trying to call them when its black and whites turn – thatProgrammingGuy May 22 '16 at 19:27
  • Why don't you just have 1 board and rotate it ? – UDKOX May 22 '16 at 19:59
  • because rotating it would just make the board be upside down instead of what i was wanting in the link where for blacks perspective has h-a at the bottom and for white it has a-h.. when i call the flip board method, whichever colors turn it is has their pieces at the bottom of the board – thatProgrammingGuy May 22 '16 at 21:41
  • @theProgrammingGuy Man, I don't know if you ever plaied chess, but the board is the same for both perspectives. You always have a white square down-right on your side. Just rotate the pieces 180º and you are done. "has their pieces at the bottom of the board" Isn't that what you want ? Flip the board for black to move ? Then flip again for white to move etc. – UDKOX May 22 '16 at 21:55
  • You can place a Swing component on **one and only one** JPanel. You have to create a separate set of JButtons for your white view and your black view. – Gilbert Le Blanc May 22 '16 at 22:39
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) See also [Making a robust, resizable Swing Chess GUI](http://stackoverflow.com/q/21142686/418556). – Andrew Thompson May 23 '16 at 02:30

0 Answers0