KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > ui > Align


1 /* ========================================================================
2  * JCommon : a free general purpose class library for the Java(tm) platform
3  * ========================================================================
4  *
5  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jcommon/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * ----------
28  * Align.java
29  * ----------
30  * (C) Copyright 2000-2004, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: Christian W. Zuckschwerdt;
33  * Contributor(s): David Gilbert (for Object Refinery Limited);
34  *
35  * $Id: Align.java,v 1.5 2005/10/18 13:18:34 mungady Exp $
36  *
37  * Changes (from 30-May-2002)
38  * --------------------------
39  * 30-May-2002 : Added title (DG);
40  * 13-Oct-2002 : Fixed errors reported by Checkstyle (DG);
41  *
42  */

43
44 package org.jfree.ui;
45
46 import java.awt.geom.Rectangle2D JavaDoc;
47
48 /**
49  * A utility class for aligning rectangles.
50  *
51  * @author David Gilbert
52  */

53 public final class Align {
54
55     /** Center alignment. */
56     public static final int CENTER = 0x00;
57
58     /** Top alignment. */
59     public static final int TOP = 0x01;
60
61     /** Bottom alignment. */
62     public static final int BOTTOM = 0x02;
63
64     /** Left alignment. */
65     public static final int LEFT = 0x04;
66
67     /** Right alignment. */
68     public static final int RIGHT = 0x08;
69
70     /** Top/Left alignment. */
71     public static final int TOP_LEFT = TOP | LEFT;
72
73     /** Top/Right alignment. */
74     public static final int TOP_RIGHT = TOP | RIGHT;
75
76     /** Bottom/Left alignment. */
77     public static final int BOTTOM_LEFT = BOTTOM | LEFT;
78
79     /** Bottom/Right alignment. */
80     public static final int BOTTOM_RIGHT = BOTTOM | RIGHT;
81
82     /** Horizontal fit. */
83     public static final int FIT_HORIZONTAL = LEFT | RIGHT;
84
85     /** Vertical fit. */
86     public static final int FIT_VERTICAL = TOP | BOTTOM;
87
88     /** Complete fit. */
89     public static final int FIT = FIT_HORIZONTAL | FIT_VERTICAL;
90
91     /** North alignment (same as TOP). */
92     public static final int NORTH = TOP;
93
94     /** South alignment (same as BOTTOM). */
95     public static final int SOUTH = BOTTOM;
96
97     /** West alignment (same as LEFT). */
98     public static final int WEST = LEFT;
99
100     /** East alignment (same as RIGHT). */
101     public static final int EAST = RIGHT;
102
103     /** North/West alignment (same as TOP_LEFT). */
104     public static final int NORTH_WEST = NORTH | WEST;
105
106     /** North/East alignment (same as TOP_RIGHT). */
107     public static final int NORTH_EAST = NORTH | EAST;
108
109     /** South/West alignment (same as BOTTOM_LEFT). */
110     public static final int SOUTH_WEST = SOUTH | WEST;
111
112     /** South/East alignment (same as BOTTOM_RIGHT). */
113     public static final int SOUTH_EAST = SOUTH | EAST;
114
115     /**
116      * Private constructor.
117      */

118     private Align() {
119         super();
120     }
121     
122     /**
123      * Aligns one rectangle (<code>rect</code>) relative to another rectangle (<code>frame</code>).
124      *
125      * @param rect the rectangle to be aligned (<code>null</code> not permitted).
126      * @param frame the reference frame (<code>null</code> not permitted).
127      * @param align the alignment code.
128      */

129     public static void align(final Rectangle2D JavaDoc rect, final Rectangle2D JavaDoc frame, final int align) {
130
131         double x = frame.getCenterX() - rect.getWidth() / 2.0;
132         double y = frame.getCenterY() - rect.getHeight() / 2.0;
133         double w = rect.getWidth();
134         double h = rect.getHeight();
135
136         if ((align & FIT_VERTICAL) == FIT_VERTICAL) {
137             h = frame.getHeight();
138         }
139
140         if ((align & FIT_HORIZONTAL) == FIT_HORIZONTAL) {
141             w = frame.getWidth();
142         }
143
144         if ((align & TOP) == TOP) {
145             y = frame.getMinY();
146         }
147
148         if ((align & BOTTOM) == BOTTOM) {
149             y = frame.getMaxY() - h;
150         }
151
152         if ((align & LEFT) == LEFT) {
153             x = frame.getX();
154         }
155
156         if ((align & RIGHT) == RIGHT) {
157             x = frame.getMaxX() - w;
158         }
159
160         rect.setRect(x, y, w, h);
161
162     }
163
164 }
165
Popular Tags