0
\$\begingroup\$

I am making a chess game, and I made the background, but now, when I try to place the pieces, they are not added into the window. I tried instead of images text, but that doesn't show up neither.

Code:

public void generate(String th, String fen) {
    ImageIcon l_winter = new ImageIcon("/Users/filipadominikruzek/NetBeansProjects/LanternChess/src/royal/bibe/assets/winter/light.gif");
    ImageIcon d_winter = new ImageIcon("/Users/filipadominikruzek/NetBeansProjects/LanternChess/src/royal/bibe/assets/winter/dark.gif");
    ImageIcon l_wood = new ImageIcon("/Users/filipadominikruzek/NetBeansProjects/LanternChess/src/royal/bibe/assets/wood/light.png");
    ImageIcon d_wood = new ImageIcon("/Users/filipadominikruzek/NetBeansProjects/LanternChess/src/royal/bibe/assets/wood/dark.png");
    ImageIcon br = new ImageIcon("/Users/filipadominikruzek/NetBeansProjects/LanternChess/src/royal/bibe/assets/pieces/br.png");
    ImageIcon bb = new ImageIcon("/Users/filipadominikruzek/NetBeansProjects/LanternChess/src/royal/bibe/assets/pieces/bb.png");
    ImageIcon bk = new ImageIcon("/Users/filipadominikruzek/NetBeansProjects/LanternChess/src/royal/bibe/assets/pieces/bk.png");
    ImageIcon bq = new ImageIcon("/Users/filipadominikruzek/NetBeansProjects/LanternChess/src/royal/bibe/assets/pieces/bq.png");
    ImageIcon bn = new ImageIcon("/Users/filipadominikruzek/NetBeansProjects/LanternChess/src/royal/bibe/assets/pieces/bn.png");
    ImageIcon bp = new ImageIcon("/Users/filipadominikruzek/NetBeansProjects/LanternChess/src/royal/bibe/assets/pieces/bp.png");
    ImageIcon wr = new ImageIcon("/Users/filipadominikruzek/NetBeansProjects/LanternChess/src/royal/bibe/assets/pieces/wr.png");
    ImageIcon wb = new ImageIcon("/Users/filipadominikruzek/NetBeansProjects/LanternChess/src/royal/bibe/assets/pieces/wb.png");
    ImageIcon wk = new ImageIcon("/Users/filipadominikruzek/NetBeansProjects/LanternChess/src/royal/bibe/assets/pieces/wk.png");
    ImageIcon wq = new ImageIcon("/Users/filipadominikruzek/NetBeansProjects/LanternChess/src/royal/bibe/assets/pieces/wq.png");
    ImageIcon wn = new ImageIcon("/Users/filipadominikruzek/NetBeansProjects/LanternChess/src/royal/bibe/assets/pieces/wn.png");
    ImageIcon wp = new ImageIcon("/Users/filipadominikruzek/NetBeansProjects/LanternChess/src/royal/bibe/assets/pieces/wp.png");
    String theme = th;
    int color = 0;
    for (int i = 0; i < 600; i = i + 75) {
        for (int j = 0; j < 600; j = j + 75) {
            JLabel panel = new JLabel();
            getContentPane().setComponentZOrder(panel, 0);
            panel.setText(null);
            panel.setSize(75, 75);
            if (color == 0) {
                if (theme.equals("wood")) {
                    panel.setIcon(l_wood);
                } else if (theme.equals("winter")) {
                    panel.setIcon(l_winter);
                } else {
                    panel.setText("notexture");
                }
                //panel.setBackground(new Color(white[0], white[1], white[2]));
                color = 1;
            } else {
                if (theme.equals("wood")) {
                    panel.setIcon(d_wood);
                } else if (theme.equals("winter")) {
                    panel.setIcon(d_winter);
                } else {
                    panel.setText("notexture");
                }
                //panel.setBackground(new Color(black[0], black[1], black[2]));
                color = 0;
            }
            panel.setLocation(j, i);
            panel.addMouseListener(new TileMouseListener(j, i));
            getContentPane().setComponentZOrder(panel, 0);
            getContentPane().add(panel);
        }
    if (color == 0) {
        color = 1;
    } else {
           color = 0;
        }
    }
    var x = 0;
    var y = 0;
    for (String f : fen.split("")) {
        JLabel pan = new JLabel("ladder");
        pan.setText("ladder");
        pan.setSize(70, 70);
        pan.setVisible(true);
        switch (f) {
            case "r" -> {
                System.out.println("here is r");
                pan.setIcon(br);
                pan.setLocation(x, y);
                System.out.println(pan.getLocation());
                getContentPane().add(pan);
                x += 70;
            }
            case "n" -> {
                pan.setIcon(bn);
                pan.setLocation(x, y);
                getContentPane().add(pan);
                x += 70;
            }
            case "b" -> {
                pan.setIcon(bb);
                pan.setLocation(x, y);
                getContentPane().add(pan);
                x += 70;
            }
            case "q" -> {
                pan.setIcon(bq);
                pan.setLocation(x, y);
                getContentPane().add(pan);
                x += 70;
            }
            case "k" -> {
                pan.setIcon(bk);
                pan.setLocation(x, y);
                getContentPane().add(pan);
                x += 70;
            }
            case "p" -> {
                pan.setIcon(bp);
                pan.setLocation(x, y);
                getContentPane().add(pan);
                x += 70;
            }
            case "R" -> {
                pan.setIcon(wr);
                pan.setLocation(x, y);
                getContentPane().add(pan);
                x += 70;
            }
            case "N" -> {
                pan.setIcon(wn);
                pan.setLocation(x, y);
                getContentPane().add(pan);
                x += 70;
            }
            case "B" -> {
                pan.setIcon(wb);
                pan.setLocation(x, y);
                getContentPane().add(pan);
                x += 70;
            }
            case "Q" -> {
                pan.setIcon(wq);
                pan.setLocation(x, y);
                getContentPane().add(pan);
                x += 70;
            }
            case "K" -> {
                pan.setIcon(wk);
                pan.setLocation(x, y);
                getContentPane().add(pan);
                x += 70;
            }
            case "P" -> {
                pan.setIcon(wp);
                pan.setLocation(x, y);
                getContentPane().add(pan);
                x += 70;
            }
            case "/" -> {
                x = 0;
                y += 70;
            }
            case "8" -> x = 0;
            default -> {
                break;
            }
        }
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ I'm not familiar with this framework, but could it be that it is because the background is painted after and over the other components? You set a z-order to it but not to the other components. \$\endgroup\$
    – Vaillancourt
    May 14 '21 at 11:56

You must log in to answer this question.

Browse other questions tagged .