001: /*
002: * @(#)Rectangle.java 1.11 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package sun.porting.utils;
029:
030: /**
031: * An extension of java.awt.Rectangle. There is a change to the behavior
032: * of addition: we treat all rectangles with width <= 0 or height <= 0 as
033: * empty. For some reason, java.awt.Rectangle does not.
034: *
035: * @version 1.7, 08/19/02
036: * @author Richard Berlin
037: */
038: public class Rectangle extends java.awt.Rectangle {
039: public Rectangle() {
040: }
041:
042: public Rectangle(int x, int y, int width, int height) {
043: setRectangle(x, y, width, height);
044: }
045:
046: public Rectangle(java.awt.Rectangle r) {
047: setRectangle(r);
048: }
049:
050: public void add(java.awt.Rectangle r) {
051: if (r.width <= 0 || r.height <= 0) {
052: return;
053: }
054: if ((this .width <= 0) || (this .height <= 0)) {
055: setRectangle(r);
056: return;
057: }
058: super .add(r);
059: }
060:
061: public void add(int x, int y, int width, int height) {
062: if (width <= 0 || height <= 0) {
063: return;
064: }
065: if (this .width <= 0 || this .height <= 0) {
066: this .x = x;
067: this .y = y;
068: this .width = width;
069: this .height = height;
070: } else {
071: int x1 = Math.min(this .x, x);
072: int y1 = Math.min(this .y, y);
073: int x2 = Math.max(this .x + this .width, x + width);
074: int y2 = Math.max(this .y + this .height, y + height);
075: this .x = x1;
076: this .y = y1;
077: this .width = x2 - x1;
078: this .height = y2 - y1;
079: }
080: }
081:
082: public boolean intersects(int rx, int ry, int rwidth, int rheight) {
083: return !((rx + rwidth <= x) || (ry + rheight <= y)
084: || (rx >= x + width) || (ry >= y + height));
085: }
086:
087: public boolean contains(int rx, int ry, int rw, int rh) {
088: return ((rx >= x) && (ry >= y) && ((x + width) >= (rx + rw)) && ((y + height) >= (ry + rh)));
089: }
090:
091: public boolean contains(java.awt.Rectangle r) {
092: return contains(r.x, r.y, r.width, r.height);
093: }
094:
095: // Destructively intersect with the current rectangle.
096: public void intersect(int rx, int ry, int rwidth, int rheight) {
097: if (isEmpty()) {
098: return;
099: }
100: int x2 = Math.min(x + width, rx + rwidth);
101: int y2 = Math.min(y + height, ry + rheight);
102: x = Math.max(x, rx);
103: y = Math.max(y, ry);
104: width = x2 - x;
105: height = y2 - y;
106: }
107:
108: // Destructively intersect r with the current rectangle.
109: public void intersect(java.awt.Rectangle r) {
110: if (r.isEmpty()) {
111: setEmpty();
112: return;
113: }
114: intersect(r.x, r.y, r.width, r.height);
115: }
116:
117: public void subtract(int rx, int ry, int rw, int rh) {
118: if (isEmpty() || (rw == 0) || (rh == 0)) {
119: return;
120: }
121: if ((rx <= x) && ((rx + rw) >= (x + width))) {
122: // completely covers top or bottom edge
123: if (ry <= y) {
124: height += y;
125: y = ry + rh;
126: height -= y;
127: } else if ((ry + rh) >= (y + height)) {
128: height = ry - y;
129: }
130: }
131: if (height <= 0) {
132: setEmpty();
133: return;
134: }
135: if ((ry <= y) && ((ry + rh) >= (y + height))) {
136: // completely covers left or right edge
137: if (rx <= x) {
138: width += x;
139: x = rx + rw;
140: width -= x;
141: } else if ((rx + rw) >= (x + width)) {
142: width = rx - x;
143: }
144: }
145: if (width <= 0) {
146: setEmpty();
147: }
148: }
149:
150: // destructively subtract r from the current rectangle. As with
151: // the add operation, the result is the bounding box of the true answer,
152: // i.e. the smallest rectangle that encloses (this - r).
153: public void subtract(java.awt.Rectangle r) {
154: subtract(r.x, r.y, r.width, r.height);
155: }
156:
157: public void setEmpty() {
158: width = 0;
159: height = 0;
160: }
161:
162: public void setRectangle(int x, int y, int width, int height) {
163: this .x = x;
164: this .y = y;
165: this .width = width;
166: this .height = height;
167: }
168:
169: public void setRectangle(java.awt.Rectangle r) {
170: x = r.x;
171: y = r.y;
172: width = r.width;
173: height = r.height;
174: }
175: }
|