Search code examples
javaswinglayerchess

Chess Layering Issue


I'm fairly new to Java, and I decided to try to make a Chess game with Swing. I know that my program is extremely inefficient, but that is not really my problem. My issue is that I cannot view the image of the white pawn after I've added the JButtons, but I can see the pawn if I put the code to add the pawn JLabel before the addition of the buttons. For this reason, I thought that the problem was that I had a layering problem, and I tried replacing the panels with LayeredPanes and didn't have any success.

Basically, what I want to happen is to have the JButton and the JLabel both show up in the same square and keep the button clickable. Also I'd really like to not incorporate any new imports, and just use JButtons, JLabels, etc. Sorry if my explanation is a bit confusing, and thanks in advance. Here is my code:

private JFrame frame = new JFrame();
private JPanel[][] square = new JPanel[8][8];
private JButton[][] squareB = new JButton[8][8];
private JPanel blank[] = new JPanel[6];
private JButton newGame = new JButton("New Game");
private JButton exitGame = new JButton("Exit");

ArrayList <Chess> pieces = new ArrayList<Chess>();
ChessGame() throws IOException
{   

    frame.setLayout(new GridLayout(9,8));
    frame.setSize(800,900);
    for(int x=0; x<8; x++)
    {
        for(int y=0; y<8; y++)
        {
            square[x][y] = new JPanel();
            square[x][y].setSize(100,100);
        }
    }

    for(int y=0; y<8; y++)
    {
        for(int x=0; x<8; x++)
        {
            frame.add(square[x][y]);
            if(y%2==0)
                if(x%2==0)
                    square[x][y].setBackground(Color.cyan);
                else
                    square[x][y].setBackground(Color.blue);
            else
                if(x%2==0)
                    square[x][y].setBackground(Color.blue);
                else
                    square[x][y].setBackground(Color.cyan);
        }
    }
    for(int x=0; x<8; x++)
    {
        for(int y=0; y<8; y++)
        {
            squareB[x][y] = new JButton();
            squareB[x][y] = new Chess(x,y);
            square[x][y].add(squareB[x][y]);
            squareB[x][y].setSize(square[x][y].getSize());
            squareB[x][y].setMargin(new Insets(0, 0, 100, 100));
            squareB[x][y].setContentAreaFilled( false );  
            squareB[x][y].setOpaque(false);
            squareB[x][y].setContentAreaFilled(false);
            squareB[x][y].setBorderPainted(false);
            squareB[x][y].addActionListener(this);
        }
    }
    for(int x=0; x<6; x++)
    {
        blank[x] = new JPanel();
        frame.add(blank[x]);
        if(x==2)
        {
            frame.add(newGame);
            frame.add(exitGame);
        }
    }

    JLabel WPawn = new JLabel(new ImageIcon(((new ImageIcon("C:\\Users\\Matthew\\Desktop\\Chess Pieces\\WPawn.png")).getImage()).getScaledInstance(80, 83, java.awt.Image.SCALE_SMOOTH)));
    squareB[1][1].add(WPawn);

    newGame.addActionListener(this);
    exitGame.addActionListener(this);
    frame.setVisible(true);
}
public static void main(String[] args) throws IOException
{
    new ChessGame();
}

Solution

  • Instead of trying to add a JLabel to a JButton, which has no layout manager, set the JButtons icon

    Image WPawn = new ImageIcon(ImageIcon("C:\\Users\\Matthew\\Desktop\\Chess Pieces\\WPawn.png").getImage().getScaledInstance(80, 83, java.awt.Image.SCALE_SMOOTH));
    squareB[1][1].setIcon(WPawn);
    

    You might like to have a close look at How to use Buttons

    Ps I'd also recommend ImageIO over ImageIcon for reading images