    001    /* ========================================================================
    002     * JCommon : a free general purpose class library for the Java(tm) platform
    003     * ========================================================================
    004     *
    005     * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
    006     *
    007     * Project Info:  http://www.jfree.org/jcommon/index.html
    008     *
    009     * This library is free software; you can redistribute it and/or modify it
    010     * under the terms of the GNU Lesser General Public License as published by
    011     * the Free Software Foundation; either version 2.1 of the License, or
    012     * (at your option) any later version.
    013     *
    014     * This library is distributed in the hope that it will be useful, but
    015     * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    016     * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
    017     * License for more details.
    018     *
    019     * You should have received a copy of the GNU Lesser General Public
    020     * License along with this library; if not, write to the Free Software
    021     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
    022     * USA.
    023     *
    024     * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
    025     * in the United States and other countries.]
    026     *
    027     * ----------
    028     * Align.java
    029     * ----------
    030     * (C) Copyright 2000-2004, by Object Refinery Limited and Contributors.
    031     *
    032     * Original Author:  Christian W. Zuckschwerdt;
    033     * Contributor(s):   David Gilbert (for Object Refinery Limited);
    034     *
    035     * $Id: Align.java,v 1.5 2005/10/18 13:18:34 mungady Exp $
    036     *
    037     * Changes (from 30-May-2002)
    038     * --------------------------
    039     * 30-May-2002 : Added title (DG);
    040     * 13-Oct-2002 : Fixed errors reported by Checkstyle (DG);
    041     *
    042     */
    043
    044    package org.jfree.ui;
    045
    046    import java.awt.geom.Rectangle2D;
    047
    048    /**
    049     * A utility class for aligning rectangles.
    050     *
    051     * @author David Gilbert
    052     */
    053    public final class Align {
    054
    055        /** Center alignment. */
    056        public static final int CENTER = 0x00;
    057
    058        /** Top alignment. */
    059        public static final int TOP = 0x01;
    060
    061        /** Bottom alignment. */
    062        public static final int BOTTOM = 0x02;
    063
    064        /** Left alignment. */
    065        public static final int LEFT = 0x04;
    066
    067        /** Right alignment. */
    068        public static final int RIGHT = 0x08;
    069
    070        /** Top/Left alignment. */
    071        public static final int TOP_LEFT = TOP | LEFT;
    072
    073        /** Top/Right alignment. */
    074        public static final int TOP_RIGHT = TOP | RIGHT;
    075
    076        /** Bottom/Left alignment. */
    077        public static final int BOTTOM_LEFT = BOTTOM | LEFT;
    078
    079        /** Bottom/Right alignment. */
    080        public static final int BOTTOM_RIGHT = BOTTOM | RIGHT;
    081
    082        /** Horizontal fit. */
    083        public static final int FIT_HORIZONTAL = LEFT | RIGHT;
    084
    085        /** Vertical fit. */
    086        public static final int FIT_VERTICAL = TOP | BOTTOM;
    087
    088        /** Complete fit. */
    089        public static final int FIT = FIT_HORIZONTAL | FIT_VERTICAL;
    090
    091        /** North alignment (same as TOP). */
    092        public static final int NORTH = TOP;
    093
    094        /** South alignment (same as BOTTOM). */
    095        public static final int SOUTH = BOTTOM;
    096
    097        /** West alignment (same as LEFT). */
    098        public static final int WEST = LEFT;
    099
    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 rect, final Rectangle2D 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    }