KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > graphics > Rectangle


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.swt.graphics;
12
13
14 import org.eclipse.swt.internal.SerializableCompatibility;
15 import org.eclipse.swt.*;
16
17 /**
18  * Instances of this class represent rectangular areas in an
19  * (x, y) coordinate system. The top left corner of the rectangle
20  * is specified by its x and y values, and the extent of the
21  * rectangle is specified by its width and height.
22  * <p>
23  * The coordinate space for rectangles and points is considered
24  * to have increasing values downward and to the right from its
25  * origin making this the normal, computer graphics oriented notion
26  * of (x, y) coordinates rather than the strict mathematical one.
27  * </p>
28  * <p>
29  * The hashCode() method in this class uses the values of the public
30  * fields to compute the hash value. When storing instances of the
31  * class in hashed collections, do not modify these fields after the
32  * object has been inserted.
33  * </p>
34  * <p>
35  * Application code does <em>not</em> need to explicitly release the
36  * resources managed by each instance when those instances are no longer
37  * required, and thus no <code>dispose()</code> method is provided.
38  * </p>
39  *
40  * @see Point
41  */

42
43 public final class Rectangle implements SerializableCompatibility {
44     
45     /**
46      * the x coordinate of the rectangle
47      */

48     public int x;
49     
50     /**
51      * the y coordinate of the rectangle
52      */

53     public int y;
54     
55     /**
56      * the width of the rectangle
57      */

58     public int width;
59     
60     /**
61      * the height of the rectangle
62      */

63     public int height;
64
65     static final long serialVersionUID = 3256439218279428914L;
66     
67 /**
68  * Construct a new instance of this class given the
69  * x, y, width and height values.
70  *
71  * @param x the x coordinate of the origin of the rectangle
72  * @param y the y coordinate of the origin of the rectangle
73  * @param width the width of the rectangle
74  * @param height the height of the rectangle
75  */

76 public Rectangle (int x, int y, int width, int height) {
77     this.x = x;
78     this.y = y;
79     this.width = width;
80     this.height = height;
81 }
82
83 /**
84  * Destructively replaces the x, y, width and height values
85  * in the receiver with ones which represent the union of the
86  * rectangles specified by the receiver and the given rectangle.
87  * <p>
88  * The union of two rectangles is the smallest single rectangle
89  * that completely covers both of the areas covered by the two
90  * given rectangles.
91  * </p>
92  *
93  * @param rect the rectangle to merge with the receiver
94  *
95  * @exception IllegalArgumentException <ul>
96  * <li>ERROR_NULL_ARGUMENT - if the argument is null</li>
97  * </ul>
98  */

99 public void add (Rectangle rect) {
100     if (rect == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
101     int left = x < rect.x ? x : rect.x;
102     int top = y < rect.y ? y : rect.y;
103     int lhs = x + width;
104     int rhs = rect.x + rect.width;
105     int right = lhs > rhs ? lhs : rhs;
106     lhs = y + height;
107     rhs = rect.y + rect.height;
108     int bottom = lhs > rhs ? lhs : rhs;
109     x = left; y = top; width = right - left; height = bottom - top;
110 }
111
112 /**
113  * Returns <code>true</code> if the point specified by the
114  * arguments is inside the area specified by the receiver,
115  * and <code>false</code> otherwise.
116  *
117  * @param x the x coordinate of the point to test for containment
118  * @param y the y coordinate of the point to test for containment
119  * @return <code>true</code> if the rectangle contains the point and <code>false</code> otherwise
120  */

121 public boolean contains (int x, int y) {
122     return (x >= this.x) && (y >= this.y) && ((x - this.x) < width) && ((y - this.y) < height);
123 }
124
125 /**
126  * Returns <code>true</code> if the given point is inside the
127  * area specified by the receiver, and <code>false</code>
128  * otherwise.
129  *
130  * @param pt the point to test for containment
131  * @return <code>true</code> if the rectangle contains the point and <code>false</code> otherwise
132  *
133  * @exception IllegalArgumentException <ul>
134  * <li>ERROR_NULL_ARGUMENT - if the argument is null</li>
135  * </ul>
136  */

137 public boolean contains (Point pt) {
138     if (pt == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
139     return contains(pt.x, pt.y);
140 }
141
142 /**
143  * Compares the argument to the receiver, and returns true
144  * if they represent the <em>same</em> object using a class
145  * specific comparison.
146  *
147  * @param object the object to compare with this object
148  * @return <code>true</code> if the object is the same as this object and <code>false</code> otherwise
149  *
150  * @see #hashCode()
151  */

152 public boolean equals (Object JavaDoc object) {
153     if (object == this) return true;
154     if (!(object instanceof Rectangle)) return false;
155     Rectangle r = (Rectangle)object;
156     return (r.x == this.x) && (r.y == this.y) && (r.width == this.width) && (r.height == this.height);
157 }
158
159 /**
160  * Returns an integer hash code for the receiver. Any two
161  * objects that return <code>true</code> when passed to
162  * <code>equals</code> must return the same value for this
163  * method.
164  *
165  * @return the receiver's hash
166  *
167  * @see #equals(Object)
168  */

169 public int hashCode () {
170     return x ^ y ^ width ^ height;
171 }
172
173 /**
174  * Destructively replaces the x, y, width and height values
175  * in the receiver with ones which represent the intersection of the
176  * rectangles specified by the receiver and the given rectangle.
177  *
178  * @param rect the rectangle to intersect with the receiver
179  *
180  * @exception IllegalArgumentException <ul>
181  * <li>ERROR_NULL_ARGUMENT - if the argument is null</li>
182  * </ul>
183  *
184  * since 3.0
185  */

186 public void intersect (Rectangle rect) {
187     if (rect == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
188     if (this == rect) return;
189     int left = x > rect.x ? x : rect.x;
190     int top = y > rect.y ? y : rect.y;
191     int lhs = x + width;
192     int rhs = rect.x + rect.width;
193     int right = lhs < rhs ? lhs : rhs;
194     lhs = y + height;
195     rhs = rect.y + rect.height;
196     int bottom = lhs < rhs ? lhs : rhs;
197     x = right < left ? 0 : left;
198     y = bottom < top ? 0 : top;
199     width = right < left ? 0 : right - left;
200     height = bottom < top ? 0 : bottom - top;
201 }
202
203 /**
204  * Returns a new rectangle which represents the intersection
205  * of the receiver and the given rectangle.
206  * <p>
207  * The intersection of two rectangles is the rectangle that
208  * covers the area which is contained within both rectangles.
209  * </p>
210  *
211  * @param rect the rectangle to intersect with the receiver
212  * @return the intersection of the receiver and the argument
213  *
214  * @exception IllegalArgumentException <ul>
215  * <li>ERROR_NULL_ARGUMENT - if the argument is null</li>
216  * </ul>
217  */

218 public Rectangle intersection (Rectangle rect) {
219     if (rect == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
220     if (this == rect) return new Rectangle (x, y, width, height);
221     int left = x > rect.x ? x : rect.x;
222     int top = y > rect.y ? y : rect.y;
223     int lhs = x + width;
224     int rhs = rect.x + rect.width;
225     int right = lhs < rhs ? lhs : rhs;
226     lhs = y + height;
227     rhs = rect.y + rect.height;
228     int bottom = lhs < rhs ? lhs : rhs;
229     return new Rectangle (
230         right < left ? 0 : left,
231         bottom < top ? 0 : top,
232         right < left ? 0 : right - left,
233         bottom < top ? 0 : bottom - top);
234 }
235
236 /**
237  * Returns <code>true</code> if the rectangle described by the
238  * arguments intersects with the receiver and <code>false</code>
239  * otherwise.
240  * <p>
241  * Two rectangles intersect if the area of the rectangle
242  * representing their intersection is not empty.
243  * </p>
244  *
245  * @param x the x coordinate of the origin of the rectangle
246  * @param y the y coordinate of the origin of the rectangle
247  * @param width the width of the rectangle
248  * @param height the height of the rectangle
249  * @return <code>true</code> if the rectangle intersects with the receiver, and <code>false</code> otherwise
250  *
251  * @exception IllegalArgumentException <ul>
252  * <li>ERROR_NULL_ARGUMENT - if the argument is null</li>
253  * </ul>
254  *
255  * @see #intersection(Rectangle)
256  * @see #isEmpty()
257  *
258  * @since 3.0
259  */

260 public boolean intersects (int x, int y, int width, int height) {
261     return (x < this.x + this.width) && (y < this.y + this.height) &&
262         (x + width > this.x) && (y + height > this.y);
263 }
264
265 /**
266  * Returns <code>true</code> if the given rectangle intersects
267  * with the receiver and <code>false</code> otherwise.
268  * <p>
269  * Two rectangles intersect if the area of the rectangle
270  * representing their intersection is not empty.
271  * </p>
272  *
273  * @param rect the rectangle to test for intersection
274  * @return <code>true</code> if the rectangle intersects with the receiver, and <code>false</code> otherwise
275  *
276  * @exception IllegalArgumentException <ul>
277  * <li>ERROR_NULL_ARGUMENT - if the argument is null</li>
278  * </ul>
279  *
280  * @see #intersection(Rectangle)
281  * @see #isEmpty()
282  */

283 public boolean intersects (Rectangle rect) {
284     if (rect == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
285     return rect == this || intersects (rect.x, rect.y, rect.width, rect.height);
286 }
287         
288 /**
289  * Returns <code>true</code> if the receiver does not cover any
290  * area in the (x, y) coordinate plane, and <code>false</code> if
291  * the receiver does cover some area in the plane.
292  * <p>
293  * A rectangle is considered to <em>cover area</em> in the
294  * (x, y) coordinate plane if both its width and height are
295  * non-zero.
296  * </p>
297  *
298  * @return <code>true</code> if the receiver is empty, and <code>false</code> otherwise
299  */

300 public boolean isEmpty () {
301     return (width <= 0) || (height <= 0);
302 }
303
304 /**
305  * Returns a string containing a concise, human-readable
306  * description of the receiver.
307  *
308  * @return a string representation of the rectangle
309  */

310 public String JavaDoc toString () {
311     return "Rectangle {" + x + ", " + y + ", " + width + ", " + height + "}"; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
312
}
313
314 /**
315  * Returns a new rectangle which represents the union of
316  * the receiver and the given rectangle.
317  * <p>
318  * The union of two rectangles is the smallest single rectangle
319  * that completely covers both of the areas covered by the two
320  * given rectangles.
321  * </p>
322  *
323  * @param rect the rectangle to perform union with
324  * @return the union of the receiver and the argument
325  *
326  * @exception IllegalArgumentException <ul>
327  * <li>ERROR_NULL_ARGUMENT - if the argument is null</li>
328  * </ul>
329  *
330  * @see #add(Rectangle)
331  */

332 public Rectangle union (Rectangle rect) {
333     if (rect == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
334     int left = x < rect.x ? x : rect.x;
335     int top = y < rect.y ? y : rect.y;
336     int lhs = x + width;
337     int rhs = rect.x + rect.width;
338     int right = lhs > rhs ? lhs : rhs;
339     lhs = y + height;
340     rhs = rect.y + rect.height;
341     int bottom = lhs > rhs ? lhs : rhs;
342     return new Rectangle (left, top, right - left, bottom - top);
343 }
344
345 }
346
Popular Tags