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.Point2D; 032import java.awt.geom.Rectangle2D; 033import org.jfree.chart.util.Args; 034 035/** 036 * Used to indicate an anchor point for a rectangle. 037 */ 038public enum RectangleAnchor { 039 040 /** Center. */ 041 CENTER("RectangleAnchor.CENTER"), 042 043 /** Top. */ 044 TOP("RectangleAnchor.TOP"), 045 046 /** Top-Left. */ 047 TOP_LEFT("RectangleAnchor.TOP_LEFT"), 048 049 /** Top-Right. */ 050 TOP_RIGHT("RectangleAnchor.TOP_RIGHT"), 051 052 /** Bottom. */ 053 BOTTOM("RectangleAnchor.BOTTOM"), 054 055 /** Bottom-Left. */ 056 BOTTOM_LEFT("RectangleAnchor.BOTTOM_LEFT"), 057 058 /** Bottom-Right. */ 059 BOTTOM_RIGHT("RectangleAnchor.BOTTOM_RIGHT"), 060 061 /** Left. */ 062 LEFT("RectangleAnchor.LEFT"), 063 064 /** Right. */ 065 RIGHT("RectangleAnchor.RIGHT"); 066 067 /** The name. */ 068 private final String name; 069 070 /** 071 * Private constructor. 072 * 073 * @param name the name. 074 */ 075 RectangleAnchor(String name) { 076 this.name = name; 077 } 078 079 /** 080 * Returns the anchor point relative to the specified rectangle. 081 * 082 * @param rectangle the rectangle (<code>null</code> not permitted). 083 * 084 * @return The anchor point (never <code>null</code>). 085 */ 086 public Point2D getAnchorPoint(Rectangle2D rectangle) { 087 Args.nullNotPermitted(rectangle, "rectangle"); 088 Point2D result = new Point2D.Double(); 089 if (this == RectangleAnchor.CENTER) { 090 result.setLocation(rectangle.getCenterX(), rectangle.getCenterY()); 091 } else if (this == RectangleAnchor.TOP) { 092 result.setLocation(rectangle.getCenterX(), rectangle.getMinY()); 093 } else if (this == RectangleAnchor.BOTTOM) { 094 result.setLocation(rectangle.getCenterX(), rectangle.getMaxY()); 095 } else if (this == RectangleAnchor.LEFT) { 096 result.setLocation(rectangle.getMinX(), rectangle.getCenterY()); 097 } else if (this == RectangleAnchor.RIGHT) { 098 result.setLocation(rectangle.getMaxX(), rectangle.getCenterY()); 099 } else if (this == RectangleAnchor.TOP_LEFT) { 100 result.setLocation(rectangle.getMinX(), rectangle.getMinY()); 101 } else if (this == RectangleAnchor.TOP_RIGHT) { 102 result.setLocation(rectangle.getMaxX(), rectangle.getMinY()); 103 } else if (this == RectangleAnchor.BOTTOM_LEFT) { 104 result.setLocation(rectangle.getMinX(), rectangle.getMaxY()); 105 } else if (this == RectangleAnchor.BOTTOM_RIGHT) { 106 result.setLocation(rectangle.getMaxX(), rectangle.getMaxY()); 107 } 108 return result; 109 } 110 111 /** 112 * Returns a string representing the object. 113 * 114 * @return The string. 115 */ 116 @Override 117 public String toString() { 118 return this.name; 119 } 120 121 /** 122 * Creates a new rectangle with the specified dimensions that is aligned to 123 * the given anchor point {@code (anchorX, anchorY)}. 124 * 125 * @param dimensions the dimensions ({@code null} not permitted). 126 * @param anchorX the x-anchor. 127 * @param anchorY the y-anchor. 128 * @param anchor the anchor ({@code null} not permitted). 129 * 130 * @return A rectangle. 131 */ 132 public static Rectangle2D createRectangle(Size2D dimensions, 133 double anchorX, double anchorY, RectangleAnchor anchor) { 134 Rectangle2D result = null; 135 double w = dimensions.getWidth(); 136 double h = dimensions.getHeight(); 137 if (anchor == RectangleAnchor.CENTER) { 138 result = new Rectangle2D.Double(anchorX - w / 2.0, 139 anchorY - h / 2.0, w, h); 140 } else if (anchor == RectangleAnchor.TOP) { 141 result = new Rectangle2D.Double(anchorX - w / 2.0, anchorY, w, h); 142 } else if (anchor == RectangleAnchor.BOTTOM) { 143 result = new Rectangle2D.Double(anchorX - w / 2.0, anchorY - h, 144 w, h); 145 } else if (anchor == RectangleAnchor.LEFT) { 146 result = new Rectangle2D.Double(anchorX, anchorY - h / 2.0, w, h); 147 } else if (anchor == RectangleAnchor.RIGHT) { 148 result = new Rectangle2D.Double(anchorX - w, anchorY - h / 2.0, 149 w, h); 150 } else if (anchor == RectangleAnchor.TOP_LEFT) { 151 result = new Rectangle2D.Double(anchorX, anchorY, w, h); 152 } else if (anchor == RectangleAnchor.TOP_RIGHT) { 153 result = new Rectangle2D.Double(anchorX - w, anchorY, w, h); 154 } else if (anchor == RectangleAnchor.BOTTOM_LEFT) { 155 result = new Rectangle2D.Double(anchorX, anchorY - h, w, h); 156 } else if (anchor == RectangleAnchor.BOTTOM_RIGHT) { 157 result = new Rectangle2D.Double(anchorX - w, anchorY - h, w, h); 158 } 159 return result; 160 } 161 162}