0

I'm building a board game using java swing, and I'm using JLabels for the Tiles of the game, and JLabels for the pawns. My question, is there a way I can add the pawn on top of a Tile by doing something like this :

Tiles[currentPosition].add(pawnLabel);

with the code above the program is being executed but I cant see the pawn. If I use:

Tiles[currentPosition].add(pawnLabel, 1);

I'm getting this exception:

  • illegal component position

Do you have any ideas?

Ntavas
  • 1
  • Not sure why you have "1" as a parameter. Swing has a parent/child relationship so you can just add the child component to the parent. However, you need to set the layout manager of the Tile label to use a BorderLayout (or some other layout manager) in order to add the child component. Though the question might be why are you using a JLabel as the background. Maybe use a JPanel? Check out https://stackoverflow.com/questions/2561690/placing-component-on-glass-pane/2562685#2562685 for an example of a chess board with icons for the chess pieces. – camickr Dec 30 '21 at 15:26
  • Im using a JPanel for the background and Jlabels for the Tiles placed on this JPanel. – Ntavas Dec 30 '21 at 15:37
  • 2
    I understand and my question was why? You need to color the squares of the chess board. So if you are going to use components typically people will use a JPanel with a GridLayout for the 64 Tiles. Then you need to color each Tile so you use a panel with the appropriate square color. Then you need to add a JLabel to a specific Tile to represent a chess piece. There is no need to use a JLabel to represent a Tile because you don't need all the painting logic of the JLabel. All you need to do is paint the color. – camickr Dec 30 '21 at 15:46
  • The reason im using JLabel is because each Tile has a spesifc image. In chess the board doesnt have any images, except for the pawns. – Ntavas Dec 30 '21 at 15:58
  • 1
    Another option is to create a drawing `JPanel`, draw the chessboard, and draw images of chess pieces on the chessboard. – Gilbert Le Blanc Dec 30 '21 at 15:59
  • I figured that with the implementation that I have its best to use the setBounds method, something like pawn.setBounds(Tile[currentPosition].getBounds); and then add it to the main panel. – Ntavas Dec 30 '21 at 17:05
  • If you're replying to someone do tag them @camickr, so that they get notified (the `@` is important). *"... its best to use the setBounds method"*, Swing has to deal with different OS, PLAFs, screen sizes and resolutions, let the layout managers do the hard work for you, if you use `setBounds(...)` a.k.a. `null-layout` you're going to have more and more, and harder problems to solve, [for example](https://stackoverflow.com/a/42521097/2180785). – Frakcool Dec 30 '21 at 18:54
  • @Frakcool I guess I have to study more about layouts cause I'm a new to this. Thanks for your advice! – Ntavas Dec 30 '21 at 19:00
  • You were given some examples and ideas by both camickr and Gilbert, try them out and btw, your variables (such as `Tiles` should start with a lowerCase letter) – Frakcool Dec 30 '21 at 19:00
  • Also check out this [chessboard example](https://stackoverflow.com/a/21142687/418556). It doesn't use any labels, but instead buttons. It uses the buttons as both tile places and to display the pieces, so no need to put components on top of one another. – Andrew Thompson Dec 30 '21 at 20:30

0 Answers0