1. /*
  2. * @(#)Rectangle.java 1.48 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.awt;
  8. import java.awt.geom.Rectangle2D;
  9. /**
  10. * A <code>Rectangle</code> specifies an area in a coordinate space that is
  11. * enclosed by the <code>Rectangle</code> object's top-left point
  12. * (<i>x</i>, <i>y</i>)
  13. * in the coordinate space, its width, and its height.
  14. * <p>
  15. * A <code>Rectangle</code> object's <code>width</code> and
  16. * <code>height</code> are <code>public</code> fields. The constructors
  17. * that create a <code>Rectangle</code>, and the methods that can modify
  18. * one, do not prevent setting a negative value for width or height.
  19. * <p>
  20. * A <code>Rectangle</code> whose width or height is negative is considered
  21. * empty. If the <code>Rectangle</code> is empty, then the
  22. * <code>isEmpty</code> method returns <code>true</code>. No point can be
  23. * contained by or inside an empty <code>Rectangle</code>. The
  24. * values of <code>width</code> and <code>height</code>, however, are still
  25. * valid. An empty <code>Rectangle</code> still has a location in the
  26. * coordinate space, and methods that change its size or location remain
  27. * valid. The behavior of methods that operate on more than one
  28. * <code>Rectangle</code> is undefined if any of the participating
  29. * <code>Rectangle</code> objects has a negative
  30. * <code>width</code> or <code>height</code>. These methods include
  31. * <code>intersects</code>, <code>intersection</code>, and
  32. * <code>union</code>.
  33. *
  34. * @version 1.39, 06/24/98
  35. * @author Sami Shaio
  36. * @since JDK1.0
  37. */
  38. public class Rectangle extends Rectangle2D
  39. implements Shape, java.io.Serializable
  40. {
  41. /**
  42. * The <i>x</i> coordinate of the <code>Rectangle</code>.
  43. *
  44. * @serial
  45. * @see #setLocation(int, int)
  46. * @see #getLocation()
  47. */
  48. public int x;
  49. /**
  50. * The <i>y</i> coordinate of the <code>Rectangle</code>.
  51. *
  52. * @serial
  53. * @see #setLocation(int, int)
  54. * @see #getLocation()
  55. */
  56. public int y;
  57. /**
  58. * The width of the <code>Rectangle</code>.
  59. * @serial
  60. * @see #setSize(int, int)
  61. * @see #getSize()
  62. * @since JDK1.0.
  63. */
  64. public int width;
  65. /**
  66. * The height of the <code>Rectangle</code>.
  67. *
  68. * @serial
  69. * @see #setSize(int, int)
  70. * @see #getSize()
  71. */
  72. public int height;
  73. /*
  74. * JDK 1.1 serialVersionUID
  75. */
  76. private static final long serialVersionUID = -4345857070255674764L;
  77. /**
  78. * Initialize JNI field and method IDs
  79. */
  80. private static native void initIDs();
  81. static {
  82. /* ensure that the necessary native libraries are loaded */
  83. Toolkit.loadLibraries();
  84. initIDs();
  85. }
  86. /**
  87. * Constructs a new <code>Rectangle</code> whose top-left corner
  88. * is at (0, 0) in the coordinate space, and whose width and
  89. * height are both zero.
  90. */
  91. public Rectangle() {
  92. this(0, 0, 0, 0);
  93. }
  94. /**
  95. * Constructs a new <code>Rectangle</code>, initialized to match
  96. * the values of the specificed <code>Rectangle</code>.
  97. * @param r the <code>Rectangle</code> from which to copy initial values
  98. * to a newly constructed <code>Rectangle</code>
  99. * @since JDK1.1
  100. */
  101. public Rectangle(Rectangle r) {
  102. this(r.x, r.y, r.width, r.height);
  103. }
  104. /**
  105. * Constructs a new <code>Rectangle</code> whose top-left corner is
  106. * specified as
  107. * (<code>x</code>, <code>y</code>) and whose width and height
  108. * are specified by the arguments of the same name.
  109. * @param x, y the specified coordinates
  110. * @param width the width of the <code>Rectangle</code>
  111. * @param height the height of the <code>Rectangle</code>
  112. */
  113. public Rectangle(int x, int y, int width, int height) {
  114. this.x = x;
  115. this.y = y;
  116. this.width = width;
  117. this.height = height;
  118. }
  119. /**
  120. * Constructs a new <code>Rectangle</code> whose top-left corner
  121. * is at (0, 0) in the coordinate space, and whose width and
  122. * height are specified by the arguments of the same name.
  123. * @param width the width of the <code>Rectangle</code>
  124. * @param height the height of the <code>Rectangle</code>
  125. */
  126. public Rectangle(int width, int height) {
  127. this(0, 0, width, height);
  128. }
  129. /**
  130. * Constructs a new <code>Rectangle</code> whose top-left corner is
  131. * specified by the {@link Point} argument, and
  132. * whose width and height are specified by the
  133. * {@link Dimension} argument.
  134. * @param p a <code>Point</code> that is the top-left corner of
  135. * the <code>Rectangle</code>
  136. * @param d a <code>Dimension</code>, representing the
  137. * width and height of the <code>Rectangle</code>
  138. */
  139. public Rectangle(Point p, Dimension d) {
  140. this(p.x, p.y, d.width, d.height);
  141. }
  142. /**
  143. * Constructs a new <code>Rectangle</code> whose top-left corner is the
  144. * specified <code>Point</code>, and whose width and height are both zero.
  145. * @param p a <code>Point</code> that is the top left corner
  146. * of the <code>Rectangle</code>
  147. */
  148. public Rectangle(Point p) {
  149. this(p.x, p.y, 0, 0);
  150. }
  151. /**
  152. * Constructs a new <code>Rectangle</code> whose top left corner is
  153. * (0, 0) and whose width and height are specified
  154. * by the <code>Dimension</code> argument.
  155. * @param d a <code>Dimension</code>, specifying width and height
  156. */
  157. public Rectangle(Dimension d) {
  158. this(0, 0, d.width, d.height);
  159. }
  160. /**
  161. * Returns the X coordinate of the bounding <code>Rectangle</code> in
  162. * <code>double</code> precision.
  163. * @return the x coordinate of the bounding <code>Rectangle</code>.
  164. */
  165. public double getX() {
  166. return x;
  167. }
  168. /**
  169. * Returns the Y coordinate of the bounding <code>Rectangle</code> in
  170. * <code>double</code> precision.
  171. * @return the y coordinate of the bounding <code>Rectangle</code>.
  172. */
  173. public double getY() {
  174. return y;
  175. }
  176. /**
  177. * Returns the width of the bounding <code>Rectangle</code> in
  178. * <code>double</code> precision.
  179. * @return the width of the bounding <code>Rectangle</code>.
  180. */
  181. public double getWidth() {
  182. return width;
  183. }
  184. /**
  185. * Returns the height of the bounding <code>Rectangle</code> in
  186. * <code>double</code> precision.
  187. * @return the height of the bounding <code>Rectangle</code>.
  188. */
  189. public double getHeight() {
  190. return height;
  191. }
  192. /**
  193. * Gets the bounding <code>Rectangle</code> of this <code>Rectangle</code>.
  194. * <p>
  195. * This method is included for completeness, to parallel the
  196. * <code>getBounds</code> method of
  197. * {@link Component}.
  198. * @return a new <code>Rectangle</code>, equal to the
  199. * bounding <code>Rectangle</code> for this <code>Rectangle</code>.
  200. * @see java.awt.Component#getBounds
  201. * @since JDK1.1
  202. */
  203. public Rectangle getBounds() {
  204. return new Rectangle(x, y, width, height);
  205. }
  206. /**
  207. * Return the high precision bounding box of this rectangle.
  208. * @since JDK1.2
  209. */
  210. public Rectangle2D getBounds2D() {
  211. return new Rectangle(x, y, width, height);
  212. }
  213. /**
  214. * Sets the bounding <code>Rectangle</code> of this <code>Rectangle</code>
  215. * to match the specified <code>Rectangle</code>.
  216. * <p>
  217. * This method is included for completeness, to parallel the
  218. * <code>setBounds</code> method of <code>Component</code>.
  219. * @param r the specified <code>Rectangle</code>
  220. * @see java.awt.Component#setBounds(java.awt.Rectangle)
  221. * @since JDK1.1
  222. */
  223. public void setBounds(Rectangle r) {
  224. setBounds(r.x, r.y, r.width, r.height);
  225. }
  226. /**
  227. * Sets the bounding <code>Rectangle</code> of this
  228. * <code>Rectangle</code> to the specified
  229. * <code>x</code>, <code>y</code>, <code>width</code>,
  230. * and <code>height</code>.
  231. * <p>
  232. * This method is included for completeness, to parallel the
  233. * <code>setBounds</code> method of <code>Component</code>.
  234. * @param x, y the new coordinates for the top-left
  235. * corner of this <code>Rectangle</code>
  236. * @param width the new width for this <code>Rectangle</code>
  237. * @param height the new height for this <code>Rectangle</code>
  238. * @see java.awt.Component#setBounds(int, int, int, int)
  239. * @since JDK1.1
  240. */
  241. public void setBounds(int x, int y, int width, int height) {
  242. reshape(x, y, width, height);
  243. }
  244. /**
  245. * Sets the bounds of this <code>Rectangle</code> to the specified
  246. * <code>x</code>, <code>y</code>, <code>width</code>,
  247. * and <code>height</code>.
  248. * This method is included for completeness, to parallel the
  249. * <code>setBounds</code> method of <code>Component</code>.
  250. * @param width the new width for the <code>Dimension</code> object
  251. * @param height the new height for the <code>Dimension</code> object
  252. */
  253. public void setRect(double x, double y, double width, double height) {
  254. int x0 = (int) Math.floor(x);
  255. int y0 = (int) Math.floor(y);
  256. int x1 = (int) Math.ceil(x+width);
  257. int y1 = (int) Math.ceil(y+height);
  258. setBounds(x0, y0, x1-x0, y1-y0);
  259. }
  260. /**
  261. * @deprecated As of JDK version 1.1,
  262. * replaced by <code>setBounds(int, int, int, int)</code>.
  263. */
  264. public void reshape(int x, int y, int width, int height) {
  265. this.x = x;
  266. this.y = y;
  267. this.width = width;
  268. this.height = height;
  269. }
  270. /**
  271. * Returns the location of this <code>Rectangle</code>.
  272. * <p>
  273. * This method is included for completeness, to parallel the
  274. * <code>getLocation</code> method of <code>Component</code>.
  275. * @return the <code>Point</code> that is the top-left corner of
  276. * this <code>Rectangle</code>.
  277. * @see java.awt.Component#getLocation
  278. * @since JDK1.1
  279. */
  280. public Point getLocation() {
  281. return new Point(x, y);
  282. }
  283. /**
  284. * Moves this <code>Rectangle</code> to the specified location.
  285. * <p>
  286. * This method is included for completeness, to parallel the
  287. * <code>setLocation</code> method of <code>Component</code>.
  288. * @param p the <code>Point</code> specifying the new location
  289. * for this <code>Rectangle</code>
  290. * @see java.awt.Component#setLocation(java.awt.Point)
  291. * @since JDK1.1
  292. */
  293. public void setLocation(Point p) {
  294. setLocation(p.x, p.y);
  295. }
  296. /**
  297. * Moves this <code>Rectangle</code> to the specified location.
  298. * <p>
  299. * This method is included for completeness, to parallel the
  300. * <code>setLocation</code> method of <code>Component</code>.
  301. * @param x, y the coordinates of the new location
  302. * @see java.awt.Component#setLocation(int, int)
  303. * @since JDK1.1
  304. */
  305. public void setLocation(int x, int y) {
  306. move(x, y);
  307. }
  308. /**
  309. * @deprecated As of JDK version 1.1,
  310. * replaced by <code>setLocation(int, int)</code>.
  311. */
  312. public void move(int x, int y) {
  313. this.x = x;
  314. this.y = y;
  315. }
  316. /**
  317. * Translates this <code>Rectangle</code> the indicated distance,
  318. * to the right along the x coordinate axis, and
  319. * downward along the y coordinate axis.
  320. * @param dx the distance to move this <code>Rectangle</code>
  321. * along the x axis
  322. * @param dy the distance to move this <code>Rectangle</code>
  323. * along the y axis
  324. * @see java.awt.Rectangle#setLocation(int, int)
  325. * @see java.awt.Rectangle#setLocation(java.awt.Point)
  326. */
  327. public void translate(int x, int y) {
  328. this.x += x;
  329. this.y += y;
  330. }
  331. /**
  332. * Gets the size of this <code>Rectangle</code>, represented by
  333. * the returned <code>Dimension</code>.
  334. * <p>
  335. * This method is included for completeness, to parallel the
  336. * <code>getSize</code> method of <code>Component</code>.
  337. * @return a <code>Dimension</code>, representing the size of
  338. * this <code>Rectangle</code>.
  339. * @see java.awt.Component#getSize
  340. * @since JDK1.1
  341. */
  342. public Dimension getSize() {
  343. return new Dimension(width, height);
  344. }
  345. /**
  346. * Sets the size of this <code>Rectangle</code> to match the
  347. * specified <code>Dimension</code>.
  348. * <p>
  349. * This method is included for completeness, to parallel the
  350. * <code>setSize</code> method of <code>Component</code>.
  351. * @param d the new size for the <code>Dimension</code> object
  352. * @see java.awt.Component#setSize(java.awt.Dimension)
  353. * @since JDK1.1
  354. */
  355. public void setSize(Dimension d) {
  356. setSize(d.width, d.height);
  357. }
  358. /**
  359. * Sets the size of this <code>Rectangle</code> to the specified
  360. * width and height.
  361. * <p>
  362. * This method is included for completeness, to parallel the
  363. * <code>setSize</code> method of <code>Component</code>.
  364. * @param width the new width for this <code>Rectangle</code>
  365. * @param height the new height for this <code>Rectangle</code>
  366. * @see java.awt.Component#setSize(int, int)
  367. * @since JDK1.1
  368. */
  369. public void setSize(int width, int height) {
  370. resize(width, height);
  371. }
  372. /**
  373. * @deprecated As of JDK version 1.1,
  374. * replaced by <code>setSize(int, int)</code>.
  375. */
  376. public void resize(int width, int height) {
  377. this.width = width;
  378. this.height = height;
  379. }
  380. /**
  381. * Checks whether or not this <code>Rectangle</code> contains the
  382. * specified <code>Point</code>.
  383. * @param p the <code>Point</code> to test
  384. * @return <code>true</code> if the <code>Point</code>
  385. * (<i>x</i>, <i>y</i>) is inside this
  386. * <code>Rectangle</code>
  387. * <code>false</code> otherwise.
  388. * @since JDK1.1
  389. */
  390. public boolean contains(Point p) {
  391. return contains(p.x, p.y);
  392. }
  393. /**
  394. * Checks whether or not this <code>Rectangle</code> contains the
  395. * point at the specified location
  396. * (<i>x</i>, <i>y</i>).
  397. * @param x, y the specified coordinates
  398. * @return <code>true</code> if the point
  399. * (<i>x</i>, <i>y</i>) is inside this
  400. * <code>Rectangle</code>
  401. * <code>false</code> otherwise.
  402. * @since JDK1.1
  403. */
  404. public boolean contains(int x, int y) {
  405. return inside(x, y);
  406. }
  407. /**
  408. * Checks whether or not this <code>Rectangle</code> entirely contains
  409. * the specified <code>Rectangle</code>.
  410. * @param r the specified <code>Rectangle</code>
  411. * @return <code>true</code> if the <code>Rectangle</code>
  412. * is contained entirely inside this <code>Rectangle</code>
  413. * <code>false</code> otherwise.
  414. * @since JDK1.1
  415. */
  416. public boolean contains(Rectangle r) {
  417. return contains(r.x, r.y, r.width, r.height);
  418. }
  419. /**
  420. * Checks whether this <code>Rectangle</code> entirely contains
  421. * the <code>Rectangle</code>
  422. * at the specified location (<i>X</i>, <i>Y</i>) with the
  423. * specified dimensions (<i>W</i>, <i>H</i>).
  424. * @param x, y the specified coordinates
  425. * @param W the width of the <code>Rectangle</code>
  426. * @param H the height of the <code>Rectangle</code>
  427. * @return <code>true</code> if the <code>Rectangle</code> specified by
  428. * (<i>X</i>, <i>Y</i>, <i>W</i>, <i>H</i>)
  429. * is entirely enclosed inside this <code>Rectangle</code>
  430. * <code>false</code> otherwise.
  431. * @since JDK1.1
  432. */
  433. public boolean contains(int X, int Y, int W, int H) {
  434. int width = this.width;
  435. int height = this.height;
  436. if (width <= 0 || height <= 0 || W <= 0 || H <= 0) {
  437. return false;
  438. }
  439. int x = this.x;
  440. int y = this.y;
  441. return (X >= x &&
  442. Y >= y &&
  443. X + W <= x + width &&
  444. Y + H <= y + height);
  445. }
  446. /**
  447. * @deprecated As of JDK version 1.1,
  448. * replaced by <code>contains(int, int)</code>.
  449. */
  450. public boolean inside(int x, int y) {
  451. return (x >= this.x) && ((x - this.x) < this.width) && (y >= this.y) && ((y-this.y) < this.height);
  452. }
  453. /**
  454. * Determines whether or not this <code>Rectangle</code> and the specified
  455. * <code>Rectangle</code> intersect. Two rectangles intersect if
  456. * their intersection is nonempty.
  457. * @param r the specified <code>Rectangle</code>
  458. * @return <code>true</code> if the specified <code>Rectangle</code>
  459. * and this <code>Rectangle</code> insersect;
  460. * <code>false</code> otherwise.
  461. */
  462. public boolean intersects(Rectangle r) {
  463. return !((r.x + r.width <= x) ||
  464. (r.y + r.height <= y) ||
  465. (r.x >= x + width) ||
  466. (r.y >= y + height));
  467. }
  468. /**
  469. * Computes the intersection of this <code>Rectangle</code> with the
  470. * specified <code>Rectangle</code>. Returns a new <code>Rectangle</code>
  471. * that represents the intersection of the two rectangles.
  472. * @param r the specified <code>Rectangle</code>
  473. * @return the largest <code>Rectangle</code> contained in both the
  474. * specified <code>Rectangle</code> and in
  475. * this<code>Rectangle</code>.
  476. */
  477. public Rectangle intersection(Rectangle r) {
  478. int x1 = Math.max(x, r.x);
  479. int x2 = Math.min(x + width, r.x + r.width);
  480. int y1 = Math.max(y, r.y);
  481. int y2 = Math.min(y + height, r.y + r.height);
  482. if (((x2 - x1) < 0) || ((y2 - y1) < 0))
  483. // Width or height is negative. No intersection.
  484. return new Rectangle(0,0,0,0);
  485. else
  486. return new Rectangle(x1, y1, x2 - x1, y2 - y1);
  487. }
  488. /**
  489. * Computes the union of this <code>Rectangle</code> with the
  490. * specified <code>Rectangle</code>. Returns a new
  491. * <code>Rectangle</code> that
  492. * represents the union of the two rectangles
  493. * @param r the specified <code>Rectangle</code>
  494. * @return the smallest <code>Rectangle</code> containing both
  495. * the specified <code>Rectangle</code> and this
  496. * <code>Rectangle</code>.
  497. */
  498. public Rectangle union(Rectangle r) {
  499. int x1 = Math.min(x, r.x);
  500. int x2 = Math.max(x + width, r.x + r.width);
  501. int y1 = Math.min(y, r.y);
  502. int y2 = Math.max(y + height, r.y + r.height);
  503. return new Rectangle(x1, y1, x2 - x1, y2 - y1);
  504. }
  505. /**
  506. * Adds a point, specified by the integer arguments <code>newx</code>
  507. * and <code>newy</code>, to this <code>Rectangle</code>. The
  508. * resulting <code>Rectangle</code> is
  509. * the smallest <code>Rectangle</code> that contains both the
  510. * original <code>Rectangle</code> and the specified point.
  511. * <p>
  512. * After adding a point, a call to <code>contains</code> with the
  513. * added point as an argument does not necessarily return
  514. * <code>true</code>. The <code>contains</code> method does not
  515. * return <code>true</code> for points on the right or bottom
  516. * edges of a <code>Rectangle</code>. Therefore, if the added point
  517. * falls on the right or bottom edge of the enlarged
  518. * <code>Rectangle</code>, <code>contains</code> returns
  519. * <code>false</code> for that point.
  520. * @param newx, newy the coordinates of the new point
  521. */
  522. public void add(int newx, int newy) {
  523. int x1 = Math.min(x, newx);
  524. int x2 = Math.max(x + width, newx);
  525. int y1 = Math.min(y, newy);
  526. int y2 = Math.max(y + height, newy);
  527. x = x1;
  528. y = y1;
  529. width = x2 - x1;
  530. height = y2 - y1;
  531. }
  532. /**
  533. * Adds the specified <code>Point</code> to this
  534. * <code>Rectangle</code>. The resulting <code>Rectangle</code>
  535. * is the smallest <code>Rectangle</code> that contains both the
  536. * original <code>Rectangle</code> and the specified
  537. * <code>Point</code>.
  538. * <p>
  539. * After adding a <code>Point</code>, a call to <code>contains</code>
  540. * with the added <code>Point</code> as an argument does not
  541. * necessarily return <code>true</code>. The <code>contains</code>
  542. * method does not return <code>true</code> for points on the right
  543. * or bottom edges of a <code>Rectangle</code>. Therefore if the added
  544. * <code>Point</code> falls on the right or bottom edge of the
  545. * enlarged <code>Rectangle</code>, <code>contains</code> returns
  546. * <code>false</code> for that <code>Point</code>.
  547. * @param pt the new <code>Point</code> to add to this
  548. * <code>Rectangle</code>
  549. */
  550. public void add(Point pt) {
  551. add(pt.x, pt.y);
  552. }
  553. /**
  554. * Adds a <code>Rectangle</code> to this <code>Rectangle</code>.
  555. * The resulting <code>Rectangle</code> is the union of the two
  556. * rectangles.
  557. * @param r the specified <code>Rectangle</code>
  558. */
  559. public void add(Rectangle r) {
  560. int x1 = Math.min(x, r.x);
  561. int x2 = Math.max(x + width, r.x + r.width);
  562. int y1 = Math.min(y, r.y);
  563. int y2 = Math.max(y + height, r.y + r.height);
  564. x = x1;
  565. y = y1;
  566. width = x2 - x1;
  567. height = y2 - y1;
  568. }
  569. /**
  570. * Resizes the <code>Rectangle</code> both horizontally and vertically.
  571. * <p>
  572. * This method modifies the <code>Rectangle</code> so that it is
  573. * <code>h</code> units larger on both the left and right side,
  574. * and <code>v</code> units larger at both the top and bottom.
  575. * <p>
  576. * The new <code>Rectangle</code> has (<code>x - h</code>,
  577. * <code>y - v</code>) as its top-left corner, a
  578. * width of
  579. * <code>width</code> <code>+</code> <code>2h</code>,
  580. * and a height of
  581. * <code>height</code> <code>+</code> <code>2v</code>.
  582. * <p>
  583. * If negative values are supplied for <code>h</code> and
  584. * <code>v</code>, the size of the <code>Rectangle</code>
  585. * decreases accordingly.
  586. * The <code>grow</code> method does not check whether the resulting
  587. * values of <code>width</code> and <code>height</code> are
  588. * non-negative.
  589. * @param h the horizontal expansion
  590. * @param v the vertical expansion
  591. */
  592. public void grow(int h, int v) {
  593. x -= h;
  594. y -= v;
  595. width += h * 2;
  596. height += v * 2;
  597. }
  598. /**
  599. * Determines whether or not this <code>Rectangle</code> is empty. A
  600. * <code>Rectangle</code> is empty if its width or its height is less
  601. * than or equal to zero.
  602. * @return <code>true</code> if this <code>Rectangle</code> is empty;
  603. * <code>false</code> otherwise.
  604. */
  605. public boolean isEmpty() {
  606. return (width <= 0) || (height <= 0);
  607. }
  608. /**
  609. * Determines where the specified coordinates lie with respect
  610. * to this <code>Rectangle</code>.
  611. * This method computes a binary OR of the appropriate mask values
  612. * indicating, for each side of this <code>Rectangle</code>,
  613. * whether or not the specified coordinates are on the same side of the
  614. * edge as the rest of this <code>Rectangle</code>.
  615. * @param x, y the specified coordinates
  616. * @return the logical OR of all appropriate out codes.
  617. * @see #OUT_LEFT
  618. * @see #OUT_TOP
  619. * @see #OUT_RIGHT
  620. * @see #OUT_BOTTOM
  621. * @since JDK1.2
  622. */
  623. public int outcode(double x, double y) {
  624. int out = 0;
  625. if (this.width <= 0) {
  626. out |= OUT_LEFT | OUT_RIGHT;
  627. } else if (x < this.x) {
  628. out |= OUT_LEFT;
  629. } else if (x > this.x + this.width) {
  630. out |= OUT_RIGHT;
  631. }
  632. if (this.height <= 0) {
  633. out |= OUT_TOP | OUT_BOTTOM;
  634. } else if (y < this.y) {
  635. out |= OUT_TOP;
  636. } else if (y > this.y + this.height) {
  637. out |= OUT_BOTTOM;
  638. }
  639. return out;
  640. }
  641. /**
  642. * Returns a new {@link Rectangle2D} object
  643. * representing the intersection of this <code>Rectangle</code> with the
  644. * specified <code>Rectangle2D</code>.
  645. * @param r the <code>Rectangle2D</code> to be intersected
  646. * with this <code>Rectangle</code>
  647. * @return the largest <code>Rectangle2D</code> contained in both the
  648. * specified <code>Rectangle2D</code> and in
  649. * this <code>Rectangle</code>.
  650. * @since JDK1.2
  651. */
  652. public Rectangle2D createIntersection(Rectangle2D r) {
  653. if (r instanceof Rectangle) {
  654. return intersection((Rectangle) r);
  655. }
  656. Rectangle2D dest = new Rectangle2D.Double();
  657. Rectangle2D.intersect(this, r, dest);
  658. return dest;
  659. }
  660. /**
  661. * Returns a new <code>Rectangle2D</code> object representing the
  662. * union of this <code>Rectangle</code> with the specified
  663. * <code>Rectangle2D</code>.
  664. * @param r the <code>Rectangle2D</code> to be combined with
  665. * this <code>Rectangle</code>
  666. * @return the smallest <code>Rectangle2D</code> containing
  667. * both the specified <code>Rectangle2D</code> and this
  668. * <code>Rectangle</code>.
  669. * @since JDK1.2
  670. */
  671. public Rectangle2D createUnion(Rectangle2D r) {
  672. if (r instanceof Rectangle) {
  673. return union((Rectangle) r);
  674. }
  675. Rectangle2D dest = new Rectangle2D.Double();
  676. Rectangle2D.union(this, r, dest);
  677. return dest;
  678. }
  679. /**
  680. * Checks whether two rectangles are equal.
  681. * <p>
  682. * The result is <code>true</code> if and only if the argument is not
  683. * <code>null</code> and is a <code>Rectangle</code> object that has the
  684. * same top-left corner, width, and height as this <code>Rectangle</code>.
  685. * @param obj the <code>Object</code> to compare with
  686. * this <code>Rectangle</code>
  687. * @return <code>true</code> if the objects are equal;
  688. * <code>false</code> otherwise.
  689. */
  690. public boolean equals(Object obj) {
  691. if (obj instanceof Rectangle) {
  692. Rectangle r = (Rectangle)obj;
  693. return ((x == r.x) &&
  694. (y == r.y) &&
  695. (width == r.width) &&
  696. (height == r.height));
  697. }
  698. return super.equals(obj);
  699. }
  700. /**
  701. * Returns a <code>String</code> representing this
  702. * <code>Rectangle</code> and its values.
  703. * @return a <code>String</code> representing this
  704. * <code>Rectangle</code> object's coordinate and size values.
  705. */
  706. public String toString() {
  707. return getClass().getName() + "[x=" + x + ",y=" + y + ",width=" + width + ",height=" + height + "]";
  708. }
  709. }