View Javadoc

1   /***
2    * Redistribution  and use  in source  and binary  forms, with  or without
3    * modification, are permitted provided  that the following conditions are
4    * met :
5    *
6    * . Redistributions  of  source  code  must  retain  the  above copyright
7    *   notice, this list of conditions and the following disclaimer.
8    *
9    * . Redistributions in  binary form  must reproduce  the above  copyright
10   *   notice, this list of conditions  and the following disclaimer in  the
11   *   documentation and/or other materials provided with the distribution.
12   *
13   * . The name of the author may not be used to endorse or promote products
14   *   derived from this software without specific prior written permission.
15   *
16   * THIS SOFTWARE IS  PROVIDED BY THE  AUTHOR ``AS IS''  AND ANY EXPRESS  OR
17   * IMPLIED  WARRANTIES,  INCLUDING,  BUT   NOT  LIMITED  TO,  THE   IMPLIED
18   * WARRANTIES OF MERCHANTABILITY AND  FITNESS FOR A PARTICULAR  PURPOSE ARE
19   * DISCLAIMED.  IN NO  EVENT SHALL  THE AUTHOR  BE LIABLE  FOR ANY  DIRECT,
20   * INDIRECT,  INCIDENTAL,  SPECIAL,  EXEMPLARY,  OR  CONSEQUENTIAL  DAMAGES
21   * (INCLUDING,  BUT  NOT LIMITED  TO,  PROCUREMENT OF  SUBSTITUTE  GOODS OR
22   * SERVICES;  LOSS  OF USE,  DATA,  OR PROFITS;  OR  BUSINESS INTERRUPTION)
23   * HOWEVER CAUSED  AND ON  ANY THEORY  OF LIABILITY,  WHETHER IN  CONTRACT,
24   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25   * ANY  WAY  OUT OF  THE  USE OF  THIS  SOFTWARE, EVEN  IF  ADVISED OF  THE
26   * POSSIBILITY OF SUCH DAMAGE.
27   *
28   * $Id: GridDemo.java,v 1.6 2005/09/09 18:24:38 mat007 Exp $
29   */
30  
31  package jtge.sample.grid;
32  
33  import java.awt.Dimension;
34  import java.awt.Toolkit;
35  import javax.swing.JFrame;
36  import javax.swing.SwingUtilities;
37  import jtge.util.grid.Grid;
38  import jtge.util.grid.IGrid;
39  import jtge.util.grid.builder.FlatHexagonBuilder;
40  import jtge.util.grid.builder.PeakHexagonBuilder;
41  import jtge.util.grid.directiongroup.FlatHexagonDirectionGroup;
42  import jtge.util.grid.directiongroup.FlatShiftedHexagonDirectionGroup;
43  import jtge.util.grid.directiongroup.PeakHexagonDirectionGroup;
44  import jtge.util.grid.directiongroup.PeakShiftedHexagonDirectionGroup;
45  
46  /***
47   * Demonstrates the various hexagon grid types handled by jtge.
48   * <p>
49   * This demo opens four windows, each holding a specific type of hexagonal grid.
50   *
51   * @author Jean-Laurent Fabre de Morlhon
52   * @version $Id: GridDemo.java,v 1.6 2005/09/09 18:24:38 mat007 Exp $
53   */
54  public final class GridDemo
55  {
56      /***
57       * The size of the border around each grid in pixels.
58       */
59      private static final int BORDER_SIZE = 5;
60      /***
61       * The width and height of each grid in tiles.
62       */
63      private static final int NUMBER_OF_HEXAGONS = 10;
64      /***
65       * The size of each hexagon (must be odd for better layout).
66       */
67      private static final int HEXAGON_SIDE_LENGTH = 16;
68  
69      private GridDemo()
70      {
71      }
72  
73      /***
74       * Entry point.
75       *
76       * @param args unused
77       */
78      public static void main( final String[] args )
79      {
80          final JFrame flatFrame = createFlatGridFrame( "Flat Hexagon Grid", false );
81          final JFrame flatShiftedFrame = createFlatGridFrame( "Flat Shifted Hexagon Grid", true );
82          final JFrame peakFrame = createPeakGridFrame( "Peak Hexagon Grid", false );
83          final JFrame peakShiftedFrame = createPeakGridFrame( "Peak Shifted Hexagon Grid", true );
84          final Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
85          final int space = 10;
86          flatFrame.setLocation( dim.width / 2 - space - flatFrame.getWidth(), dim.height / 2 - space
87                  - flatFrame.getHeight() );
88          flatShiftedFrame.setLocation( dim.width / 2 + space, dim.height / 2 - space - flatShiftedFrame.getHeight() );
89          peakFrame.setLocation( dim.width / 2 - space - peakFrame.getWidth(), dim.height / 2 + space );
90          peakShiftedFrame.setLocation( dim.width / 2 + space, dim.height / 2 + space );
91          show( flatFrame, flatShiftedFrame, peakFrame, peakShiftedFrame );
92      }
93  
94      private static JFrame createFlatGridFrame( final String title, final boolean shifted )
95      {
96          final FlatHexagonBuilder builder = new FlatHexagonBuilder( HEXAGON_SIDE_LENGTH, shifted );
97          final IGrid grid = createFlatGrid( shifted );
98          final HexagonCanvas canvas = new HexagonCanvas( grid, builder, BORDER_SIZE );
99          return new GridFrame( title, canvas );
100     }
101 
102     private static IGrid createFlatGrid( final boolean shifted )
103     {
104         if( shifted )
105             return new Grid( NUMBER_OF_HEXAGONS, NUMBER_OF_HEXAGONS, new FlatShiftedHexagonDirectionGroup() );
106         return new Grid( NUMBER_OF_HEXAGONS, NUMBER_OF_HEXAGONS, new FlatHexagonDirectionGroup() );
107     }
108 
109     private static JFrame createPeakGridFrame( final String title, final boolean shifted )
110     {
111         final PeakHexagonBuilder builder = new PeakHexagonBuilder( HEXAGON_SIDE_LENGTH, shifted );
112         final IGrid grid = createPeakGrid( shifted );
113         final HexagonCanvas canvas = new HexagonCanvas( grid, builder, BORDER_SIZE );
114         return new GridFrame( title, canvas );
115     }
116 
117     private static IGrid createPeakGrid( final boolean shifted )
118     {
119         if( shifted )
120             return new Grid( NUMBER_OF_HEXAGONS, NUMBER_OF_HEXAGONS, new PeakShiftedHexagonDirectionGroup() );
121         return new Grid( NUMBER_OF_HEXAGONS, NUMBER_OF_HEXAGONS, new PeakHexagonDirectionGroup() );
122     }
123 
124     private static void show( final JFrame flatFrame, final JFrame flatShiftedFrame, final JFrame peakFrame,
125             final JFrame peakShiftedFrame )
126     {
127         final Runnable showFrame = new Runnable()
128         {
129             public void run()
130             {
131                 flatFrame.show();
132                 flatShiftedFrame.show();
133                 peakFrame.show();
134                 peakShiftedFrame.show();
135             }
136         };
137         try
138         {
139             SwingUtilities.invokeLater( showFrame );
140         }
141         catch( Exception e )
142         {
143             System.out.println( e );
144             e.printStackTrace();
145         }
146     }
147 }