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