001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2022, by David Gilbert and Contributors.
006 *
007 * Project Info:  http://www.jfree.org/jfreechart/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 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 */
028
029package org.jfree.chart.api;
030
031import java.awt.geom.Rectangle2D;
032import org.jfree.chart.internal.Args;
033    
034/**
035 * Used to indicate how to align one rectangle with another rectangle.
036 */
037public enum RectangleAlignment {
038
039    /** Fill the frame */
040    FILL,
041    
042    /** Fill the height of the frame. */
043    FILL_VERTICAL,
044    
045    /** Fill the width of the frame. */
046    FILL_HORIZONTAL,
047    
048    /** Align to the top left of the frame. */
049    TOP_LEFT,
050    
051    /** Align to the top of the frame. */
052    TOP_CENTER, 
053    
054    /** Align to the top right of the frame. */
055    TOP_RIGHT, 
056    
057    /** Align to the left side of the frame, centered vertically. */
058    CENTER_LEFT,
059
060    /** Align to the center of the frame. */
061    CENTER,
062
063    /** Align to the right side of the frame, centered vertically. */    
064    CENTER_RIGHT,
065    
066    /** Align to the bottom left of the frame. */    
067    BOTTOM_LEFT,
068    
069    /** Align to the bottom of the frame. */    
070    BOTTOM_CENTER,
071    
072    /** Align to the bottom right of the frame. */    
073    BOTTOM_RIGHT;
074
075    /**
076     * Returns the anchor point relative to the specified rectangle.
077     * 
078     * @param rect  the rectangle to align ({@code null} not permitted).
079     * @param frame  the frame to align with ({@code null} not permitted).
080     */
081    public void align(Rectangle2D rect, Rectangle2D frame) {
082        Args.nullNotPermitted(rect, "rect");
083        Args.nullNotPermitted(frame, "frame");
084        double x = rect.getX();
085        double y = rect.getY();
086        double w = rect.getWidth();
087        double h = rect.getHeight();
088
089        switch (this) {
090            case BOTTOM_CENTER:
091                x = frame.getCenterX() - rect.getWidth() / 2.0;
092                y = frame.getMaxY() - h;
093                break;
094            case BOTTOM_LEFT:
095                x = frame.getX();
096                y = frame.getMaxY() - h;
097                break;
098            case BOTTOM_RIGHT:
099                x = frame.getMaxX() - w;
100                y = frame.getMaxY() - h;
101                break;
102            case CENTER:
103                x = frame.getCenterX() - rect.getWidth() / 2.0;
104                y = frame.getCenterY() - rect.getHeight() / 2.0;
105                break;
106            case FILL:
107                x = frame.getX();
108                y = frame.getY();
109                w = frame.getWidth();
110                h = frame.getHeight();
111                break;
112            case FILL_HORIZONTAL:
113                x = frame.getX();
114                w = frame.getWidth();
115                break;
116            case FILL_VERTICAL:
117                y = frame.getY();
118                h = frame.getHeight();
119                break;
120            case CENTER_LEFT:
121                x = frame.getX(); 
122                y = frame.getCenterY() - rect.getHeight() / 2.0;
123                break;
124            case CENTER_RIGHT:
125                x = frame.getMaxX() - w;                
126                y = frame.getCenterY() - rect.getHeight() / 2.0;
127                break;
128            case TOP_CENTER:
129                x = frame.getCenterX() - rect.getWidth() / 2.0;
130                y = frame.getY();                
131                break;
132            case TOP_LEFT:
133                x = frame.getX();
134                y = frame.getY();                
135                break;
136            case TOP_RIGHT:
137                x = frame.getMaxX() - w; 
138                y = frame.getY();                
139                break;
140            default:
141                throw new IllegalStateException("Unexpected RectangleAlignment value");
142        }
143        rect.setRect(x, y, w, h);
144    }
145    
146}