001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-present, 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.ui; 030 031import java.awt.geom.Rectangle2D; 032 033/** 034 * A utility class for aligning rectangles. 035 */ 036public final class Align { 037 038 /** Center alignment. */ 039 public static final int CENTER = 0x00; 040 041 /** Top alignment. */ 042 public static final int TOP = 0x01; 043 044 /** Bottom alignment. */ 045 public static final int BOTTOM = 0x02; 046 047 /** Left alignment. */ 048 public static final int LEFT = 0x04; 049 050 /** Right alignment. */ 051 public static final int RIGHT = 0x08; 052 053 /** Top/Left alignment. */ 054 public static final int TOP_LEFT = TOP | LEFT; 055 056 /** Top/Right alignment. */ 057 public static final int TOP_RIGHT = TOP | RIGHT; 058 059 /** Bottom/Left alignment. */ 060 public static final int BOTTOM_LEFT = BOTTOM | LEFT; 061 062 /** Bottom/Right alignment. */ 063 public static final int BOTTOM_RIGHT = BOTTOM | RIGHT; 064 065 /** Horizontal fit. */ 066 public static final int FIT_HORIZONTAL = LEFT | RIGHT; 067 068 /** Vertical fit. */ 069 public static final int FIT_VERTICAL = TOP | BOTTOM; 070 071 /** Complete fit. */ 072 public static final int FIT = FIT_HORIZONTAL | FIT_VERTICAL; 073 074 /** North alignment (same as TOP). */ 075 public static final int NORTH = TOP; 076 077 /** South alignment (same as BOTTOM). */ 078 public static final int SOUTH = BOTTOM; 079 080 /** West alignment (same as LEFT). */ 081 public static final int WEST = LEFT; 082 083 /** East alignment (same as RIGHT). */ 084 public static final int EAST = RIGHT; 085 086 /** North/West alignment (same as TOP_LEFT). */ 087 public static final int NORTH_WEST = NORTH | WEST; 088 089 /** North/East alignment (same as TOP_RIGHT). */ 090 public static final int NORTH_EAST = NORTH | EAST; 091 092 /** South/West alignment (same as BOTTOM_LEFT). */ 093 public static final int SOUTH_WEST = SOUTH | WEST; 094 095 /** South/East alignment (same as BOTTOM_RIGHT). */ 096 public static final int SOUTH_EAST = SOUTH | EAST; 097 098 /** 099 * Private constructor. 100 */ 101 private Align() { 102 super(); 103 } 104 105 /** 106 * Aligns one rectangle ({@code rect}) relative to another rectangle ({@code frame}). 107 * 108 * @param rect the rectangle to be aligned ({@code null} not permitted). 109 * @param frame the reference frame ({@code null} not permitted). 110 * @param align the alignment code. 111 */ 112 public static void align(Rectangle2D rect, Rectangle2D frame, int align) { 113 114 double x = frame.getCenterX() - rect.getWidth() / 2.0; 115 double y = frame.getCenterY() - rect.getHeight() / 2.0; 116 double w = rect.getWidth(); 117 double h = rect.getHeight(); 118 119 if ((align & FIT_VERTICAL) == FIT_VERTICAL) { 120 h = frame.getHeight(); 121 } 122 123 if ((align & FIT_HORIZONTAL) == FIT_HORIZONTAL) { 124 w = frame.getWidth(); 125 } 126 127 if ((align & TOP) == TOP) { 128 y = frame.getMinY(); 129 } 130 131 if ((align & BOTTOM) == BOTTOM) { 132 y = frame.getMaxY() - h; 133 } 134 135 if ((align & LEFT) == LEFT) { 136 x = frame.getX(); 137 } 138 139 if ((align & RIGHT) == RIGHT) { 140 x = frame.getMaxX() - w; 141 } 142 143 rect.setRect(x, y, w, h); 144 145 } 146 147} 148