I am working on a chess game. I want the Container board to use GridLayout to display an 8x8 grid from JPanels. (This will make functions such as highlighting selected squares and actual moves much easier.) Then I would like to add fragments on top of this layer so that they can be dragged and dropped. At first, I had shapes depicting them, drawing them in a separate JPanels square, but thought this would be a problem when trying to drag them later. Since then, I have been trying to use JLayeredPane as the main container, but have encountered several problems.
One of them is that after I set the GridLayout for the JLayeredPane, no matter which Integer I use to specify the layer to add JLabel or another image, the pieces are added to the grid, making their positions set and distort the whole board . I read that using LayoutManagers can interfere with the positioning of a layer on a JLayeredPane, so this is not too surprising. (Although the Oracle demo from the JLayeredPane tutorial seems to do it very well: http://download.oracle.com/javase/tutorial/uiswing/examples/components/LayeredPaneDemo2Project/src/components/LayeredPaneDemo2.java )
However, I also tried to put the JPanels grid in my own JPanel and then add it to the low JLayeredPane, the idea is that I could add drag and drop icons for splitting, opaque JPanels at the higher JLayeredPane. However, when I do this, after I just have a JPanel mesh inside the JLayeredPane (i.e., before adding a drag layer), the mesh will not be displayed.
I also tried to override the JLayeredPane paintComponent (and paint) methods to paint the pieces, but they are hidden by JPanels (I see that they really exist by setting JPanels to opaque) and as far as I can tell, there is no way to set the graphics layer on JLayeredPane. I also tried using a glass frame to draw shapes, but there was also unwanted behavior there.
Any help explaining some of this behavior, or when I am wrong, will be greatly appreciated!
source share