1. /*
  2. * @(#)RoundRectangle2D.java 1.14 00/02/02
  3. *
  4. * Copyright 1997-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.geom;
  11. /**
  12. * The <code>RoundRectangle2D</code> class defines a rectangle with
  13. * rounded corners defined by a location (x, y), a
  14. * dimension (w x h), and the width and height of an arc
  15. * with which to round the corners.
  16. * <p>
  17. * This class is the abstract superclass for all objects that
  18. * store a 2D rounded rectangle.
  19. * The actual storage representation of the coordinates is left to
  20. * the subclass.
  21. *
  22. * @version 1.14, 02/02/00
  23. * @author Jim Graham
  24. */
  25. public abstract class RoundRectangle2D extends RectangularShape {
  26. /**
  27. * The <code>Float</code> class defines a rectangle with rounded
  28. * corners all specified in <code>float</code> coordinates.
  29. */
  30. public static class Float extends RoundRectangle2D {
  31. /**
  32. * The X coordinate of this <code>RoundRectangle2D</code>.
  33. */
  34. public float x;
  35. /**
  36. * The Y coordinate of this <code>RoundRectangle2D</code>.
  37. */
  38. public float y;
  39. /**
  40. * The width of this <code>RoundRectangle2D</code>.
  41. */
  42. public float width;
  43. /**
  44. * The height of this <code>RoundRectangle2D</code>.
  45. */
  46. public float height;
  47. /**
  48. * The width of the arc that rounds off the corners.
  49. */
  50. public float arcwidth;
  51. /**
  52. * The height of the arc that rounds off the corners.
  53. */
  54. public float archeight;
  55. /**
  56. * Constructs a new <code>RoundRectangle2D</code>, initialized to
  57. * location (0.0, 0), size (0.0, 0.0), and corner arcs
  58. * of radius 0.0.
  59. */
  60. public Float() {
  61. }
  62. /**
  63. * Constructs and initializes a <code>RoundRectangle2D</code>
  64. * from the specified coordinates.
  65. * @param x,  the coordinates to which to set the newly
  66. * constructed <code>RoundRectangle2D</code>
  67. * @param w the width to which to set the newly
  68. * constructed <code>RoundRectangle2D</code>
  69. * @param h the height to which to set the newly
  70. * constructed <code>RoundRectangle2D</code>
  71. * @param arcw the width of the arc to use to round off the
  72. * corners of the newly constructed <code>RoundRectangle2D</code>
  73. * @param arch the height of the arc to use to round off the
  74. * corners of the newly constructed <code>RoundRectangle2D</code>
  75. */
  76. public Float(float x, float y, float w, float h,
  77. float arcw, float arch) {
  78. setRoundRect(x, y, w, h, arcw, arch);
  79. }
  80. /**
  81. * Returns the X coordinate of this <code>RoundRectangle2D</code>
  82. * in <code>double</code> precision.
  83. * @return the X coordinate of this <code>RoundRectangle2D</code>.
  84. */
  85. public double getX() {
  86. return (double) x;
  87. }
  88. /**
  89. * Returns the Y coordinate of this <code>RoundRectangle2D</code>
  90. * in <code>double</code> precision.
  91. * @return the Y coordinate of this <code>RoundRectangle2D</code>.
  92. */
  93. public double getY() {
  94. return (double) y;
  95. }
  96. /**
  97. * Returns the width of this <code>RoundRectangle2D</code>
  98. * in <code>double</code> precision.
  99. * @return the width of this <code>RoundRectangle2D</code>.
  100. */
  101. public double getWidth() {
  102. return (double) width;
  103. }
  104. /**
  105. * Returns the height of this <code>RoundRectangle2D</code>
  106. * in <code>double</code> precision.
  107. * @return the height of this <code>RoundRectangle2D</code>.
  108. */
  109. public double getHeight() {
  110. return (double) height;
  111. }
  112. /**
  113. * Returns the width of the arc that rounds off the corners.
  114. * @return the width of the arc that rounds off the corners
  115. * of this <code>RoundRectangle2D</code>.
  116. */
  117. public double getArcWidth() {
  118. return (double) arcwidth;
  119. }
  120. /**
  121. * Returns the height of the arc that rounds off the corners.
  122. * @return the height of the arc that rounds off the corners
  123. * of this <code>RoundRectangle2D</code>.
  124. */
  125. public double getArcHeight() {
  126. return (double) archeight;
  127. }
  128. /**
  129. * Determines whether or not this <code>RoundRectangle2D</code>
  130. * is empty.
  131. * @return <code>true</code> if this <code>RoundRectangle2D</code>
  132. * is empty; <code>false</code> othwerwise.
  133. */
  134. public boolean isEmpty() {
  135. return (width <= 0.0f) || (height <= 0.0f);
  136. }
  137. /**
  138. * Sets the location, size, and arc radii of this
  139. * <code>RoundRectangle2D</code> to the
  140. * specified <code>float</code> values.
  141. * @param x, y the coordinates to which to set the
  142. * location of this <code>RoundRectangle2D</code>
  143. * @param w the width to which to set this
  144. * <code>RoundRectangle2D</code>
  145. * @param h the height to which to set this
  146. * <code>RoundRectangle2D</code>
  147. * @param arcw the width to which to set the arc of this
  148. * <code>RoundRectangle2D</code>
  149. * @param arch the height to which to set the arc of this
  150. * <code>RoundRectangle2D</code>
  151. */
  152. public void setRoundRect(float x, float y, float w, float h,
  153. float arcw, float arch) {
  154. this.x = x;
  155. this.y = y;
  156. this.width = w;
  157. this.height = h;
  158. this.arcwidth = arcw;
  159. this.archeight = arch;
  160. }
  161. /**
  162. * Sets the location, size, and arc radii of this
  163. * <code>RoundRectangle2D</code> to the
  164. * specified <code>double</code> values.
  165. * @param x, y the coordinates to which to set the
  166. * location of this <code>RoundRectangle2D</code>
  167. * @param w the width to which to set this
  168. * <code>RoundRectangle2D</code>
  169. * @param h the height to which to set this
  170. * <code>RoundRectangle2D</code>
  171. * @param arcw the width to which to set the arc of this
  172. * <code>RoundRectangle2D</code>
  173. * @param arch the height to which to set the arc of this
  174. * <code>RoundRectangle2D</code>
  175. */
  176. public void setRoundRect(double x, double y, double w, double h,
  177. double arcw, double arch) {
  178. this.x = (float) x;
  179. this.y = (float) y;
  180. this.width = (float) w;
  181. this.height = (float) h;
  182. this.arcwidth = (float) arcw;
  183. this.archeight = (float) arch;
  184. }
  185. /**
  186. * Sets this <code>RoundRectangle2D</code> to be the same as the
  187. * specified <code>RoundRectangle2D</code>.
  188. * @param rr the specified <code>RoundRectangle2D</code>
  189. */
  190. public void setRoundRect(RoundRectangle2D rr) {
  191. this.x = (float) rr.getX();
  192. this.y = (float) rr.getY();
  193. this.width = (float) rr.getWidth();
  194. this.height = (float) rr.getHeight();
  195. this.arcwidth = (float) rr.getArcWidth();
  196. this.archeight = (float) rr.getArcHeight();
  197. }
  198. /**
  199. * Returns the high precision bounding box of this
  200. * <code>RoundRectangle2D</code>.
  201. * @return a {@link Rectangle2D} that is the bounding
  202. * box of this <code>RoundRectangle2D</code>.
  203. */
  204. public Rectangle2D getBounds2D() {
  205. return new Rectangle2D.Float(x, y, width, height);
  206. }
  207. }
  208. /**
  209. * The <code>Double</code> class defines a rectangle with rounded
  210. * corners all specified in <code>double</code> coordinates.
  211. */
  212. public static class Double extends RoundRectangle2D {
  213. /**
  214. * The X coordinate of this <code>RoundRectangle2D</code>.
  215. */
  216. public double x;
  217. /**
  218. * The Y coordinate of this <code>RoundRectangle2D</code>.
  219. */
  220. public double y;
  221. /**
  222. * The width of this <code>RoundRectangle2D</code>.
  223. */
  224. public double width;
  225. /**
  226. * The height of this <code>RoundRectangle2D</code>.
  227. */
  228. public double height;
  229. /**
  230. * The width of the arc that rounds off the corners.
  231. */
  232. public double arcwidth;
  233. /**
  234. * The height of the arc that rounds off the corners.
  235. */
  236. public double archeight;
  237. /**
  238. * Constructs a new <code>RoundRectangle2D</code>, initialized to
  239. * location (0.0, 0), size (0.0, 0.0), and corner arcs
  240. * of radius 0.0.
  241. */
  242. public Double() {
  243. }
  244. /**
  245. * Constructs and initializes a <code>RoundRectangle2D</code>
  246. * from the specified coordinates.
  247. * @param x,  the coordinates to which to set the newly
  248. * constructed <code>RoundRectangle2D</code>
  249. * @param w the width to which to set the newly
  250. * constructed <code>RoundRectangle2D</code>
  251. * @param h the height to which to set the newly
  252. * constructed <code>RoundRectangle2D</code>
  253. * @param arcw the width of the arc to use to round off the
  254. * corners of the newly constructed <code>RoundRectangle2D</code>
  255. * @param arch the height of the arc to use to round off the
  256. * corners of the newly constructed <code>RoundRectangle2D</code>
  257. */
  258. public Double(double x, double y, double w, double h,
  259. double arcw, double arch) {
  260. setRoundRect(x, y, w, h, arcw, arch);
  261. }
  262. /**
  263. * Returns the X coordinate of this <code>RoundRectangle2D</code>
  264. * in <code>double</code> precision.
  265. * @return the X coordinate of this <code>RoundRectangle2D</code>.
  266. */
  267. public double getX() {
  268. return x;
  269. }
  270. /**
  271. * Returns the Y coordinate of this <code>RoundRectangle2D</code>
  272. * in <code>double</code> precision.
  273. * @return the Y coordinate of this <code>RoundRectangle2D</code>.
  274. */
  275. public double getY() {
  276. return y;
  277. }
  278. /**
  279. * Returns the width of this <code>RoundRectangle2D</code>
  280. * in <code>double</code> precision.
  281. * @return the width of this <code>RoundRectangle2D</code>.
  282. */
  283. public double getWidth() {
  284. return width;
  285. }
  286. /**
  287. * Returns the height of this <code>RoundRectangle2D</code>
  288. * in <code>double</code> precision.
  289. * @return the height of this <code>RoundRectangle2D</code>.
  290. */
  291. public double getHeight() {
  292. return height;
  293. }
  294. /**
  295. * Returns the width of the arc that rounds off the corners.
  296. * @return the width of the arc that rounds off the corners
  297. * of this <code>RoundRectangle2D</code>.
  298. */
  299. public double getArcWidth() {
  300. return arcwidth;
  301. }
  302. /**
  303. * Returns the height of the arc that rounds off the corners.
  304. * @return the height of the arc that rounds off the corners
  305. * of this <code>RoundRectangle2D</code>.
  306. */
  307. public double getArcHeight() {
  308. return archeight;
  309. }
  310. /**
  311. * Determines whether or not this <code>RoundRectangle2D</code>
  312. * is empty.
  313. * @return <code>true</code> if this <code>RoundRectangle2D</code>
  314. * is empty; <code>false</code> othwerwise.
  315. */
  316. public boolean isEmpty() {
  317. return (width <= 0.0f) || (height <= 0.0f);
  318. }
  319. /**
  320. * Sets the location, size, and arc radii of this
  321. * <code>RoundRectangle2D</code> to the
  322. * specified <code>double</code> values.
  323. * @param x, y the coordinates to which to set the
  324. * location of this <code>RoundRectangle2D</code>
  325. * @param w the width to which to set this
  326. * <code>RoundRectangle2D</code>
  327. * @param h the height to which to set this
  328. * <code>RoundRectangle2D</code>
  329. * @param arcw the width to which to set the arc of this
  330. * <code>RoundRectangle2D</code>
  331. * @param arch the height to which to set the arc of this
  332. * <code>RoundRectangle2D</code>
  333. */
  334. public void setRoundRect(double x, double y, double w, double h,
  335. double arcw, double arch) {
  336. this.x = x;
  337. this.y = y;
  338. this.width = w;
  339. this.height = h;
  340. this.arcwidth = arcw;
  341. this.archeight = arch;
  342. }
  343. /**
  344. * Sets this <code>RoundRectangle2D</code> to be the same as the
  345. * specified <code>RoundRectangle2D</code>.
  346. * @param rr the specified <code>RoundRectangle2D</code>
  347. */
  348. public void setRoundRect(RoundRectangle2D rr) {
  349. this.x = rr.getX();
  350. this.y = rr.getY();
  351. this.width = rr.getWidth();
  352. this.height = rr.getHeight();
  353. this.arcwidth = rr.getArcWidth();
  354. this.archeight = rr.getArcHeight();
  355. }
  356. /**
  357. * Returns the high precision bounding box of this
  358. * <code>RoundRectangle2D</code>.
  359. * @return a {@link Rectangle2D} that is the bounding
  360. * box of this <code>RoundRectangle2D</code>.
  361. */
  362. public Rectangle2D getBounds2D() {
  363. return new Rectangle2D.Double(x, y, width, height);
  364. }
  365. }
  366. /**
  367. * This is an abstract class that cannot be instantiated directly.
  368. * Type-specific implementation subclasses are available for
  369. * instantiation and provide a number of formats for storing
  370. * the information necessary to satisfy the various accessor
  371. * methods below.
  372. *
  373. * @see java.awt.geom.RoundRectangle2D.Float
  374. * @see java.awt.geom.RoundRectangle2D.Double
  375. */
  376. protected RoundRectangle2D() {
  377. }
  378. /**
  379. * Gets the width of the arc that rounds off the corners.
  380. * @return the width of the arc that rounds off the corners
  381. * of this <code>RoundRectangle2D</code>.
  382. */
  383. public abstract double getArcWidth();
  384. /**
  385. * Gets the height of the arc that rounds off the corners.
  386. * @return the height of the arc that rounds off the corners
  387. * of this <code>RoundRectangle2D</code>.
  388. */
  389. public abstract double getArcHeight();
  390. /**
  391. * Sets the location, size, and corner radii of this
  392. * <code>RoundRectangle2D</code> to the specified
  393. * <code>double</code> values.
  394. * @param x, y the coordinates to which to set the
  395. * location of this <code>RoundRectangle2D</code>
  396. * @param w the width to which to set this
  397. * <code>RoundRectangle2D</code>
  398. * @param h the height to which to set this
  399. * <code>RoundRectangle2D</code>
  400. * @param arcWidth the width to which to set the arc of this
  401. * <code>RoundRectangle2D</code>
  402. * @param arcHeight the height to which to set the arc of this
  403. * <code>RoundRectangle2D</code>
  404. */
  405. public abstract void setRoundRect(double x, double y, double w, double h,
  406. double arcWidth, double arcHeight);
  407. /**
  408. * Sets this <code>RoundRectangle2D</code> to be the same as the
  409. * specified <code>RoundRectangle2D</code>.
  410. * @param rr the specified <code>RoundRectangle2D</code>
  411. */
  412. public void setRoundRect(RoundRectangle2D rr) {
  413. setRoundRect(rr.getX(), rr.getY(), rr.getWidth(), rr.getHeight(),
  414. rr.getArcWidth(), rr.getArcHeight());
  415. }
  416. /**
  417. * Sets the location and size of the outer bounds of this
  418. * <code>RoundRectangle2D</code> to the specified rectangular values.
  419. * @param x, y the coordinates to which to set the location
  420. * of this <code>RoundRectangle2D</code>
  421. * @param w the width to which to set this
  422. * <code>RoundRectangle2D</code>
  423. * @param h the height to which to set this
  424. * <code>RoundRectangle2D</code>
  425. */
  426. public void setFrame(double x, double y, double w, double h) {
  427. setRoundRect(x, y, w, h, getArcWidth(), getArcHeight());
  428. }
  429. /**
  430. * Tests if the specified coordinates are inside the boundary of
  431. * this <code>RoundRectangle2D</code>.
  432. * @param x, y the coordinates to test
  433. * @return <code>true</code> if the specified coordinates are
  434. * inside the boundary of this <code>RoundRectangle2D</code>
  435. * <code>false</code> otherwise.
  436. */
  437. public boolean contains(double x, double y) {
  438. if (isEmpty()) {
  439. return false;
  440. }
  441. double rrx0 = getX();
  442. double rry0 = getY();
  443. double rrx1 = rrx0 + getWidth();
  444. double rry1 = rry0 + getHeight();
  445. // Check for trivial rejection - point is outside bounding rectangle
  446. if (x < rrx0 || y < rry0 || x >= rrx1 || y >= rry1) {
  447. return false;
  448. }
  449. double aw = Math.min(getWidth(), Math.abs(getArcWidth())) / 2.0;
  450. double ah = Math.min(getHeight(), Math.abs(getArcHeight())) / 2.0;
  451. // Check which corner point is in and do circular containment
  452. // test - otherwise simple acceptance
  453. if (x >= (rrx0 += aw) && x < (rrx0 = rrx1 - aw)) {
  454. return true;
  455. }
  456. if (y >= (rry0 += ah) && y < (rry0 = rry1 - ah)) {
  457. return true;
  458. }
  459. x = (x - rrx0) / aw;
  460. y = (y - rry0) / ah;
  461. return (x * x + y * y <= 1.0);
  462. }
  463. private int classify(double coord, double left, double right,
  464. double arcsize) {
  465. if (coord < left) {
  466. return 0;
  467. } else if (coord < left + arcsize) {
  468. return 1;
  469. } else if (coord < right - arcsize) {
  470. return 2;
  471. } else if (coord < right) {
  472. return 3;
  473. } else {
  474. return 4;
  475. }
  476. }
  477. /**
  478. * Tests if the interior of this <code>RoundRectangle2D</code>
  479. * intersects the interior of a specified set of rectangular
  480. * coordinates.
  481. * @param x, y the coordinates of the upper left corner
  482. * of the specified set of rectangular coordinates
  483. * @param w the width of the specified set of rectangular
  484. * coordinates
  485. * @param h the height of the specified set of rectangular
  486. * coordinates
  487. * @return <code>true</code> if the interior of this
  488. * <code>RoundRectangle2D</code> intersects the interior of the
  489. * specified set of rectangular coordinates.
  490. */
  491. public boolean intersects(double x, double y, double w, double h) {
  492. if (isEmpty() || w <= 0 || h <= 0) {
  493. return false;
  494. }
  495. double rrx0 = getX();
  496. double rry0 = getY();
  497. double rrx1 = rrx0 + getWidth();
  498. double rry1 = rry0 + getHeight();
  499. // Check for trivial rejection - bounding rectangles do not intersect
  500. if (x + w <= rrx0 || x >= rrx1 || y + h <= rry0 || y >= rry1) {
  501. return false;
  502. }
  503. double aw = Math.min(getWidth(), Math.abs(getArcWidth())) / 2.0;
  504. double ah = Math.min(getHeight(), Math.abs(getArcHeight())) / 2.0;
  505. int x0class = classify(x, rrx0, rrx1, aw);
  506. int x1class = classify(x + w, rrx0, rrx1, aw);
  507. int y0class = classify(y, rry0, rry1, ah);
  508. int y1class = classify(y + h, rry0, rry1, ah);
  509. // Trivially accept if any point is inside inner rectangle
  510. if (x0class == 2 || x1class == 2 || y0class == 2 || y1class == 2) {
  511. return true;
  512. }
  513. // Trivially accept if either edge spans inner rectangle
  514. if ((x0class < 2 && x1class > 2) || (y0class < 2 && y1class > 2)) {
  515. return true;
  516. }
  517. // Since neither edge spans the center, then one of the corners
  518. // must be in one of the rounded edges. We detect this case if
  519. // a [xy]0class is 3 or a [xy]1class is 1. One of those two cases
  520. // must be true for each direction.
  521. // We now find a "nearest point" to test for being inside a rounded
  522. // corner.
  523. x = (x1class == 1) ? (x = x + w - (rrx0 + aw)) : (x = x - (rrx1 - aw));
  524. y = (y1class == 1) ? (y = y + h - (rry0 + ah)) : (y = y - (rry1 - ah));
  525. x = x / aw;
  526. y = y / ah;
  527. return (x * x + y * y <= 1.0);
  528. }
  529. /**
  530. * Tests if the interior of this <code>RoundRectangle2D</code>
  531. * entirely contains the specified set of rectangular coordinates.
  532. * @param x, y the coordinates of the specified set of
  533. * rectangular coordinates
  534. * @param w the width of the specified set of rectangular
  535. * coordinates
  536. * @param h the height of the specified set of rectangular
  537. * coordinates
  538. * @return <code>true</code> if the interior of this
  539. * <code>RoundRectangle2D</code> entirely contains the specified
  540. * set of rectangular coordinates; <code>false</code> otherwise.
  541. */
  542. public boolean contains(double x, double y, double w, double h) {
  543. if (isEmpty() || w <= 0 || h <= 0) {
  544. return false;
  545. }
  546. return (contains(x, y) &&
  547. contains(x + w, y) &&
  548. contains(x, y + h) &&
  549. contains(x + w, y + h));
  550. }
  551. /**
  552. * Returns an iteration object that defines the boundary of this
  553. * <code>RoundRectangle2D</code>.
  554. * The iterator for this class is multi-threaded safe, which means
  555. * that this <code>RoundRectangle2D</code> class guarantees that
  556. * modifications to the geometry of this <code>RoundRectangle2D</code>
  557. * object do not affect any iterations of that geometry that
  558. * are already in process.
  559. * @param at an optional <code>AffineTransform</code> to be applied to
  560. * the coordinates as they are returned in the iteration, or
  561. * <code>null</code> if untransformed coordinates are desired
  562. * @return the <code>PathIterator</code> object that returns the
  563. * geometry of the outline of this
  564. * <code>RoundRectangle2D</code>, one segment at a time.
  565. */
  566. public PathIterator getPathIterator(AffineTransform at) {
  567. return new RoundRectIterator(this, at);
  568. }
  569. }