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