uk.co.keang.hex.gui
Class HexLayout

java.lang.Object
  extended by uk.co.keang.hex.gui.HexLayout
All Implemented Interfaces:
java.awt.LayoutManager, java.io.Serializable

public class HexLayout
extends java.lang.Object
implements java.awt.LayoutManager, java.io.Serializable

The Hexagonal layout manager lays out components in a hexagonal grid. If components implement IHexagonal they will be rotated by this layout manager to suit the selected layout direction. Components not implementing IHexagonal and/or Components of any other shape can be laid out but their visible areas may overlap.

This layout manager supports two layout directions: Vertical and horizontal, and three layout styles: Wavey, Straight Min First and Straight Max First.

 Direction                                                                        /\  /\
 Vertical = hexagonal components are rotated so that their sides are vertical ie |  ||  |
                                                                                  \/  \/
                                                                                      __
 Horizontal = hexagonal components are rotated so that their sides are horizontal ie /  \
                                                                                     \  /
                                                                                      ==
                                                                                     /  \
                                                                                     \  /
                                                                                      --
 

Style: (all examples shown are for vertical layout)
Wavey = the same number of components per line

 ie  * * * *
      * * * *
     * * * *
      * * * *
 

Straight Min First = Alternate lines, starting with the first line, have one less component.

 ie   * * *
     * * * *
      * * *
     * * * *
 

Straight Max First = Alternate lines, starting with the second line, have one less component.

 ie  * * * *
      * * *
     * * * *
      * * *
 

The size of the layout area is described in terms of the number of components per line and the number of lines. Either dimension can be zero, in which that dimension is sized to fit the number of components in the panel. If both dimensions are specified and layout area is sized to the given dimension. The components are placed along each line until the max line length is reached. If more components are added than the size of the area permits the number of lines is increased.

For Vertical direction the components are added from left to right. For Horizontal direction the components are added from top to bottom.

By default the hexagons will be stretched to fill the window in both the X and Y axis, if you require pure hexagons call setStretchy(false).

Version:
1.1
Author:
A.G.Docherty
See Also:
Serialized Form

Nested Class Summary
static class HexLayout.HexLayoutStyle
          An enum defining the layout styles available
 
Field Summary
(package private)  int cgap
          This is the gap (in pixels) which specifies the space between components.
(package private)  int cpl
          This is the number of rows specified for the grid.
(package private)  int lines
          This is the number of columns specified for the grid.
 
Constructor Summary
HexLayout()
          Creates a hex layout with a default of one column per component, in a single row.
HexLayout(int r, int c)
          Creates a hex layout with the specified number of rows and columns.
HexLayout(int nComps, int nLines, int hgap, HexBtnUtilities.HexOrientation o, HexLayout.HexLayoutStyle style)
          Creates an offset hex layout with the specified number of lines and components per line.
 
Method Summary
 void addLayoutComponent(java.lang.String name, java.awt.Component comp)
          Adds the specified component with the specified name to the layout.
 int getComponentsPerLine()
          Gets the number of components per line in this layout.
 int getGap()
          Gets the gap between components.
 int getLines()
          Gets the number of lines in this layout.
 boolean isStretchy()
          Returns true if hexagons can be stretched to fill the whole panel
 void layoutContainer(java.awt.Container parent)
          Lays out the specified container using this layout.
 java.awt.Dimension minimumLayoutSize(java.awt.Container parent)
          Determines the minimum size of the container argument using this grid layout.
 java.awt.Dimension preferredLayoutSize(java.awt.Container parent)
          Determines the preferred size of the container argument using this grid layout.
 void removeLayoutComponent(java.awt.Component comp)
          Removes the specified component from the layout.
 void setComponentsPerLine(int c)
          Sets the number of components per line in this layout to the specified value.
 void setGap(int gap)
          Sets the gap between components to the specified value.
 void setLines(int n)
          Sets the number of lines in this layout to the specified value.
 void setStretchy(boolean stretch)
          Sets whether hexagons have to be pure or whether they can be stretched to fill the whole panel
 java.lang.String toString()
          Returns the string representation of this grid layout's values.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

cgap

int cgap
This is the gap (in pixels) which specifies the space between components. They can be changed at any time. This should be a non-negative integer.


cpl

int cpl
This is the number of rows specified for the grid. The number of rows can be changed at any time. This should be a non negative integer, where '0' means 'any number' meaning that the number of Rows in that dimension depends on the other dimension.


lines

int lines
This is the number of columns specified for the grid. The number of columns can be changed at any time. This should be a non negative integer, where '0' means 'any number' meaning that the number of Columns in that dimension depends on the other dimension.

Constructor Detail

HexLayout

public HexLayout()
Creates a hex layout with a default of one column per component, in a single row.


HexLayout

public HexLayout(int r,
                 int c)
Creates a hex layout with the specified number of rows and columns. All components in the layout are given equal size.

One, but not both, of rows and cols can be zero, which means that any number of objects can be placed in a row or in a column.

Parameters:
r - the rows, with the value zero meaning any number of rows.
c - the columns, with the value zero meaning any number of columns.

HexLayout

public HexLayout(int nComps,
                 int nLines,
                 int hgap,
                 HexBtnUtilities.HexOrientation o,
                 HexLayout.HexLayoutStyle style)
Creates an offset hex layout with the specified number of lines and components per line. All components in the layout are given equal size.

In addition, the gap between components is set to the specified value.

If nComps is zero any number of objects can be placed on a line. If lines is zero there can be any number of lines. If both are zero the layout will be approximately a square layout.

All HexLayout constructors defer to this one.

Parameters:
nComps - the rows, with the value zero meaning any number of rows
nLines - the columns, with the value zero meaning any number of columns
hgap - the gap around the component
o - the orientation of the hexagon ie vertical or horizontal
Method Detail

getComponentsPerLine

public int getComponentsPerLine()
Gets the number of components per line in this layout.

Returns:
the number of components per line in this layout

setComponentsPerLine

public void setComponentsPerLine(int c)
Sets the number of components per line in this layout to the specified value.

Parameters:
c - the number of components per line in this layout

getLines

public int getLines()
Gets the number of lines in this layout.

Returns:
the number of lines in this layout

setLines

public void setLines(int n)
Sets the number of lines in this layout to the specified value.

Parameters:
n - the number of lines in this layout

getGap

public int getGap()
Gets the gap between components.

Returns:
the gap between components

setGap

public void setGap(int gap)
Sets the gap between components to the specified value.

Parameters:
gap - the gap between components

setStretchy

public void setStretchy(boolean stretch)
Sets whether hexagons have to be pure or whether they can be stretched to fill the whole panel

Parameters:
stretch - true to allow components to be stretched, false for pure hexagons

isStretchy

public boolean isStretchy()
Returns true if hexagons can be stretched to fill the whole panel

Returns:
true if components can be stretched

addLayoutComponent

public void addLayoutComponent(java.lang.String name,
                               java.awt.Component comp)
Adds the specified component with the specified name to the layout.

Specified by:
addLayoutComponent in interface java.awt.LayoutManager
Parameters:
name - the name of the component
comp - the component to be added

removeLayoutComponent

public void removeLayoutComponent(java.awt.Component comp)
Removes the specified component from the layout.

Specified by:
removeLayoutComponent in interface java.awt.LayoutManager
Parameters:
comp - the component to be removed

preferredLayoutSize

public java.awt.Dimension preferredLayoutSize(java.awt.Container parent)
Determines the preferred size of the container argument using this grid layout.

The preferred width of a grid layout is the largest preferred width of all of the components in the container times the number of columns, plus the horizontal padding times the number of columns minus one, plus the left and right insets of the target container, plus the width of half a component if there is more than one row.

The preferred height of a grid layout is the largest preferred height of all of the components in the container plus three quarters of the largest minimum height of all of the components in the container times the number of rows greater than one, plus the vertical padding times the number of rows minus one, plus the top and bottom insets of the target container.

Specified by:
preferredLayoutSize in interface java.awt.LayoutManager
Parameters:
parent - the container in which to do the layout
Returns:
the preferred dimensions to lay out the subcomponents of the specified container
See Also:
GridLayout.minimumLayoutSize(java.awt.Container), Container.getPreferredSize()

minimumLayoutSize

public java.awt.Dimension minimumLayoutSize(java.awt.Container parent)
Determines the minimum size of the container argument using this grid layout.

The minimum width of a grid layout is the largest minimum width of all of the components in the container times the number of columns, plus the horizontal padding times the number of columns minus one, plus the left and right insets of the target container, plus the width of half a component if there is more than one row.

The minimum height of a grid layout is the largest minimum height of all of the components in the container plus three quarters of the largest minimum height of all of the components in the container times the number of rows greater than one, plus the vertical padding times the number of rows minus one, plus the top and bottom insets of the target container.

Specified by:
minimumLayoutSize in interface java.awt.LayoutManager
Parameters:
parent - the container in which to do the layout
Returns:
the minimum dimensions needed to lay out the subcomponents of the specified container
See Also:
GridLayout.preferredLayoutSize(java.awt.Container), Container.doLayout()

layoutContainer

public void layoutContainer(java.awt.Container parent)
Lays out the specified container using this layout.

This method reshapes the components in the specified target container in order to satisfy the constraints of the GridLayout object.

The grid layout manager determines the size of individual components by dividing the free space in the container into equal-sized portions according to the number of rows and columns in the layout. The container's free space equals the container's size minus any insets and any specified horizontal or vertical gap. All components in a grid layout are given the same size.

Specified by:
layoutContainer in interface java.awt.LayoutManager
Parameters:
parent - the container in which to do the layout
See Also:
Container, Container.doLayout()

toString

public java.lang.String toString()
Returns the string representation of this grid layout's values.

Overrides:
toString in class java.lang.Object
Returns:
a string representation of this grid layout