0

I'm trying to make a Chess Game, I'm stuck with this:

I create a bunch of panels each one has a label that has an icon (piece icon) with a loop for every panel to represent a case in the game. How can I delete an icon from the last position after the user drags the piece to a new position?

import ma.jerroudi.cheesegame.bouard.Bouard;
import ma.jerroudi.cheesegame.bouard.Case;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

public class CheeseGameGui extends JFrame {
    Point prevPt;
    Point currentPt;

    public CheeseGameGui() {
        Bouard bouard = new Bouard();
        addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent eP) {
                prevPt = eP.getPoint();
                System.out.println(" x: " + eP.getX() + " y: " + eP.getY());
                System.out.println("tab[" + (int) prevPt.getX() / 50 + "] [" + (int) prevPt.getY() / 50 + "]");
            }

            @Override
            public void mouseReleased(MouseEvent eR) {
                currentPt = eR.getPoint();
                System.out.println(" x: " + eR.getX() + " y: " + eR.getY());
                bouard.caseChange((int) prevPt.getY() / 50, (int) prevPt.getX() / 50, (int) currentPt.getY() / 50, (int) currentPt.getX() / 50);
                System.out.println("piece name : " + bouard.getCase((int) currentPt.getY() / 50, (int) currentPt.getX() / 50).getSymbol());
            }
        });
        this.setTitle("jeroudi cheese game");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ImageIcon image = new ImageIcon("logo.jpg");
        this.setIconImage(image.getImage());
        for (int i = 0; i <= Bouard.MAX_SIZE; i++) {
            for (int j = 0; j <= Bouard.MAX_SIZE; j++) {
                JPanel mypanel = new JPanel();
                this.add(mypanel);
                JLabel labelImg = new JLabel();
                int xIndice = j * 50;
                int yIndice = i * 50;
                mypanel.setBounds(xIndice, yIndice, 50, 50);
                mypanel.setBorder(BorderFactory.createLineBorder(Color.green));
                ImageIcon pieceImg = new ImageIcon(new ImageIcon(bouard.getCase(i, j).getPiecePhat()).getImage().getScaledInstance(50, 50, Image.SCALE_DEFAULT));
                labelImg.setIcon(pieceImg);
                mypanel.add(labelImg);
                this.getContentPane().add(mypanel);
            }
        }
        setSize(450, 450);
        setLayout(null);
        this.setUndecorated(true);
        setVisible(true);
    }
}


Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
jerr
  • 1
  • 1
  • 1
    check out: https://stackoverflow.com/questions/6811247/drawing-in-jlayeredpane-over-exising-jpanels/6811800#6811800 for one approach. – camickr Sep 23 '21 at 14:27
  • 1
    [One more](https://stackoverflow.com/questions/21142686/making-a-robust-resizable-swing-chess-gui) approach – Sergiy Medvynskyy Sep 23 '21 at 14:39
  • 1
    And just to completely screw with you, [example](https://stackoverflow.com/questions/26621830/jlayeredpanel-layout-manager-free-moving-objects/26624184#26624184), [example](https://stackoverflow.com/questions/41836723/which-layout-manager-to-use-on-this-game/41837799#41837799) and [example](https://stackoverflow.com/questions/13698217/how-to-make-draggable-components-with-imageicon/13700490#13700490) – MadProgrammer Sep 23 '21 at 14:46

1 Answers1

1

setLayout(null); Don't do this. Instead, set a GridLayout to the chessboard (a single JPanel).

Don't add labels to panels then add the panels to the chessboard, simply add the 'labels' directly to the one chessboard panel. But make them undecorated buttons (JButton).


Important

Keep a reference to the buttons in an array (JButton[8][8]).

Establish a ChessModel which encapsulates the state of a chess game and a method to configure the GUI (the button icons) to match the model.

When the user (or their opponent) moves a piece, update the model then refresh the chessboard.


Oh, and now the GUI is laid out, instead of the (wrong) guess setSize(450, 450);, simply call pack() for the right sized GUI. For more on laying out the chessboard, see Making a robust, resizable Swing Chess GUI.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433