-2

I'm currently creating a GUI for a Chess game. It's a college assignment. Currently, We've finished the engine. So, for the board, I've created a 2D array of JButtons with the code below.

public class GUI extends JFrame implements ActionListener {

    private JPanel gameBoard;
    private JButton[][] board = new JButton[4][4];
    gameBoard = new JPanel();
    add(board);
    gameBoard.setLayout(new GridLayout(7, 6));
    gameBoard.setBounds(400, 30, 700, 700);
    board = new JButton[4][4] 
    for(int i = 0;i<4;i++) {
      for(int j = 0;j < 4;j++) {
         board[i][j] = new JButton();
         gameBoard.add(board[i][j]);
      }
    }
}  

I am a beginner so I apologize if this code gives a you a nightmare haha, but I'd love it if someone took my implementation as it is and told me how to add an actual piece from the engine. The engine assembles the pieces on its own and creates a board of its own. I also initialized a new game in my GUI, so I can use my Engine's methods than getsPieces and etc. My question is, how do I attach an object of type Piece to the JButton? Thank you! Also I'm open for tips on how to improve my questions, thanks.

SeverityOne
  • 2,476
  • 12
  • 25
JayJay199
  • 1
  • 2
  • 1
    have your Piece extend the JButton class and then have your board be a collection of Piece's instead of JButtons – RAZ_Muh_Taz Apr 23 '18 at 15:34
  • @RAZ_Muh_Taz can't do that, Milestone states that I cannot touch the engine, but create something else. The engine contains a board of Pieces, but I need to attach these pieces to the board of Buttons so that I can implement the movement. – JayJay199 Apr 23 '18 at 15:35

1 Answers1

0

I see a couple of solutions:

Using a Map binding

You could consider doing the opposite : Creating a map to bind a button to a chest piece (like a sort of SQL join table). You could implement something like that:

final Map<Piece, JButton> piecePositions = new HashMap<>();

// When game starts, fill the map
void start() {
    piecePositions.put(whiteTower1, btnx0y0);
    ...
}

// When you move a piece, all you have to do is changing the assignment
void move (Piece toMove, JButton destination) {
    piecePositions.put(toMove, destination);
}

Using custom icons

Another answer is to create a subclass of Icon, which has a Piece as attribute. That way, you can directly associate the piece to the JButton through the icon, and as a bonus, your button will immediately display the piece icon.

class PieceIcon extends ImageIcon {
    final Piece piece;

    PieceIcon(URL pieceImage, Piece piece) {
        super(pieceImage);
        this.piece = piece;
    }

    // When you move, you change your buttons
    public void move(JButton from, JButton to) {
        to.setIcon(from.getIcon());
        from.setIcon(null);
    }
}

Final word

However, I must admit that both previous answers are kind of hacks. To properly manage a game board, I would add a level of indirection by creating a "Grid" class, whose role would be to manage board assignments. This way, you could test multiple solutions, far more evolved than what I've suggested.

Good luck for your project.

amanin
  • 3,436
  • 13
  • 17