• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Layout Manager for Square game

 
Abhin Balur
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

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.

Thank You.
Regards.
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some more explanation/screenshot/diagram/link would be helpful.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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'
 
Abhin Balur
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Something like this. Square game
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh!
You mean the 13 puzzle.

GridLayout will suit your requirement perfectly.
 
Abhin Balur
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To Manish,

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?
 
Rob Spoor
Sheriff
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
GridLayout uses the insertion order of the container. If you use the add method that takes an index you can specify the location to add a component at.
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do not bother with adding/removing from the parent container, it will complicate things.
Like Michael mentioned in his reply, just change the text/appearance of the buttons.
 
Abhin Balur
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Maneesh

I got your point. Dealing with text/appearance will be much easier. Thankx for the quick replies. Will keep posting if i get stuck while writing the puzzle

@Rob
Just for the knowledge; is there any add method that takes the location index?
 
Rob Spoor
Sheriff
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.awt.Container

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;

Vice versa:
int index = row * columns + column;


* unless the container's component orientation is from right to left.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic