1 7 8 package java.awt.geom; 9 10 import java.awt.Shape ; 11 import java.awt.Rectangle ; 12 13 26 public abstract class RectangularShape implements Shape , Cloneable { 27 35 protected RectangularShape() { 36 } 37 38 44 public abstract double getX(); 45 46 52 public abstract double getY(); 53 54 59 public abstract double getWidth(); 60 61 66 public abstract double getHeight(); 67 68 75 public double getMinX() { 76 return getX(); 77 } 78 79 86 public double getMinY() { 87 return getY(); 88 } 89 90 97 public double getMaxX() { 98 return getX() + getWidth(); 99 } 100 101 108 public double getMaxY() { 109 return getY() + getHeight(); 110 } 111 112 119 public double getCenterX() { 120 return getX() + getWidth() / 2.0; 121 } 122 123 130 public double getCenterY() { 131 return getY() + getHeight() / 2.0; 132 } 133 134 143 public Rectangle2D getFrame() { 144 return new Rectangle2D.Double (getX(), getY(), getWidth(), getHeight()); 145 } 146 147 154 public abstract boolean isEmpty(); 155 156 167 public abstract void setFrame(double x, double y, double w, double h); 168 169 179 public void setFrame(Point2D loc, Dimension2D size) { 180 setFrame(loc.getX(), loc.getY(), size.getWidth(), size.getHeight()); 181 } 182 183 191 public void setFrame(Rectangle2D r) { 192 setFrame(r.getX(), r.getY(), r.getWidth(), r.getHeight()); 193 } 194 195 203 public void setFrameFromDiagonal(double x1, double y1, 204 double x2, double y2) { 205 if (x2 < x1) { 206 double t = x1; 207 x1 = x2; 208 x2 = t; 209 } 210 if (y2 < y1) { 211 double t = y1; 212 y1 = y2; 213 y2 = t; 214 } 215 setFrame(x1, y1, x2 - x1, y2 - y1); 216 } 217 218 225 public void setFrameFromDiagonal(Point2D p1, Point2D p2) { 226 setFrameFromDiagonal(p1.getX(), p1.getY(), p2.getX(), p2.getY()); 227 } 228 229 237 public void setFrameFromCenter(double centerX, double centerY, 238 double cornerX, double cornerY) { 239 double halfW = Math.abs(cornerX - centerX); 240 double halfH = Math.abs(cornerY - centerY); 241 setFrame(centerX - halfW, centerY - halfH, halfW * 2.0, halfH * 2.0); 242 } 243 244 252 public void setFrameFromCenter(Point2D center, Point2D corner) { 253 setFrameFromCenter(center.getX(), center.getY(), 254 corner.getX(), corner.getY()); 255 } 256 257 265 public boolean contains(Point2D p) { 266 return contains(p.getX(), p.getY()); 267 } 268 269 277 public boolean intersects(Rectangle2D r) { 278 return intersects(r.getX(), r.getY(), r.getWidth(), r.getHeight()); 279 } 280 281 289 public boolean contains(Rectangle2D r) { 290 return contains(r.getX(), r.getY(), r.getWidth(), r.getHeight()); 291 } 292 293 298 public Rectangle getBounds() { 299 double width = getWidth(); 300 double height = getHeight(); 301 if (width < 0 || height < 0) { 302 return new Rectangle (); 303 } 304 double x = getX(); 305 double y = getY(); 306 double x1 = Math.floor(x); 307 double y1 = Math.floor(y); 308 double x2 = Math.ceil(x + width); 309 double y2 = Math.ceil(y + height); 310 return new Rectangle ((int) x1, (int) y1, 311 (int) (x2 - x1), (int) (y2 - y1)); 312 } 313 314 339 public PathIterator getPathIterator(AffineTransform at, double flatness) { 340 return new FlatteningPathIterator (getPathIterator(at), flatness); 341 } 342 343 351 public Object clone() { 352 try { 353 return super.clone(); 354 } catch (CloneNotSupportedException e) { 355 throw new InternalError (); 357 } 358 } 359 } 360 | Popular Tags |