1 7 8 package java.awt; 9 10 import java.awt.geom.Rectangle2D ; 11 12 41 public class Rectangle extends Rectangle2D 42 implements Shape , java.io.Serializable 43 { 44 45 52 public int x; 53 54 61 public int y; 62 63 70 public int width; 71 72 79 public int height; 80 81 84 private static final long serialVersionUID = -4345857070255674764L; 85 86 89 private static native void initIDs(); 90 91 static { 92 93 Toolkit.loadLibraries(); 94 if (!GraphicsEnvironment.isHeadless()) { 95 initIDs(); 96 } 97 } 98 99 104 public Rectangle() { 105 this(0, 0, 0, 0); 106 } 107 108 115 public Rectangle(Rectangle r) { 116 this(r.x, r.y, r.width, r.height); 117 } 118 119 129 public Rectangle(int x, int y, int width, int height) { 130 this.x = x; 131 this.y = y; 132 this.width = width; 133 this.height = height; 134 } 135 136 143 public Rectangle(int width, int height) { 144 this(0, 0, width, height); 145 } 146 147 157 public Rectangle(Point p, Dimension d) { 158 this(p.x, p.y, d.width, d.height); 159 } 160 161 167 public Rectangle(Point p) { 168 this(p.x, p.y, 0, 0); 169 } 170 171 177 public Rectangle(Dimension d) { 178 this(0, 0, d.width, d.height); 179 } 180 181 186 public double getX() { 187 return x; 188 } 189 190 195 public double getY() { 196 return y; 197 } 198 199 204 public double getWidth() { 205 return width; 206 } 207 208 213 public double getHeight() { 214 return height; 215 } 216 217 230 public Rectangle getBounds() { 231 return new Rectangle (x, y, width, height); 232 } 233 234 238 public Rectangle2D getBounds2D() { 239 return new Rectangle (x, y, width, height); 240 } 241 242 253 public void setBounds(Rectangle r) { 254 setBounds(r.x, r.y, r.width, r.height); 255 } 256 257 275 public void setBounds(int x, int y, int width, int height) { 276 reshape(x, y, width, height); 277 } 278 279 292 public void setRect(double x, double y, double width, double height) { 293 int x0 = (int) Math.floor(x); 294 int y0 = (int) Math.floor(y); 295 int x1 = (int) Math.ceil(x+width); 296 int y1 = (int) Math.ceil(y+height); 297 setBounds(x0, y0, x1-x0, y1-y0); 298 } 299 300 315 @Deprecated 316 public void reshape(int x, int y, int width, int height) { 317 this.x = x; 318 this.y = y; 319 this.width = width; 320 this.height = height; 321 } 322 323 335 public Point getLocation() { 336 return new Point (x, y); 337 } 338 339 350 public void setLocation(Point p) { 351 setLocation(p.x, p.y); 352 } 353 354 365 public void setLocation(int x, int y) { 366 move(x, y); 367 } 368 369 377 @Deprecated 378 public void move(int x, int y) { 379 this.x = x; 380 this.y = y; 381 } 382 383 394 public void translate(int x, int y) { 395 this.x += x; 396 this.y += y; 397 } 398 399 412 public Dimension getSize() { 413 return new Dimension (width, height); 414 } 415 416 427 public void setSize(Dimension d) { 428 setSize(d.width, d.height); 429 } 430 431 443 public void setSize(int width, int height) { 444 resize(width, height); 445 } 446 447 456 @Deprecated 457 public void resize(int width, int height) { 458 this.width = width; 459 this.height = height; 460 } 461 462 472 public boolean contains(Point p) { 473 return contains(p.x, p.y); 474 } 475 476 488 public boolean contains(int x, int y) { 489 return inside(x, y); 490 } 491 492 502 public boolean contains(Rectangle r) { 503 return contains(r.x, r.y, r.width, r.height); 504 } 505 506 521 public boolean contains(int X, int Y, int W, int H) { 522 int w = this.width; 523 int h = this.height; 524 if ((w | h | W | H) < 0) { 525 return false; 527 } 528 int x = this.x; 530 int y = this.y; 531 if (X < x || Y < y) { 532 return false; 533 } 534 w += x; 535 W += X; 536 if (W <= X) { 537 if (w >= x || W > w) return false; 542 } else { 543 if (w >= x && W > w) return false; 547 } 548 h += y; 549 H += Y; 550 if (H <= Y) { 551 if (h >= y || H > h) return false; 552 } else { 553 if (h >= y && H > h) return false; 554 } 555 return true; 556 } 557 558 571 @Deprecated 572 public boolean inside(int X, int Y) { 573 int w = this.width; 574 int h = this.height; 575 if ((w | h) < 0) { 576 return false; 578 } 579 int x = this.x; 581 int y = this.y; 582 if (X < x || Y < y) { 583 return false; 584 } 585 w += x; 586 h += y; 587 return ((w < x || w > X) && 589 (h < y || h > Y)); 590 } 591 592 602 public boolean intersects(Rectangle r) { 603 int tw = this.width; 604 int th = this.height; 605 int rw = r.width; 606 int rh = r.height; 607 if (rw <= 0 || rh <= 0 || tw <= 0 || th <= 0) { 608 return false; 609 } 610 int tx = this.x; 611 int ty = this.y; 612 int rx = r.x; 613 int ry = r.y; 614 rw += rx; 615 rh += ry; 616 tw += tx; 617 th += ty; 618 return ((rw < rx || rw > tx) && 620 (rh < ry || rh > ty) && 621 (tw < tx || tw > rx) && 622 (th < ty || th > ry)); 623 } 624 625 638 public Rectangle intersection(Rectangle r) { 639 int tx1 = this.x; 640 int ty1 = this.y; 641 int rx1 = r.x; 642 int ry1 = r.y; 643 long tx2 = tx1; tx2 += this.width; 644 long ty2 = ty1; ty2 += this.height; 645 long rx2 = rx1; rx2 += r.width; 646 long ry2 = ry1; ry2 += r.height; 647 if (tx1 < rx1) tx1 = rx1; 648 if (ty1 < ry1) ty1 = ry1; 649 if (tx2 > rx2) tx2 = rx2; 650 if (ty2 > ry2) ty2 = ry2; 651 tx2 -= tx1; 652 ty2 -= ty1; 653 if (tx2 < Integer.MIN_VALUE) tx2 = Integer.MIN_VALUE; 657 if (ty2 < Integer.MIN_VALUE) ty2 = Integer.MIN_VALUE; 658 return new Rectangle (tx1, ty1, (int) tx2, (int) ty2); 659 } 660 661 671 public Rectangle union(Rectangle r) { 672 int x1 = Math.min(x, r.x); 673 int x2 = Math.max(x + width, r.x + r.width); 674 int y1 = Math.min(y, r.y); 675 int y2 = Math.max(y + height, r.y + r.height); 676 return new Rectangle (x1, y1, x2 - x1, y2 - y1); 677 } 678 679 697 public void add(int newx, int newy) { 698 int x1 = Math.min(x, newx); 699 int x2 = Math.max(x + width, newx); 700 int y1 = Math.min(y, newy); 701 int y2 = Math.max(y + height, newy); 702 x = x1; 703 y = y1; 704 width = x2 - x1; 705 height = y2 - y1; 706 } 707 708 726 public void add(Point pt) { 727 add(pt.x, pt.y); 728 } 729 730 736 public void add(Rectangle r) { 737 int x1 = Math.min(x, r.x); 738 int x2 = Math.max(x + width, r.x + r.width); 739 int y1 = Math.min(y, r.y); 740 int y2 = Math.max(y + height, r.y + r.height); 741 x = x1; 742 y = y1; 743 width = x2 - x1; 744 height = y2 - y1; 745 } 746 747 770 public void grow(int h, int v) { 771 x -= h; 772 y -= v; 773 width += h * 2; 774 height += v * 2; 775 } 776 777 784 public boolean isEmpty() { 785 return (width <= 0) || (height <= 0); 786 } 787 788 804 public int outcode(double x, double y) { 805 814 int out = 0; 815 if (this.width <= 0) { 816 out |= OUT_LEFT | OUT_RIGHT; 817 } else if (x < this.x) { 818 out |= OUT_LEFT; 819 } else if (x > this.x + (double) this.width) { 820 out |= OUT_RIGHT; 821 } 822 if (this.height <= 0) { 823 out |= OUT_TOP | OUT_BOTTOM; 824 } else if (y < this.y) { 825 out |= OUT_TOP; 826 } else if (y > this.y + (double) this.height) { 827 out |= OUT_BOTTOM; 828 } 829 return out; 830 } 831 832 843 public Rectangle2D createIntersection(Rectangle2D r) { 844 if (r instanceof Rectangle ) { 845 return intersection((Rectangle ) r); 846 } 847 Rectangle2D dest = new Rectangle2D.Double (); 848 Rectangle2D.intersect(this, r, dest); 849 return dest; 850 } 851 852 863 public Rectangle2D createUnion(Rectangle2D r) { 864 if (r instanceof Rectangle ) { 865 return union((Rectangle ) r); 866 } 867 Rectangle2D dest = new Rectangle2D.Double (); 868 Rectangle2D.union(this, r, dest); 869 return dest; 870 } 871 872 883 public boolean equals(Object obj) { 884 if (obj instanceof Rectangle ) { 885 Rectangle r = (Rectangle )obj; 886 return ((x == r.x) && 887 (y == r.y) && 888 (width == r.width) && 889 (height == r.height)); 890 } 891 return super.equals(obj); 892 } 893 894 900 public String toString() { 901 return getClass().getName() + "[x=" + x + ",y=" + y + ",width=" + width + ",height=" + height + "]"; 902 } 903 } 904 | Popular Tags |