1 7 8 package java.awt.geom; 9 10 24 public abstract class RoundRectangle2D extends RectangularShape { 25 29 public static class Float extends RoundRectangle2D { 30 33 public float x; 34 35 38 public float y; 39 40 43 public float width; 44 45 48 public float height; 49 50 53 public float arcwidth; 54 55 58 public float archeight; 59 60 65 public Float() { 66 } 67 68 82 public Float(float x, float y, float w, float h, 83 float arcw, float arch) { 84 setRoundRect(x, y, w, h, arcw, arch); 85 } 86 87 92 public double getX() { 93 return (double) x; 94 } 95 96 101 public double getY() { 102 return (double) y; 103 } 104 105 110 public double getWidth() { 111 return (double) width; 112 } 113 114 119 public double getHeight() { 120 return (double) height; 121 } 122 123 128 public double getArcWidth() { 129 return (double) arcwidth; 130 } 131 132 137 public double getArcHeight() { 138 return (double) archeight; 139 } 140 141 147 public boolean isEmpty() { 148 return (width <= 0.0f) || (height <= 0.0f); 149 } 150 151 166 public void setRoundRect(float x, float y, float w, float h, 167 float arcw, float arch) { 168 this.x = x; 169 this.y = y; 170 this.width = w; 171 this.height = h; 172 this.arcwidth = arcw; 173 this.archeight = arch; 174 } 175 176 191 public void setRoundRect(double x, double y, double w, double h, 192 double arcw, double arch) { 193 this.x = (float) x; 194 this.y = (float) y; 195 this.width = (float) w; 196 this.height = (float) h; 197 this.arcwidth = (float) arcw; 198 this.archeight = (float) arch; 199 } 200 201 206 public void setRoundRect(RoundRectangle2D rr) { 207 this.x = (float) rr.getX(); 208 this.y = (float) rr.getY(); 209 this.width = (float) rr.getWidth(); 210 this.height = (float) rr.getHeight(); 211 this.arcwidth = (float) rr.getArcWidth(); 212 this.archeight = (float) rr.getArcHeight(); 213 } 214 215 221 public Rectangle2D getBounds2D() { 222 return new Rectangle2D.Float (x, y, width, height); 223 } 224 } 225 226 230 public static class Double extends RoundRectangle2D { 231 234 public double x; 235 236 239 public double y; 240 241 244 public double width; 245 246 249 public double height; 250 251 254 public double arcwidth; 255 256 259 public double archeight; 260 261 266 public Double() { 267 } 268 269 283 public Double(double x, double y, double w, double h, 284 double arcw, double arch) { 285 setRoundRect(x, y, w, h, arcw, arch); 286 } 287 288 293 public double getX() { 294 return x; 295 } 296 297 302 public double getY() { 303 return y; 304 } 305 306 311 public double getWidth() { 312 return width; 313 } 314 315 320 public double getHeight() { 321 return height; 322 } 323 324 329 public double getArcWidth() { 330 return arcwidth; 331 } 332 333 338 public double getArcHeight() { 339 return archeight; 340 } 341 342 348 public boolean isEmpty() { 349 return (width <= 0.0f) || (height <= 0.0f); 350 } 351 352 367 public void setRoundRect(double x, double y, double w, double h, 368 double arcw, double arch) { 369 this.x = x; 370 this.y = y; 371 this.width = w; 372 this.height = h; 373 this.arcwidth = arcw; 374 this.archeight = arch; 375 } 376 377 382 public void setRoundRect(RoundRectangle2D rr) { 383 this.x = rr.getX(); 384 this.y = rr.getY(); 385 this.width = rr.getWidth(); 386 this.height = rr.getHeight(); 387 this.arcwidth = rr.getArcWidth(); 388 this.archeight = rr.getArcHeight(); 389 } 390 391 397 public Rectangle2D getBounds2D() { 398 return new Rectangle2D.Double (x, y, width, height); 399 } 400 } 401 402 412 protected RoundRectangle2D() { 413 } 414 415 420 public abstract double getArcWidth(); 421 422 427 public abstract double getArcHeight(); 428 429 444 public abstract void setRoundRect(double x, double y, double w, double h, 445 double arcWidth, double arcHeight); 446 447 452 public void setRoundRect(RoundRectangle2D rr) { 453 setRoundRect(rr.getX(), rr.getY(), rr.getWidth(), rr.getHeight(), 454 rr.getArcWidth(), rr.getArcHeight()); 455 } 456 457 467 public void setFrame(double x, double y, double w, double h) { 468 setRoundRect(x, y, w, h, getArcWidth(), getArcHeight()); 469 } 470 471 479 public boolean contains(double x, double y) { 480 if (isEmpty()) { 481 return false; 482 } 483 double rrx0 = getX(); 484 double rry0 = getY(); 485 double rrx1 = rrx0 + getWidth(); 486 double rry1 = rry0 + getHeight(); 487 if (x < rrx0 || y < rry0 || x >= rrx1 || y >= rry1) { 489 return false; 490 } 491 double aw = Math.min(getWidth(), Math.abs(getArcWidth())) / 2.0; 492 double ah = Math.min(getHeight(), Math.abs(getArcHeight())) / 2.0; 493 if (x >= (rrx0 += aw) && x < (rrx0 = rrx1 - aw)) { 496 return true; 497 } 498 if (y >= (rry0 += ah) && y < (rry0 = rry1 - ah)) { 499 return true; 500 } 501 x = (x - rrx0) / aw; 502 y = (y - rry0) / ah; 503 return (x * x + y * y <= 1.0); 504 } 505 506 private int classify(double coord, double left, double right, 507 double arcsize) { 508 if (coord < left) { 509 return 0; 510 } else if (coord < left + arcsize) { 511 return 1; 512 } else if (coord < right - arcsize) { 513 return 2; 514 } else if (coord < right) { 515 return 3; 516 } else { 517 return 4; 518 } 519 } 520 521 535 public boolean intersects(double x, double y, double w, double h) { 536 if (isEmpty() || w <= 0 || h <= 0) { 537 return false; 538 } 539 double rrx0 = getX(); 540 double rry0 = getY(); 541 double rrx1 = rrx0 + getWidth(); 542 double rry1 = rry0 + getHeight(); 543 if (x + w <= rrx0 || x >= rrx1 || y + h <= rry0 || y >= rry1) { 545 return false; 546 } 547 double aw = Math.min(getWidth(), Math.abs(getArcWidth())) / 2.0; 548 double ah = Math.min(getHeight(), Math.abs(getArcHeight())) / 2.0; 549 int x0class = classify(x, rrx0, rrx1, aw); 550 int x1class = classify(x + w, rrx0, rrx1, aw); 551 int y0class = classify(y, rry0, rry1, ah); 552 int y1class = classify(y + h, rry0, rry1, ah); 553 if (x0class == 2 || x1class == 2 || y0class == 2 || y1class == 2) { 555 return true; 556 } 557 if ((x0class < 2 && x1class > 2) || (y0class < 2 && y1class > 2)) { 559 return true; 560 } 561 x = (x1class == 1) ? (x = x + w - (rrx0 + aw)) : (x = x - (rrx1 - aw)); 568 y = (y1class == 1) ? (y = y + h - (rry0 + ah)) : (y = y - (rry1 - ah)); 569 x = x / aw; 570 y = y / ah; 571 return (x * x + y * y <= 1.0); 572 } 573 574 587 public boolean contains(double x, double y, double w, double h) { 588 if (isEmpty() || w <= 0 || h <= 0) { 589 return false; 590 } 591 return (contains(x, y) && 592 contains(x + w, y) && 593 contains(x, y + h) && 594 contains(x + w, y + h)); 595 } 596 597 612 public PathIterator getPathIterator(AffineTransform at) { 613 return new RoundRectIterator (this, at); 614 } 615 } 616 | Popular Tags |