Reputation: 31
I have an array of JPanels that draws a Checkers-style board, some of those JPanels have black or yellow pieces on them, according to their position. I want to be able to move those pieces obviously.. is there a way to do that by switching their position in the array? I tried using the basic swap way in which I create a tmp and switch.. but that didn't work.. any help is much appreciated.
the swap trial:
JPanel tmp = board[4][3];
board[4][3] = board[2][7];
board[2][7] = tmp;
Upvotes: 1
Views: 86
Reputation: 324147
some of those JPanels have black or yellow pieces on them, according to their position. I want to be able to move those pieces obviously..
Upvotes: 1
Reputation: 882
The visual position of some JPanel on the screen does not correlate to its position in some array (i. e. position in memory).
You could...
Swap the array locations as you described and additionally swap the visual positions (via getLocation()
/setLocation()
)
Leave both array position and visual positions unchanged, but swap the state (i. e. swap background colors or so).
Upvotes: 1
Reputation: 1553
Have you tried repainting the entire frame?
I'm not sure what you did works as you expect. In case it doesn't you may want to try creating a method addComponents() in which you would (obviously :) ) add all the components in your frame.
When a change would be needed call removeAll(), call addComponents() again, and then validate() and repaint() :).
Upvotes: 0