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: GridFrame.java,v 1.4 2005/09/10 14:06:22 mat007 Exp $
29   */
30  
31  package jtge.sample.grid;
32  
33  import java.awt.BorderLayout;
34  import java.awt.Color;
35  import java.awt.Point;
36  import javax.swing.JFrame;
37  import javax.swing.JLabel;
38  import javax.swing.JPanel;
39  import javax.swing.WindowConstants;
40  import jtge.util.grid.Coordinate;
41  
42  /***
43   * Implements a frame to display a grid.
44   *
45   * @author Jean-Laurent
46   * @version $Id: GridFrame.java,v 1.4 2005/09/10 14:06:22 mat007 Exp $
47   */
48  public class GridFrame extends JFrame
49  {
50      private static final long serialVersionUID = 1703697166029194834L;
51      /***
52       * The displayed canvas.
53       */
54      private HexagonCanvas canvas;
55      /***
56       * A label at the bottom for displaying mouse-hovered hexagon coordinate.
57       */
58      private JLabel labelInfo;
59  
60      /***
61       * Create a frame for an hexagon canvas.
62       *
63       * @param title the frame title
64       * @param canvas the canvas to display
65       */
66      public GridFrame( final String title, final HexagonCanvas canvas )
67      {
68          super( title );
69          if( canvas == null )
70              throw new IllegalArgumentException( "argument 'canvas' is null" );
71          setDefaultCloseOperation( WindowConstants.DISPOSE_ON_CLOSE );
72          this.canvas = canvas;
73          final JPanel panel = new JPanel();
74          panel.setLayout( new BorderLayout() );
75          labelInfo = new JLabel( " ", JLabel.CENTER );
76          panel.setBackground( Color.YELLOW );
77          panel.add( canvas, BorderLayout.CENTER );
78          panel.add( labelInfo, BorderLayout.SOUTH );
79          setContentPane( panel );
80          final GridFrameMouseAdapter mouseAdapter = new GridFrameMouseAdapter( this );
81          canvas.addMouseMotionListener( mouseAdapter );
82          canvas.addMouseListener( mouseAdapter );
83          pack();
84      }
85  
86      private void setText( final String text )
87      {
88          labelInfo.setText( text );
89      }
90  
91      /***
92       * Display the position within the bottom text of the widget.
93       *
94       * @param position a Point relative to this JFrame
95       */
96      public final void displayPosition( final Point position )
97      {
98          final Coordinate coordinate = canvas.fromPixel( position );
99          if( coordinate != null )
100             setText( "Mouse on coordinate : " + coordinate );
101         else
102             setText( "Mouse out of map bounds" );
103         canvas.select( coordinate );
104     }
105 
106     /***
107      * Empty any position written in the text label.
108      */
109     public final void resetPosition()
110     {
111         setText( "" );
112         canvas.select( null );
113     }
114 }