0

I'm trying to make a chess game in java using NetBeans IDE for the GUI, but I can't make a chesspiece stay above the chessboard, when I try the chessboard just gets pushed away. I'm using a label for both images. How can I make the chesspieces stay above the chessboard without making the chessboard a background? (The window will have the chessboard and a information panel to the right like timer and stuff, so i don't want to make the chessboard image a background to the entire frame)

nachokk
  • 14,363
  • 4
  • 24
  • 53
WalrusNine
  • 103
  • 9
  • You might want to post a snippet of code to help people understand your problem and help you. – b2Wc0EKKOvLPn Mar 09 '14 at 22:39
  • are you using swing or javafx?? – nachokk Mar 09 '14 at 22:41
  • @nachokk I'm using swing. For now I'm only using the IDE to do some tests and planning the program, didn't start the program yet. But got stuck in the GUI test because of the overlay problem. I just want to know how to make two images overlay using NetBeans IDE. – WalrusNine Mar 09 '14 at 22:47
  • @nachokk well the thing is I started learning java 2 weeks ago so if I don't use the IDE I won't make it on time so the teacher said to use the IDE. Anyway, by setting the AbsoluteLayout it worked, I'm able to put a piece above another, but the OverlayLayout didn't (again, in the IDE, not programming by myself hehe). But is AbsoluteLayout recommended or not? null layout would let me overlay as well btw. – WalrusNine Mar 09 '14 at 23:08

1 Answers1

2

There are a number things you might try.

You could...

add the piece label to the board label. This would allow you to "overlay" the piece over the individual cells. This would require you to use a layout manager for the cell, something like a GridBagLayout might do, as it automatically centers the components within it's parent's space.

You Could...

Use a JLayeredPane using something like a GridBagLayout, would allow you to over one panel on top of another.

The bottom layer would be a JPanel, which contained each cell of the board and the top layer would be a transparent JPanel which contained all the pieces

This becomes more complicated as you'd need to devise some way to fill the empty spaces of the "pieces" layer.

Both layers would use either a GridBagLayout or GridLayout depending on your needs...

You Could....

Use a single container and a GridBagLayout. This would allow you to add two components to the same (virtual) cell within the layout at the same time.

This becomes a little tricky, as you need to manage the z-depthness of the components to ensure that the pieces are laid out on top of the board.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366