• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Layout Manager for Square game

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some more explanation/screenshot/diagram/link would be helpful.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Something like this. Square game
 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 22665
127
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22665
127
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic