1 7 8 package java.awt.geom; 9 10 22 public abstract class Ellipse2D extends RectangularShape { 23 27 public static class Float extends Ellipse2D { 28 32 public float x; 33 34 38 public float y; 39 40 43 public float width; 44 45 48 public float height; 49 50 54 public Float() { 55 } 56 57 64 public Float(float x, float y, float w, float h) { 65 setFrame(x, y, w, h); 66 } 67 68 74 public double getX() { 75 return (double) x; 76 } 77 78 84 public double getY() { 85 return (double) y; 86 } 87 88 93 public double getWidth() { 94 return (double) width; 95 } 96 97 102 public double getHeight() { 103 return (double) height; 104 } 105 106 112 public boolean isEmpty() { 113 return (width <= 0.0 || height <= 0.0); 114 } 115 116 126 public void setFrame(float x, float y, float w, float h) { 127 this.x = x; 128 this.y = y; 129 this.width = w; 130 this.height = h; 131 } 132 133 143 public void setFrame(double x, double y, double w, double h) { 144 this.x = (float) x; 145 this.y = (float) y; 146 this.width = (float) w; 147 this.height = (float) h; 148 } 149 150 156 public Rectangle2D getBounds2D() { 157 return new Rectangle2D.Float (x, y, width, height); 158 } 159 } 160 161 165 public static class Double extends Ellipse2D { 166 170 public double x; 171 172 176 public double y; 177 178 181 public double width; 182 183 186 public double height; 187 188 192 public Double() { 193 } 194 195 202 public Double(double x, double y, double w, double h) { 203 setFrame(x, y, w, h); 204 } 205 206 212 public double getX() { 213 return x; 214 } 215 216 222 public double getY() { 223 return y; 224 } 225 226 231 public double getWidth() { 232 return width; 233 } 234 235 240 public double getHeight() { 241 return height; 242 } 243 244 251 public boolean isEmpty() { 252 return (width <= 0.0 || height <= 0.0); 253 } 254 255 265 public void setFrame(double x, double y, double w, double h) { 266 this.x = x; 267 this.y = y; 268 this.width = w; 269 this.height = h; 270 } 271 272 278 public Rectangle2D getBounds2D() { 279 return new Rectangle2D.Double (x, y, width, height); 280 } 281 } 282 283 293 protected Ellipse2D() { 294 } 295 296 303 public boolean contains(double x, double y) { 304 double ellw = getWidth(); 307 if (ellw <= 0.0) { 308 return false; 309 } 310 double normx = (x - getX()) / ellw - 0.5; 311 double ellh = getHeight(); 312 if (ellh <= 0.0) { 313 return false; 314 } 315 double normy = (y - getY()) / ellh - 0.5; 316 return (normx * normx + normy * normy) < 0.25; 317 } 318 319 329 public boolean intersects(double x, double y, double w, double h) { 330 if (w <= 0.0 || h <= 0.0) { 331 return false; 332 } 333 double ellw = getWidth(); 336 if (ellw <= 0.0) { 337 return false; 338 } 339 double normx0 = (x - getX()) / ellw - 0.5; 340 double normx1 = normx0 + w / ellw; 341 double ellh = getHeight(); 342 if (ellh <= 0.0) { 343 return false; 344 } 345 double normy0 = (y - getY()) / ellh - 0.5; 346 double normy1 = normy0 + h / ellh; 347 double nearx, neary; 351 if (normx0 > 0.0) { 352 nearx = normx0; 354 } else if (normx1 < 0.0) { 355 nearx = normx1; 357 } else { 358 nearx = 0.0; 359 } 360 if (normy0 > 0.0) { 361 neary = normy0; 363 } else if (normy1 < 0.0) { 364 neary = normy1; 366 } else { 367 neary = 0.0; 368 } 369 return (nearx * nearx + neary * neary) < 0.25; 370 } 371 372 382 public boolean contains(double x, double y, double w, double h) { 383 return (contains(x, y) && 384 contains(x + w, y) && 385 contains(x, y + h) && 386 contains(x + w, y + h)); 387 } 388 389 404 public PathIterator getPathIterator(AffineTransform at) { 405 return new EllipseIterator (this, at); 406 } 407 } 408 | Popular Tags |