001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Denis M. Kishenko
019: * @version $Revision$
020: */package java.awt;
021:
022: import java.awt.geom.Rectangle2D;
023: import java.io.Serializable;
024:
025: public class Rectangle extends Rectangle2D implements Shape,
026: Serializable {
027:
028: private static final long serialVersionUID = -4345857070255674764L;
029:
030: public int x;
031: public int y;
032: public int width;
033: public int height;
034:
035: public Rectangle() {
036: setBounds(0, 0, 0, 0);
037: }
038:
039: public Rectangle(Point p) {
040: setBounds(p.x, p.y, 0, 0);
041: }
042:
043: public Rectangle(Point p, Dimension d) {
044: setBounds(p.x, p.y, d.width, d.height);
045: }
046:
047: public Rectangle(int x, int y, int width, int height) {
048: setBounds(x, y, width, height);
049: }
050:
051: public Rectangle(int width, int height) {
052: setBounds(0, 0, width, height);
053: }
054:
055: public Rectangle(Rectangle r) {
056: setBounds(r.x, r.y, r.width, r.height);
057: }
058:
059: public Rectangle(Dimension d) {
060: setBounds(0, 0, d.width, d.height);
061: }
062:
063: @Override
064: public double getX() {
065: return x;
066: }
067:
068: @Override
069: public double getY() {
070: return y;
071: }
072:
073: @Override
074: public double getHeight() {
075: return height;
076: }
077:
078: @Override
079: public double getWidth() {
080: return width;
081: }
082:
083: @Override
084: public boolean isEmpty() {
085: return width <= 0 || height <= 0;
086: }
087:
088: public Dimension getSize() {
089: return new Dimension(width, height);
090: }
091:
092: public void setSize(int width, int height) {
093: this .width = width;
094: this .height = height;
095: }
096:
097: public void setSize(Dimension d) {
098: setSize(d.width, d.height);
099: }
100:
101: public Point getLocation() {
102: return new Point(x, y);
103: }
104:
105: public void setLocation(int x, int y) {
106: this .x = x;
107: this .y = y;
108: }
109:
110: public void setLocation(Point p) {
111: setLocation(p.x, p.y);
112: }
113:
114: /**
115: * @deprecated
116: */
117: @Deprecated
118: public void move(int x, int y) {
119: setLocation(x, y);
120: }
121:
122: @Override
123: public void setRect(double x, double y, double width, double height) {
124: int x1 = (int) Math.floor(x);
125: int y1 = (int) Math.floor(y);
126: int x2 = (int) Math.ceil(x + width);
127: int y2 = (int) Math.ceil(y + height);
128: setBounds(x1, y1, x2 - x1, y2 - y1);
129: }
130:
131: /**
132: * @deprecated
133: */
134: @Deprecated
135: public void resize(int width, int height) {
136: setBounds(x, y, width, height);
137: }
138:
139: /**
140: * @deprecated
141: */
142: @Deprecated
143: public void reshape(int x, int y, int width, int height) {
144: setBounds(x, y, width, height);
145: }
146:
147: @Override
148: public Rectangle getBounds() {
149: return new Rectangle(x, y, width, height);
150: }
151:
152: @Override
153: public Rectangle2D getBounds2D() {
154: return getBounds();
155: }
156:
157: public void setBounds(int x, int y, int width, int height) {
158: this .x = x;
159: this .y = y;
160: this .height = height;
161: this .width = width;
162: }
163:
164: public void setBounds(Rectangle r) {
165: setBounds(r.x, r.y, r.width, r.height);
166: }
167:
168: public void grow(int dx, int dy) {
169: x -= dx;
170: y -= dy;
171: width += dx + dx;
172: height += dy + dy;
173: }
174:
175: public void translate(int mx, int my) {
176: x += mx;
177: y += my;
178: }
179:
180: public void add(int px, int py) {
181: int x1 = Math.min(x, px);
182: int x2 = Math.max(x + width, px);
183: int y1 = Math.min(y, py);
184: int y2 = Math.max(y + height, py);
185: setBounds(x1, y1, x2 - x1, y2 - y1);
186: }
187:
188: public void add(Point p) {
189: add(p.x, p.y);
190: }
191:
192: public void add(Rectangle r) {
193: int x1 = Math.min(x, r.x);
194: int x2 = Math.max(x + width, r.x + r.width);
195: int y1 = Math.min(y, r.y);
196: int y2 = Math.max(y + height, r.y + r.height);
197: setBounds(x1, y1, x2 - x1, y2 - y1);
198: }
199:
200: public boolean contains(int px, int py) {
201: if (isEmpty()) {
202: return false;
203: }
204: if (px < x || py < y) {
205: return false;
206: }
207: px -= x;
208: py -= y;
209: return px < width && py < height;
210: }
211:
212: public boolean contains(Point p) {
213: return contains(p.x, p.y);
214: }
215:
216: public boolean contains(int rx, int ry, int rw, int rh) {
217: return contains(rx, ry) && contains(rx + rw - 1, ry + rh - 1);
218: }
219:
220: public boolean contains(Rectangle r) {
221: return contains(r.x, r.y, r.width, r.height);
222: }
223:
224: /**
225: * @deprecated
226: */
227: @Deprecated
228: public boolean inside(int px, int py) {
229: return contains(px, py);
230: }
231:
232: @Override
233: public Rectangle2D createIntersection(Rectangle2D r) {
234: if (r instanceof Rectangle) {
235: return intersection((Rectangle) r);
236: }
237: Rectangle2D dst = new Rectangle2D.Double();
238: Rectangle2D.intersect(this , r, dst);
239: return dst;
240: }
241:
242: public Rectangle intersection(Rectangle r) {
243: int x1 = Math.max(x, r.x);
244: int y1 = Math.max(y, r.y);
245: int x2 = Math.min(x + width, r.x + r.width);
246: int y2 = Math.min(y + height, r.y + r.height);
247: return new Rectangle(x1, y1, x2 - x1, y2 - y1);
248: }
249:
250: public boolean intersects(Rectangle r) {
251: return !intersection(r).isEmpty();
252: }
253:
254: @Override
255: public int outcode(double px, double py) {
256: int code = 0;
257:
258: if (width <= 0) {
259: code |= OUT_LEFT | OUT_RIGHT;
260: } else if (px < x) {
261: code |= OUT_LEFT;
262: } else if (px > x + width) {
263: code |= OUT_RIGHT;
264: }
265:
266: if (height <= 0) {
267: code |= OUT_TOP | OUT_BOTTOM;
268: } else if (py < y) {
269: code |= OUT_TOP;
270: } else if (py > y + height) {
271: code |= OUT_BOTTOM;
272: }
273:
274: return code;
275: }
276:
277: @Override
278: public Rectangle2D createUnion(Rectangle2D r) {
279: if (r instanceof Rectangle) {
280: return union((Rectangle) r);
281: }
282: Rectangle2D dst = new Rectangle2D.Double();
283: Rectangle2D.union(this , r, dst);
284: return dst;
285: }
286:
287: public Rectangle union(Rectangle r) {
288: Rectangle dst = new Rectangle(this );
289: dst.add(r);
290: return dst;
291: }
292:
293: @Override
294: public boolean equals(Object obj) {
295: if (obj == this ) {
296: return true;
297: }
298: if (obj instanceof Rectangle) {
299: Rectangle r = (Rectangle) obj;
300: return r.x == x && r.y == y && r.width == width
301: && r.height == height;
302: }
303: return false;
304: }
305:
306: @Override
307: public String toString() {
308: // The output format based on 1.5 release behaviour. It could be obtained in the following way
309: // System.out.println(new Rectangle().toString())
310: return getClass().getName() + "[x=" + x + ",y=" + y + //$NON-NLS-1$ //$NON-NLS-2$
311: ",width=" + width + ",height=" + height + "]"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
312: }
313:
314: }
|