View Javadoc

1   /*
2   @See License.txt@
3    */
4   
5   package ui;
6   
7   import java.awt.*;
8   import javax.swing.*;
9   
10  /***
11   * This class contains foreign methods for determining Point calculations.
12   * These should really be part of Toolkit.
13   *
14   * @author Barrie Treloar
15   */
16  public class PointUtilities
17  {
18      private PointUtilities()
19      {
20      }
21  
22      /***
23       * Returns the Point that will position the specified component on the
24       * center of the screen.  The position is where the top left hand corner
25       * needs to be located so that the component is in the screen center.
26       *
27       * @param toBeCentered the Component to center on the screen.
28       * @return the Point that will position the component so it is centered on
29       *         the screen.
30       */
31      public static Point centeredOnScreen( Component toBeCentered )
32      {
33          Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
34          Point screenCenter = new Point();
35          double x = screenSize.getWidth() / 2;
36          double y = screenSize.getHeight() / 2;
37          screenCenter.setLocation( x, y );
38          return centeredOnPoint( screenCenter, toBeCentered );
39      }
40  
41      /***
42       * Returns the screen coordinates for a Point that will position the specified
43       * component in the center of another component.  The screen coordinate space
44       * is used incase the size of the component to center is larger than the component
45       * being centered on.
46       *
47       * @param centeredOn the Component whose center is used as the point to center on.
48       * @param toBeCentered the Component that will be centered around the center point.
49       */
50      public static Point centeredOnComponent( Component centeredOn, Component toBeCentered )
51      {
52          Dimension centeredOnSize = getSize( centeredOn );
53          Point centerPoint = new Point();
54          double x = centeredOnSize.getWidth() / 2;
55          double y = centeredOnSize.getHeight() / 2;
56          centerPoint.setLocation( x, y );
57          SwingUtilities.convertPointToScreen( centerPoint, centeredOn );
58          return centeredOnPoint( centerPoint, toBeCentered );
59      }
60  
61      /***
62       * Returns the screen coordinates for a Point that will position the specified
63       * component such that it is centered on the center Point.
64       *
65       * @param centerPoint the center point in screen coordinates
66       * @param toBeCentered the Component to center around the center point
67       */
68      public static Point centeredOnPoint( Point centerPoint, Component toBeCentered )
69      {
70          Point result = new Point();
71          Dimension componentSize = getSize( toBeCentered );
72          double x = centerPoint.getX() - componentSize.getWidth() / 2;
73          double y = centerPoint.getY() - componentSize.getHeight() / 2;
74          result.setLocation( x, y );
75          return result;
76      }
77  
78      /***
79       * Returns the size of the component, or if the size is not yet set then the
80       * preferredSize of the component.
81       *
82       * @param c the Component to get the size for
83       * @return the size of the Component
84       */
85      private static Dimension getSize( Component c )
86      {
87          Dimension result;
88          result = c.getSize();
89          if ( result.width == 0 )
90          {
91              result = c.getPreferredSize();
92          }
93          return result;
94      }
95  }
96