1 45 46 package org.jfree.ui; 47 48 import java.awt.geom.Point2D ; 49 import java.awt.geom.Rectangle2D ; 50 import java.io.ObjectStreamException ; 51 import java.io.Serializable ; 52 53 58 public final class RectangleAnchor implements Serializable { 59 60 61 private static final long serialVersionUID = -2457494205644416327L; 62 63 64 public static final RectangleAnchor CENTER 65 = new RectangleAnchor("RectangleAnchor.CENTER"); 66 67 68 public static final RectangleAnchor TOP 69 = new RectangleAnchor("RectangleAnchor.TOP"); 70 71 72 public static final RectangleAnchor TOP_LEFT 73 = new RectangleAnchor("RectangleAnchor.TOP_LEFT"); 74 75 76 public static final RectangleAnchor TOP_RIGHT 77 = new RectangleAnchor("RectangleAnchor.TOP_RIGHT"); 78 79 80 public static final RectangleAnchor BOTTOM 81 = new RectangleAnchor("RectangleAnchor.BOTTOM"); 82 83 84 public static final RectangleAnchor BOTTOM_LEFT 85 = new RectangleAnchor("RectangleAnchor.BOTTOM_LEFT"); 86 87 88 public static final RectangleAnchor BOTTOM_RIGHT 89 = new RectangleAnchor("RectangleAnchor.BOTTOM_RIGHT"); 90 91 92 public static final RectangleAnchor LEFT 93 = new RectangleAnchor("RectangleAnchor.LEFT"); 94 95 96 public static final RectangleAnchor RIGHT 97 = new RectangleAnchor("RectangleAnchor.RIGHT"); 98 99 100 private String name; 101 102 107 private RectangleAnchor(final String name) { 108 this.name = name; 109 } 110 111 116 public String toString() { 117 return this.name; 118 } 119 120 128 public boolean equals(final Object obj) { 129 130 if (this == obj) { 131 return true; 132 } 133 if (!(obj instanceof RectangleAnchor)) { 134 return false; 135 } 136 137 final RectangleAnchor order = (RectangleAnchor) obj; 138 if (!this.name.equals(order.name)) { 139 return false; 140 } 141 142 return true; 143 } 144 145 150 public int hashCode() { 151 return this.name.hashCode(); 152 } 153 154 162 public static Point2D coordinates(final Rectangle2D rectangle, 163 final RectangleAnchor anchor) { 164 Point2D result = new Point2D.Double (); 165 if (anchor == RectangleAnchor.CENTER) { 166 result.setLocation(rectangle.getCenterX(), rectangle.getCenterY()); 167 } 168 else if (anchor == RectangleAnchor.TOP) { 169 result.setLocation(rectangle.getCenterX(), rectangle.getMinY()); 170 } 171 else if (anchor == RectangleAnchor.BOTTOM) { 172 result.setLocation(rectangle.getCenterX(), rectangle.getMaxY()); 173 } 174 else if (anchor == RectangleAnchor.LEFT) { 175 result.setLocation(rectangle.getMinX(), rectangle.getCenterY()); 176 } 177 else if (anchor == RectangleAnchor.RIGHT) { 178 result.setLocation(rectangle.getMaxX(), rectangle.getCenterY()); 179 } 180 else if (anchor == RectangleAnchor.TOP_LEFT) { 181 result.setLocation(rectangle.getMinX(), rectangle.getMinY()); 182 } 183 else if (anchor == RectangleAnchor.TOP_RIGHT) { 184 result.setLocation(rectangle.getMaxX(), rectangle.getMinY()); 185 } 186 else if (anchor == RectangleAnchor.BOTTOM_LEFT) { 187 result.setLocation(rectangle.getMinX(), rectangle.getMaxY()); 188 } 189 else if (anchor == RectangleAnchor.BOTTOM_RIGHT) { 190 result.setLocation(rectangle.getMaxX(), rectangle.getMaxY()); 191 } 192 return result; 193 } 194 195 206 public static Rectangle2D createRectangle(final Size2D dimensions, 207 final double anchorX, 208 final double anchorY, 209 final RectangleAnchor anchor) { 210 Rectangle2D result = null; 211 final double w = dimensions.getWidth(); 212 final double h = dimensions.getHeight(); 213 if (anchor == RectangleAnchor.CENTER) { 214 result = new Rectangle2D.Double ( 215 anchorX - w / 2.0, anchorY - h / 2.0, w, h 216 ); 217 } 218 else if (anchor == RectangleAnchor.TOP) { 219 result = new Rectangle2D.Double ( 220 anchorX - w / 2.0, anchorY - h / 2.0, w, h 221 ); 222 } 223 else if (anchor == RectangleAnchor.BOTTOM) { 224 result = new Rectangle2D.Double ( 225 anchorX - w / 2.0, anchorY - h / 2.0, w, h 226 ); 227 } 228 else if (anchor == RectangleAnchor.LEFT) { 229 result = new Rectangle2D.Double ( 230 anchorX, anchorY - h / 2.0, w, h 231 ); 232 } 233 else if (anchor == RectangleAnchor.RIGHT) { 234 result = new Rectangle2D.Double ( 235 anchorX - w, anchorY - h / 2.0, w, h 236 ); 237 } 238 else if (anchor == RectangleAnchor.TOP_LEFT) { 239 result = new Rectangle2D.Double ( 240 anchorX - w / 2.0, anchorY - h / 2.0, w, h 241 ); 242 } 243 else if (anchor == RectangleAnchor.TOP_RIGHT) { 244 result = new Rectangle2D.Double ( 245 anchorX - w / 2.0, anchorY - h / 2.0, w, h 246 ); 247 } 248 else if (anchor == RectangleAnchor.BOTTOM_LEFT) { 249 result = new Rectangle2D.Double ( 250 anchorX - w / 2.0, anchorY - h / 2.0, w, h 251 ); 252 } 253 else if (anchor == RectangleAnchor.BOTTOM_RIGHT) { 254 result = new Rectangle2D.Double ( 255 anchorX - w / 2.0, anchorY - h / 2.0, w, h 256 ); 257 } 258 return result; 259 } 260 261 268 private Object readResolve() throws ObjectStreamException { 269 RectangleAnchor result = null; 270 if (this.equals(RectangleAnchor.CENTER)) { 271 result = RectangleAnchor.CENTER; 272 } 273 else if (this.equals(RectangleAnchor.TOP)) { 274 result = RectangleAnchor.TOP; 275 } 276 else if (this.equals(RectangleAnchor.BOTTOM)) { 277 result = RectangleAnchor.BOTTOM; 278 } 279 else if (this.equals(RectangleAnchor.LEFT)) { 280 result = RectangleAnchor.LEFT; 281 } 282 else if (this.equals(RectangleAnchor.RIGHT)) { 283 result = RectangleAnchor.RIGHT; 284 } 285 else if (this.equals(RectangleAnchor.TOP_LEFT)) { 286 result = RectangleAnchor.TOP_LEFT; 287 } 288 else if (this.equals(RectangleAnchor.TOP_RIGHT)) { 289 result = RectangleAnchor.TOP_RIGHT; 290 } 291 else if (this.equals(RectangleAnchor.BOTTOM_LEFT)) { 292 result = RectangleAnchor.BOTTOM_LEFT; 293 } 294 else if (this.equals(RectangleAnchor.BOTTOM_RIGHT)) { 295 result = RectangleAnchor.BOTTOM_RIGHT; 296 } 297 return result; 298 } 299 300 } 301 | Popular Tags |