Source Code Cross Referenced for Rectangle.java in  » 6.0-JDK-Modules » j2me » java » awt » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » 6.0 JDK Modules » j2me » java.awt 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * @(#)Rectangle.java	1.17 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 java.awt;
029:
030:        /**
031:         * A <code>Rectangle</code> specifies an area in a coordinate space that is
032:         * enclosed by the <code>Rectangle</code> object's top-left point
033:         * (<i>x</i>,&nbsp;<i>y</i>)
034:         * in the coordinate space, its width, and its height.
035:         * <p>
036:         * A <code>Rectangle</code> object's <code>width</code> and
037:         * <code>height</code> are <code>public</code> fields. The constructors
038:         * that create a <code>Rectangle</code>, and the methods that can modify
039:         * one, do not prevent setting a negative value for width or height.
040:         * <p>
041:         * A <code>Rectangle</code> whose width or height is negative is considered
042:         * empty. If the <code>Rectangle</code> is empty, then the
043:         * <code>isEmpty</code> method returns <code>true</code>. No point can be
044:         * contained by or inside an empty <code>Rectangle</code>.  The
045:         * values of <code>width</code> and <code>height</code>, however, are still
046:         * valid.  An empty <code>Rectangle</code> still has a location in the
047:         * coordinate space, and methods that change its size or location remain
048:         * valid. The behavior of methods that operate on more than one
049:         * <code>Rectangle</code> is undefined if any of the participating
050:         * <code>Rectangle</code> objects has a negative
051:         * <code>width</code> or <code>height</code>. These methods include
052:         * <code>intersects</code>, <code>intersection</code>, and
053:         * <code>union</code>.
054:         *
055:         * @version 	1.52, 02/02/00
056:         * @author 	Sami Shaio
057:         * @since       JDK1.0
058:         */
059:        public class Rectangle implements  Shape, Cloneable,
060:                java.io.Serializable {
061:            /**
062:             * The <i>x</i> coordinate of the <code>Rectangle</code>.
063:             *
064:             * @serial
065:             * @see #setLocation(int, int)
066:             * @see #getLocation()
067:             */
068:            public int x;
069:            /**
070:             * The <i>y</i> coordinate of the <code>Rectangle</code>.
071:             *
072:             * @serial
073:             * @see #setLocation(int, int)
074:             * @see #getLocation()
075:             */
076:            public int y;
077:            /**
078:             * The width of the <code>Rectangle</code>.
079:             * @serial
080:             * @see #setSize(int, int)
081:             * @see #getSize()
082:             * @since     JDK1.0.
083:             */
084:            public int width;
085:            /**
086:             * The height of the <code>Rectangle</code>.
087:             *
088:             * @serial
089:             * @see #setSize(int, int)
090:             * @see #getSize()
091:             */
092:            public int height;
093:            /*
094:             * JDK 1.1 serialVersionUID
095:             */
096:            private static final long serialVersionUID = -4345857070255674764L;
097:
098:            /**
099:             * Constructs a new <code>Rectangle</code> whose top-left corner
100:             * is at (0,&nbsp;0) in the coordinate space, and whose width and
101:             * height are both zero.
102:             */
103:            public Rectangle() {
104:                this (0, 0, 0, 0);
105:            }
106:
107:            /**
108:             * Constructs a new <code>Rectangle</code>, initialized to match
109:             * the values of the specificed <code>Rectangle</code>.
110:             * @param r  the <code>Rectangle</code> from which to copy initial values
111:             *           to a newly constructed <code>Rectangle</code>
112:             * @since JDK1.1
113:             */
114:            public Rectangle(Rectangle r) {
115:                this (r.x, r.y, r.width, r.height);
116:            }
117:
118:            /**
119:             * Constructs a new <code>Rectangle</code> whose top-left corner is
120:             * specified as
121:             * (<code>x</code>,&nbsp;<code>y</code>) and whose width and height
122:             * are specified by the arguments of the same name.
123:             * @param     x,&nbsp;y the specified coordinates
124:             * @param     width    the width of the <code>Rectangle</code>
125:             * @param     height   the height of the <code>Rectangle</code>
126:             */
127:            public Rectangle(int x, int y, int width, int height) {
128:                this .x = x;
129:                this .y = y;
130:                this .width = width;
131:                this .height = height;
132:            }
133:
134:            /**
135:             * Constructs a new <code>Rectangle</code> whose top-left corner
136:             * is at (0,&nbsp;0) in the coordinate space, and whose width and
137:             * height are specified by the arguments of the same name.
138:             * @param width the width of the <code>Rectangle</code>
139:             * @param height the height of the <code>Rectangle</code>
140:             */
141:            public Rectangle(int width, int height) {
142:                this (0, 0, width, height);
143:            }
144:
145:            /**
146:             * Constructs a new <code>Rectangle</code> whose top-left corner is
147:             * specified by the {@link Point} argument, and
148:             * whose width and height are specified by the
149:             * {@link Dimension} argument.
150:             * @param p a <code>Point</code> that is the top-left corner of
151:             * the <code>Rectangle</code>
152:             * @param d a <code>Dimension</code>, representing the
153:             * width and height of the <code>Rectangle</code>
154:             */
155:            public Rectangle(Point p, Dimension d) {
156:                this (p.x, p.y, d.width, d.height);
157:            }
158:
159:            /**
160:             * Constructs a new <code>Rectangle</code> whose top-left corner is the
161:             * specified <code>Point</code>, and whose width and height are both zero.
162:             * @param p a <code>Point</code> that is the top left corner
163:             * of the <code>Rectangle</code>
164:             */
165:            public Rectangle(Point p) {
166:                this (p.x, p.y, 0, 0);
167:            }
168:
169:            /**
170:             * Constructs a new <code>Rectangle</code> whose top left corner is
171:             * (0,&nbsp;0) and whose width and height are specified
172:             * by the <code>Dimension</code> argument.
173:             * @param d a <code>Dimension</code>, specifying width and height
174:             */
175:            public Rectangle(Dimension d) {
176:                this (0, 0, d.width, d.height);
177:            }
178:
179:            /**
180:             * Returns the X coordinate of the bounding <code>Rectangle</code> in
181:             * <code>double</code> precision.
182:             * @return the x coordinate of the bounding <code>Rectangle</code>.
183:             */
184:
185:            private double getX() {
186:                return x;
187:            }
188:
189:            /**
190:             * Returns the Y coordinate of the bounding <code>Rectangle</code> in
191:             * <code>double</code> precision.
192:             * @return the y coordinate of the bounding <code>Rectangle</code>.
193:             */
194:
195:            private double getY() {
196:                return y;
197:            }
198:
199:            /**
200:             * Returns the width of the bounding <code>Rectangle</code> in
201:             * <code>double</code> precision.
202:             * @return the width of the bounding <code>Rectangle</code>.
203:             */
204:
205:            private double getWidth() {
206:                return width;
207:            }
208:
209:            /**
210:             * Returns the height of the bounding <code>Rectangle</code> in
211:             * <code>double</code> precision.
212:             * @return the height of the bounding <code>Rectangle</code>.
213:             */
214:
215:            private double getHeight() {
216:                return height;
217:            }
218:
219:            boolean contains(double x, double y) {
220:                double x0 = getX();
221:                double y0 = getY();
222:                return (x >= x0 && y >= y0 && x < x0 + getWidth() && y < y0
223:                        + getHeight());
224:            }
225:
226:            /**
227:             * Gets the bounding <code>Rectangle</code> of this <code>Rectangle</code>.
228:             * <p>
229:             * This method is included for completeness, to parallel the
230:             * <code>getBounds</code> method of
231:             * {@link Component}.
232:             * @return    a new <code>Rectangle</code>, equal to the
233:             * bounding <code>Rectangle</code> for this <code>Rectangle</code>.
234:             * @see       java.awt.Component#getBounds
235:             * @since     JDK1.1
236:             */
237:            public Rectangle getBounds() {
238:                return new Rectangle(x, y, width, height);
239:            }
240:
241:            /**
242:             * Sets the bounding <code>Rectangle</code> of this <code>Rectangle</code>
243:             * to match the specified <code>Rectangle</code>.
244:             * <p>
245:             * This method is included for completeness, to parallel the
246:             * <code>setBounds</code> method of <code>Component</code>.
247:             * @param r the specified <code>Rectangle</code>
248:             * @see       java.awt.Component#setBounds(java.awt.Rectangle)
249:             * @since     JDK1.1
250:             */
251:            public void setBounds(Rectangle r) {
252:                setBounds(r.x, r.y, r.width, r.height);
253:            }
254:
255:            /**
256:             * Sets the bounding <code>Rectangle</code> of this
257:             * <code>Rectangle</code> to the specified
258:             * <code>x</code>, <code>y</code>, <code>width</code>,
259:             * and <code>height</code>.
260:             * <p>
261:             * This method is included for completeness, to parallel the
262:             * <code>setBounds</code> method of <code>Component</code>.
263:             * @param x,&nbsp;y the new coordinates for the top-left
264:             *                    corner of this <code>Rectangle</code>
265:             * @param width the new width for this <code>Rectangle</code>
266:             * @param height the new height for this <code>Rectangle</code>
267:             * @see       java.awt.Component#setBounds(int, int, int, int)
268:             * @since     JDK1.1
269:             */
270:            public void setBounds(int x, int y, int width, int height) {
271:                reshape(x, y, width, height);
272:            }
273:
274:            /**
275:             * Sets the bounds of this <code>Rectangle</code> to the specified
276:             * <code>x</code>, <code>y</code>, <code>width</code>,
277:             * and <code>height</code>.
278:             * This method is included for completeness, to parallel the
279:             * <code>setBounds</code> method of <code>Component</code>.
280:             * @param width the new width for the <code>Dimension</code> object
281:             * @param height  the new height for the <code>Dimension</code> object
282:             */
283:
284:            /*    public void setRect(double x, double y, double width, double height) {
285:             int x0 = (int) Math.floor(x);
286:             int y0 = (int) Math.floor(y);
287:             int x1 = (int) Math.ceil(x+width);
288:             int y1 = (int) Math.ceil(y+height);
289:             setBounds(x0, y0, x1-x0, y1-y0);
290:             }
291:             */
292:
293:            /**
294:             * @deprecated As of JDK version 1.1,
295:             * replaced by <code>setBounds(int, int, int, int)</code>.
296:             */
297:            public void reshape(int x, int y, int width, int height) {
298:                this .x = x;
299:                this .y = y;
300:                this .width = width;
301:                this .height = height;
302:            }
303:
304:            /**
305:             * Returns the location of this <code>Rectangle</code>.
306:             * <p>
307:             * This method is included for completeness, to parallel the
308:             * <code>getLocation</code> method of <code>Component</code>.
309:             * @return the <code>Point</code> that is the top-left corner of
310:             *			this <code>Rectangle</code>.
311:             * @see       java.awt.Component#getLocation
312:             * @since     JDK1.1
313:             */
314:            public Point getLocation() {
315:                return new Point(x, y);
316:            }
317:
318:            /**
319:             * Moves this <code>Rectangle</code> to the specified location.
320:             * <p>
321:             * This method is included for completeness, to parallel the
322:             * <code>setLocation</code> method of <code>Component</code>.
323:             * @param p the <code>Point</code> specifying the new location
324:             *                for this <code>Rectangle</code>
325:             * @see       java.awt.Component#setLocation(java.awt.Point)
326:             * @since     JDK1.1
327:             */
328:            public void setLocation(Point p) {
329:                setLocation(p.x, p.y);
330:            }
331:
332:            /**
333:             * Moves this <code>Rectangle</code> to the specified location.
334:             * <p>
335:             * This method is included for completeness, to parallel the
336:             * <code>setLocation</code> method of <code>Component</code>.
337:             * @param x,&nbsp;y the coordinates of the new location
338:             * @see       java.awt.Component#setLocation(int, int)
339:             * @since     JDK1.1
340:             */
341:            public void setLocation(int x, int y) {
342:                move(x, y);
343:            }
344:
345:            /**
346:             * @deprecated As of JDK version 1.1,
347:             * replaced by <code>setLocation(int, int)</code>.
348:             */
349:            public void move(int x, int y) {
350:                this .x = x;
351:                this .y = y;
352:            }
353:
354:            /**
355:             * Translates this <code>Rectangle</code> the indicated distance,
356:             * to the right along the x coordinate axis, and
357:             * downward along the y coordinate axis.
358:             * @param dx the distance to move this <code>Rectangle</code>
359:             *                 along the x axis
360:             * @param dy the distance to move this <code>Rectangle</code>
361:             *                 along the y axis
362:             * @see       java.awt.Rectangle#setLocation(int, int)
363:             * @see       java.awt.Rectangle#setLocation(java.awt.Point)
364:             */
365:            public void translate(int x, int y) {
366:                this .x += x;
367:                this .y += y;
368:            }
369:
370:            /**
371:             * Gets the size of this <code>Rectangle</code>, represented by
372:             * the returned <code>Dimension</code>.
373:             * <p>
374:             * This method is included for completeness, to parallel the
375:             * <code>getSize</code> method of <code>Component</code>.
376:             * @return a <code>Dimension</code>, representing the size of
377:             *            this <code>Rectangle</code>.
378:             * @see       java.awt.Component#getSize
379:             * @since     JDK1.1
380:             */
381:            public Dimension getSize() {
382:                return new Dimension(width, height);
383:            }
384:
385:            /**
386:             * Sets the size of this <code>Rectangle</code> to match the
387:             * specified <code>Dimension</code>.
388:             * <p>
389:             * This method is included for completeness, to parallel the
390:             * <code>setSize</code> method of <code>Component</code>.
391:             * @param d the new size for the <code>Dimension</code> object
392:             * @see       java.awt.Component#setSize(java.awt.Dimension)
393:             * @since     JDK1.1
394:             */
395:            public void setSize(Dimension d) {
396:                setSize(d.width, d.height);
397:            }
398:
399:            /**
400:             * Sets the size of this <code>Rectangle</code> to the specified
401:             * width and height.
402:             * <p>
403:             * This method is included for completeness, to parallel the
404:             * <code>setSize</code> method of <code>Component</code>.
405:             * @param width the new width for this <code>Rectangle</code>
406:             * @param height the new height for this <code>Rectangle</code>
407:             * @see       java.awt.Component#setSize(int, int)
408:             * @since     JDK1.1
409:             */
410:            public void setSize(int width, int height) {
411:                resize(width, height);
412:            }
413:
414:            /**
415:             * @deprecated As of JDK version 1.1,
416:             * replaced by <code>setSize(int, int)</code>.
417:             */
418:            public void resize(int width, int height) {
419:                this .width = width;
420:                this .height = height;
421:            }
422:
423:            /**
424:             * Checks whether or not this <code>Rectangle</code> contains the
425:             * specified <code>Point</code>.
426:             * @param p the <code>Point</code> to test
427:             * @return    <code>true</code> if the <code>Point</code>
428:             *            (<i>x</i>,&nbsp;<i>y</i>) is inside this
429:             * 		  <code>Rectangle</code>;
430:             *            <code>false</code> otherwise.
431:             * @since     JDK1.1
432:             */
433:            public boolean contains(Point p) {
434:                return contains(p.x, p.y);
435:            }
436:
437:            /**
438:             * Checks whether or not this <code>Rectangle</code> contains the
439:             * point at the specified location
440:             * (<i>x</i>,&nbsp;<i>y</i>).
441:             * @param  x,&nbsp;y  the specified coordinates
442:             * @return    <code>true</code> if the point
443:             *            (<i>x</i>,&nbsp;<i>y</i>) is inside this
444:             *		  <code>Rectangle</code>;
445:             *            <code>false</code> otherwise.
446:             * @since     JDK1.1
447:             */
448:            public boolean contains(int x, int y) {
449:                return ((this .width > 0) && (this .height > 0) && (x >= this .x)
450:                        && (y >= this .y) && ((x - this .x) < width) && ((y - this .y) < height));
451:            }
452:
453:            /**
454:             * Checks whether or not this <code>Rectangle</code> entirely contains
455:             * the specified <code>Rectangle</code>.
456:             * @param     r   the specified <code>Rectangle</code>
457:             * @return    <code>true</code> if the <code>Rectangle</code>
458:             *            is contained entirely inside this <code>Rectangle</code>;
459:             *            <code>false</code> otherwise.
460:             * @since     JDK1.1
461:             */
462:            public boolean contains(Rectangle r) {
463:                return contains(r.x, r.y, r.width, r.height);
464:            }
465:
466:            /**
467:             * Checks whether this <code>Rectangle</code> entirely contains
468:             * the <code>Rectangle</code>
469:             * at the specified location (<i>X</i>,&nbsp;<i>Y</i>) with the
470:             * specified dimensions (<i>W</i>,&nbsp;<i>H</i>).
471:             * @param     x,&nbsp;y  the specified coordinates
472:             * @param     W   the width of the <code>Rectangle</code>
473:             * @param     H   the height of the <code>Rectangle</code>
474:             * @return    <code>true</code> if the <code>Rectangle</code> specified by
475:             *            (<i>X</i>,&nbsp;<i>Y</i>,&nbsp;<i>W</i>,&nbsp;<i>H</i>)
476:             *            is entirely enclosed inside this <code>Rectangle</code>;
477:             *            <code>false</code> otherwise.
478:             * @since     JDK1.1
479:             */
480:            public boolean contains(int X, int Y, int W, int H) {
481:                int width = this .width;
482:                int height = this .height;
483:                if (width <= 0 || height <= 0 || W <= 0 || H <= 0) {
484:                    return false;
485:                }
486:                int x = this .x;
487:                int y = this .y;
488:                return (X >= x && Y >= y && X + W <= x + width && Y + H <= y
489:                        + height);
490:            }
491:
492:            /**
493:             * @deprecated As of JDK version 1.1,
494:             * replaced by <code>contains(int, int)</code>.
495:             */
496:            public boolean inside(int x, int y) {
497:                return contains(x, y);
498:            }
499:
500:            /**
501:             * Determines whether or not this <code>Rectangle</code> and the specified
502:             * <code>Rectangle</code> intersect. Two rectangles intersect if
503:             * their intersection is nonempty.
504:             * @param r the specified <code>Rectangle</code>
505:             * @return    <code>true</code> if the specified <code>Rectangle</code>
506:             *            and this <code>Rectangle</code> insersect;
507:             *            <code>false</code> otherwise.
508:             */
509:            public boolean intersects(Rectangle r) {
510:                int tw = this .width;
511:                int th = this .height;
512:                int rw = r.width;
513:                int rh = r.height;
514:                if (rw <= 0 || rh <= 0 || tw <= 0 || th <= 0) {
515:                    return false;
516:                }
517:                int tx = this .x;
518:                int ty = this .y;
519:                int rx = r.x;
520:                int ry = r.y;
521:                rw += rx;
522:                rh += ry;
523:                tw += tx;
524:                th += ty;
525:                //      overflow || intersect
526:                return ((rw < rx || rw > tx) && (rh < ry || rh > ty)
527:                        && (tw < tx || tw > rx) && (th < ty || th > ry));
528:            }
529:
530:            /**
531:             * Computes the intersection of this <code>Rectangle</code> with the
532:             * specified <code>Rectangle</code>. Returns a new <code>Rectangle</code>
533:             * that represents the intersection of the two rectangles.
534:             * @param     r   the specified <code>Rectangle</code>
535:             * @return    the largest <code>Rectangle</code> contained in both the
536:             *            specified <code>Rectangle</code> and in
537:             *		  this<code>Rectangle</code>.
538:             */
539:            public Rectangle intersection(Rectangle r) {
540:                if (intersects(r)) {
541:                    int x1 = Math.max(x, r.x);
542:                    int x2 = Math.min(x + width, r.x + r.width);
543:                    int y1 = Math.max(y, r.y);
544:                    int y2 = Math.min(y + height, r.y + r.height);
545:                    if (((x2 - x1) < 0) || ((y2 - y1) < 0))
546:                        // Width or height is negative. No intersection.
547:                        return new Rectangle(0, 0, 0, 0);
548:                    else
549:                        return new Rectangle(x1, y1, x2 - x1, y2 - y1);
550:                } else {
551:                    return new Rectangle(0, 0, 0, 0);
552:                }
553:            }
554:
555:            /**
556:             * Computes the union of this <code>Rectangle</code> with the
557:             * specified <code>Rectangle</code>. Returns a new
558:             * <code>Rectangle</code> that
559:             * represents the union of the two rectangles
560:             * @param r the specified <code>Rectangle</code>
561:             * @return    the smallest <code>Rectangle</code> containing both
562:             *		  the specified <code>Rectangle</code> and this
563:             *		  <code>Rectangle</code>.
564:             */
565:            public Rectangle union(Rectangle r) {
566:                int x1 = Math.min(x, r.x);
567:                int x2 = Math.max(x + width, r.x + r.width);
568:                int y1 = Math.min(y, r.y);
569:                int y2 = Math.max(y + height, r.y + r.height);
570:                return new Rectangle(x1, y1, x2 - x1, y2 - y1);
571:            }
572:
573:            /**
574:             * Adds a point, specified by the integer arguments <code>newx</code>
575:             * and <code>newy</code>, to this <code>Rectangle</code>. The
576:             * resulting <code>Rectangle</code> is
577:             * the smallest <code>Rectangle</code> that contains both the
578:             * original <code>Rectangle</code> and the specified point.
579:             * <p>
580:             * After adding a point, a call to <code>contains</code> with the
581:             * added point as an argument does not necessarily return
582:             * <code>true</code>. The <code>contains</code> method does not
583:             * return <code>true</code> for points on the right or bottom
584:             * edges of a <code>Rectangle</code>. Therefore, if the added point
585:             * falls on the right or bottom edge of the enlarged
586:             * <code>Rectangle</code>, <code>contains</code> returns
587:             * <code>false</code> for that point.
588:             * @param newx,&nbsp;newy   the coordinates of the new point
589:             */
590:            public void add(int newx, int newy) {
591:                int x1 = Math.min(x, newx);
592:                int x2 = Math.max(x + width, newx);
593:                int y1 = Math.min(y, newy);
594:                int y2 = Math.max(y + height, newy);
595:                x = x1;
596:                y = y1;
597:                width = x2 - x1;
598:                height = y2 - y1;
599:            }
600:
601:            /**
602:             * Adds the specified <code>Point</code> to this
603:             * <code>Rectangle</code>. The resulting <code>Rectangle</code>
604:             * is the smallest <code>Rectangle</code> that contains both the
605:             * original <code>Rectangle</code> and the specified
606:             * <code>Point</code>.
607:             * <p>
608:             * After adding a <code>Point</code>, a call to <code>contains</code>
609:             * with the added <code>Point</code> as an argument does not
610:             * necessarily return <code>true</code>. The <code>contains</code>
611:             * method does not return <code>true</code> for points on the right
612:             * or bottom edges of a <code>Rectangle</code>. Therefore if the added
613:             * <code>Point</code> falls on the right or bottom edge of the
614:             * enlarged <code>Rectangle</code>, <code>contains</code> returns
615:             * <code>false</code> for that <code>Point</code>.
616:             * @param pt the new <code>Point</code> to add to this
617:             *           <code>Rectangle</code>
618:             */
619:            public void add(Point pt) {
620:                add(pt.x, pt.y);
621:            }
622:
623:            /**
624:             * Adds a <code>Rectangle</code> to this <code>Rectangle</code>.
625:             * The resulting <code>Rectangle</code> is the union of the two
626:             * rectangles.
627:             * @param  r the specified <code>Rectangle</code>
628:             */
629:            public void add(Rectangle r) {
630:                int x1 = Math.min(x, r.x);
631:                int x2 = Math.max(x + width, r.x + r.width);
632:                int y1 = Math.min(y, r.y);
633:                int y2 = Math.max(y + height, r.y + r.height);
634:                x = x1;
635:                y = y1;
636:                width = x2 - x1;
637:                height = y2 - y1;
638:            }
639:
640:            /**
641:             * Returns the hashcode for this <code>Rectangle2D</code>.
642:             * @return the hashcode for this <code>Rectangle2D</code>.
643:             */
644:            public int hashCode() {
645:                long bits = java.lang.Double.doubleToLongBits(getX());
646:                bits += java.lang.Double.doubleToLongBits(getY()) * 37;
647:                bits += java.lang.Double.doubleToLongBits(getWidth()) * 43;
648:                bits += java.lang.Double.doubleToLongBits(getHeight()) * 47;
649:                return (((int) bits) ^ ((int) (bits >> 32)));
650:            }
651:
652:            /**
653:             * Resizes the <code>Rectangle</code> both horizontally and vertically.
654:             * <p>
655:             * This method modifies the <code>Rectangle</code> so that it is
656:             * <code>h</code> units larger on both the left and right side,
657:             * and <code>v</code> units larger at both the top and bottom.
658:             * <p>
659:             * The new <code>Rectangle</code> has (<code>x&nbsp;-&nbsp;h</code>,
660:             * <code>y&nbsp;-&nbsp;v</code>) as its top-left corner, a
661:             * width of
662:             * <code>width</code>&nbsp;<code>+</code>&nbsp;<code>2h</code>,
663:             * and a height of
664:             * <code>height</code>&nbsp;<code>+</code>&nbsp;<code>2v</code>.
665:             * <p>
666:             * If negative values are supplied for <code>h</code> and
667:             * <code>v</code>, the size of the <code>Rectangle</code>
668:             * decreases accordingly.
669:             * The <code>grow</code> method does not check whether the resulting
670:             * values of <code>width</code> and <code>height</code> are
671:             * non-negative.
672:             * @param h the horizontal expansion
673:             * @param v the vertical expansion
674:             */
675:            public void grow(int h, int v) {
676:                x -= h;
677:                y -= v;
678:                width += h * 2;
679:                height += v * 2;
680:            }
681:
682:            /**
683:             * Determines whether or not this <code>Rectangle</code> is empty. A
684:             * <code>Rectangle</code> is empty if its width or its height is less
685:             * than or equal to zero.
686:             * @return     <code>true</code> if this <code>Rectangle</code> is empty;
687:             *             <code>false</code> otherwise.
688:             */
689:            public boolean isEmpty() {
690:                return (width <= 0) || (height <= 0);
691:            }
692:
693:            /**
694:             * Checks whether two rectangles are equal.
695:             * <p>
696:             * The result is <code>true</code> if and only if the argument is not
697:             * <code>null</code> and is a <code>Rectangle</code> object that has the
698:             * same top-left corner, width, and height as this <code>Rectangle</code>.
699:             * @param obj the <code>Object</code> to compare with
700:             *                this <code>Rectangle</code>
701:             * @return    <code>true</code> if the objects are equal;
702:             *            <code>false</code> otherwise.
703:             */
704:            public boolean equals(Object obj) {
705:                if (obj instanceof  Rectangle) {
706:                    Rectangle r = (Rectangle) obj;
707:                    return ((x == r.x) && (y == r.y) && (width == r.width) && (height == r.height));
708:                }
709:                return super .equals(obj);
710:            }
711:
712:            /**
713:             * Creates a new object of the same class and with the same
714:             * contents as this object.
715:             * @return     a clone of this instance.
716:             * @exception  OutOfMemoryError            if there is not enough memory.
717:             * @see        java.lang.Cloneable
718:             * @since      1.2
719:             */
720:            public Object clone() {
721:                try {
722:                    return super .clone();
723:                } catch (CloneNotSupportedException e) {
724:                    // this shouldn't happen, since we are Cloneable
725:                    throw new InternalError();
726:                }
727:            }
728:
729:            /**
730:             * Returns a <code>String</code> representing this
731:             * <code>Rectangle</code> and its values.
732:             * @return a <code>String</code> representing this
733:             *               <code>Rectangle</code> object's coordinate and size values.
734:             */
735:            public String toString() {
736:                return getClass().getName() + "[x=" + x + ",y=" + y + ",width="
737:                        + width + ",height=" + height + "]";
738:            }
739:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.