File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Layout Manager for Square game Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Layout Manager for Square game" Watch "Layout Manager for Square game" New topic
Author

Layout Manager for Square game

Abhin Balur
Greenhorn

Joined: Feb 18, 2008
Posts: 21
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

Joined: Jul 26, 2007
Posts: 10169
    
    8

Some more explanation/screenshot/diagram/link would be helpful.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
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

Joined: Feb 18, 2008
Posts: 21
Hi,

Something like this. Square game
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10169
    
    8

Oh!
You mean the 13 puzzle.

GridLayout will suit your requirement perfectly.
Abhin Balur
Greenhorn

Joined: Feb 18, 2008
Posts: 21
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

Joined: Oct 27, 2005
Posts: 19651
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10169
    
    8

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

Joined: Feb 18, 2008
Posts: 21
@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

Joined: Oct 27, 2005
Posts: 19651
    
  18

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Layout Manager for Square game
 
Similar Threads
How to set this component?
background on Java Desktop Application
Java Board Game
Square game
sliding puzzle help