Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

What layout to use for a chess program?

 
Louis Lewis
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I'm trying to create a chess program. Initially, I tried to use netbeans GUI creator's 'freeflow' layout, but encountered a lot of glitches and just generally couldn't get it to do what I wanted, so I switched temporarily to absolute layout. However, now that I have actually written the chess board part of the program, I am abandoning absolute layout because I want the chessboard to resize when the window changes size, and also I'd like to add a list that lists all the moves in a game, and a jlabel that tells the user when check or checkmate occurs.

I am using one main JPanel that contains 64 jlabels, which I change the image of throughout the program to set up the board and to make pieces move. As I said, I would like this part (the board part) to shrink if the user changes the size of the window, but I don't know how to get the jlabels to change with window size, let alone how to make it so that the images are not cut off when the labels shrink. I also have a list on the right of the board that lists all the moves in the current game, and a jlabel below the board that announces check or checkmate.

Anyone know if this is possible/what layout I should use to accomplish this? Is it possible to use the GUI creator's freeflow layout? Or should I switch to another default java layout? Or is what I'm trying to do even possible (would I have to use a custom layout or something)?

Help would be much appreciated.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you have already found out, using a null/absolute layout is always a bad idea.
The chess board is a grid of 8x8 squares. The GridLayout is exactly what you need
 
Campbell Ritchie
Marshal
Posts: 52621
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This discussion would fit better in our GUIs forum: moving.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may need to create a subclass of JLabel that reacts appropriately to being resized if the default behavior is not what you need.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would prefer to use a customized JPanel with the a colored background (black/white)
1) Create a main JPanel, set the 8x8 grid layout
2) Create and add the 64 zebra panel instances to it
3) Use a JLabel(Icon image) for the pieces.
4) Add remove these piece labels to to/from the zebra panels as required
 
Rob Camick
Ranch Hand
Posts: 2703
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but I don't know how to get the jlabels to change with window size,


The GridLayout resizes all components equally.

let alone how to make it so that the images are not cut off when the labels shrink.


Use Darryl's Stretch Icon.
 
Louis Lewis
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I changed the layout so that the main panel is freeflow, and is composed of three components; 1) a jpanel using 8X8 grid layout (with 64 jlabels) 2) a jlist, and 3) a jlabel.

Before I add an image to any of the jlabels, they stretch and change size and shape when I change the size of the window, just like I want. However, when I add images, the board's dimensions remain locked in, even when I create the icons with the stretch icon class.

This is my code for creating the stretchable square icons:



Am I misusing the stretch icon class? I don't really understand it beyond figuring out which constructor I wanted to use and then creating the icons accordingly.
 
Rob Camick
Ranch Hand
Posts: 2703
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have no idea what a FreeFlow layout is but I doubt you should be using it.

Instead use a BorderLayout. Add the panel containing the GridLayout to the CENTER. This area will grow/shrink as you resize the frame.

Create another panel for your other components and add it to the SOUTH. This panel will remain a fixed size.

 
Louis Lewis
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Freeflow layout is something built in the netbeans GUI builder, but it isn't causing me problems right now for the overall layout. The main problem I'm having is that once I set the icons of the jLabels (even with the stretch icon class), they no longer change size with the window.

Anyone know how to fix this?
 
Rob Camick
Ranch Hand
Posts: 2703
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyone know how to fix this?


Already answered, the problem is the layout manager.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!