I want to try my hands on developing some small games. I would like to start with the Square game; the one which involves sequecning the numbers, and placing them in sorted order. However i am new to Swings. So the only hurdle here is developing the GUI for me beause rest all would be pure logic. I would like to know which layout manager would be helpful, as it will involve relocating the components on a Jpanel on mouse clicks and any method which would do the same.
sounds like you are attempting a Slide Puzzle, in which case you would use a JPanel
set as a GridLayout 3x3 or 4x4 or 5x5 etc, and add the appropriate number of buttons,
but you don't move the buttons, you just change their text, depending on your 'logic'
Now that you know what the puzzle is; i will be using buttons on a JPanel with GridLayout. I just checked the API docs for this layout manager. From the first look it appears methods removelayoutcomponent and addlayoutcomponent will be helpful but it does not specify at what point on the layout, for example 4th column-3rd row to add or remove the component. I will be needing the location of the buttons. Is there anyway i can add/remove/get a compoent at a specific point/cell of GridLayout?
There is no method for cell location (row, column pair), but GridLayout fills out the columns first, rows second; basically, from left to right*, top to bottom. If you use a fixed number of columns and 0 for the rows, the GridLayout will always fill the columns, adding rows as necessary. You can then calculate the cell as follows:
int column = index % columns;
int row = index / columns;
int index = row * columns + column;
* unless the container's component orientation is from right to left.