1. /*
  2. * @(#)RoundRectIterator.java 1.7 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. import java.util.*;
  12. /**
  13. * A utility class to iterate over the path segments of an rounded rectangle
  14. * through the PathIterator interface.
  15. *
  16. * @version 10 Feb 1997
  17. * @author Jim Graham
  18. */
  19. class RoundRectIterator implements PathIterator {
  20. double x, y, w, h, aw, ah;
  21. AffineTransform affine;
  22. int index;
  23. RoundRectIterator(RoundRectangle2D rr, AffineTransform at) {
  24. this.x = rr.getX();
  25. this.y = rr.getY();
  26. this.w = rr.getWidth();
  27. this.h = rr.getHeight();
  28. this.aw = Math.min(w, Math.abs(rr.getArcWidth()));
  29. this.ah = Math.min(h, Math.abs(rr.getArcHeight()));
  30. this.affine = at;
  31. if (aw < 0 || ah < 0) {
  32. // Don't draw anything...
  33. index = ctrlpts.length;
  34. }
  35. }
  36. /**
  37. * Return the winding rule for determining the insideness of the
  38. * path.
  39. * @see #WIND_EVEN_ODD
  40. * @see #WIND_NON_ZERO
  41. */
  42. public int getWindingRule() {
  43. return WIND_NON_ZERO;
  44. }
  45. /**
  46. * Tests if there are more points to read.
  47. * @return true if there are more points to read
  48. */
  49. public boolean isDone() {
  50. return index >= ctrlpts.length;
  51. }
  52. /**
  53. * Moves the iterator to the next segment of the path forwards
  54. * along the primary direction of traversal as long as there are
  55. * more points in that direction.
  56. */
  57. public void next() {
  58. index++;
  59. }
  60. private static final double angle = Math.PI / 4.0;
  61. private static final double a = 1.0 - Math.cos(angle);
  62. private static final double b = Math.tan(angle);
  63. private static final double c = Math.sqrt(1.0 + b * b) - 1 + a;
  64. private static final double cv = 4.0 / 3.0 * a * b / c;
  65. private static final double acv = (1.0 - cv) / 2.0;
  66. // For each array:
  67. // 4 values for each point {v0, v1, v2, v3}:
  68. // point = (x + v0 * w + v1 * arcWidth,
  69. // y + v2 * h + v3 * arcHeight);
  70. private static double ctrlpts[][] = {
  71. { 0.0, 0.0, 0.0, 0.5 },
  72. { 0.0, 0.0, 1.0, -0.5 },
  73. { 0.0, 0.0, 1.0, -acv,
  74. 0.0, acv, 1.0, 0.0,
  75. 0.0, 0.5, 1.0, 0.0 },
  76. { 1.0, -0.5, 1.0, 0.0 },
  77. { 1.0, -acv, 1.0, 0.0,
  78. 1.0, 0.0, 1.0, -acv,
  79. 1.0, 0.0, 1.0, -0.5 },
  80. { 1.0, 0.0, 0.0, 0.5 },
  81. { 1.0, 0.0, 0.0, acv,
  82. 1.0, -acv, 0.0, 0.0,
  83. 1.0, -0.5, 0.0, 0.0 },
  84. { 0.0, 0.5, 0.0, 0.0 },
  85. { 0.0, acv, 0.0, 0.0,
  86. 0.0, 0.0, 0.0, acv,
  87. 0.0, 0.0, 0.0, 0.5 },
  88. {},
  89. };
  90. private static int types[] = {
  91. SEG_MOVETO,
  92. SEG_LINETO, SEG_CUBICTO,
  93. SEG_LINETO, SEG_CUBICTO,
  94. SEG_LINETO, SEG_CUBICTO,
  95. SEG_LINETO, SEG_CUBICTO,
  96. SEG_CLOSE,
  97. };
  98. /**
  99. * Returns the coordinates and type of the current path segment in
  100. * the iteration.
  101. * The return value is the path segment type:
  102. * SEG_MOVETO, SEG_LINETO, SEG_QUADTO, SEG_CUBICTO, or SEG_CLOSE.
  103. * A float array of length 6 must be passed in and may be used to
  104. * store the coordinates of the point(s).
  105. * Each point is stored as a pair of float x,y coordinates.
  106. * SEG_MOVETO and SEG_LINETO types will return one point,
  107. * SEG_QUADTO will return two points,
  108. * SEG_CUBICTO will return 3 points
  109. * and SEG_CLOSE will not return any points.
  110. * @see #SEG_MOVETO
  111. * @see #SEG_LINETO
  112. * @see #SEG_QUADTO
  113. * @see #SEG_CUBICTO
  114. * @see #SEG_CLOSE
  115. */
  116. public int currentSegment(float[] coords) {
  117. if (isDone()) {
  118. throw new NoSuchElementException("roundrect iterator out of bounds");
  119. }
  120. double ctrls[] = ctrlpts[index];
  121. int nc = 0;
  122. for (int i = 0; i < ctrls.length; i += 4) {
  123. coords[nc++] = (float) (x + ctrls[i + 0] * w + ctrls[i + 1] * aw);
  124. coords[nc++] = (float) (y + ctrls[i + 2] * h + ctrls[i + 3] * ah);
  125. }
  126. if (affine != null) {
  127. affine.transform(coords, 0, coords, 0, nc / 2);
  128. }
  129. return types[index];
  130. }
  131. /**
  132. * Returns the coordinates and type of the current path segment in
  133. * the iteration.
  134. * The return value is the path segment type:
  135. * SEG_MOVETO, SEG_LINETO, SEG_QUADTO, SEG_CUBICTO, or SEG_CLOSE.
  136. * A double array of length 6 must be passed in and may be used to
  137. * store the coordinates of the point(s).
  138. * Each point is stored as a pair of double x,y coordinates.
  139. * SEG_MOVETO and SEG_LINETO types will return one point,
  140. * SEG_QUADTO will return two points,
  141. * SEG_CUBICTO will return 3 points
  142. * and SEG_CLOSE will not return any points.
  143. * @see #SEG_MOVETO
  144. * @see #SEG_LINETO
  145. * @see #SEG_QUADTO
  146. * @see #SEG_CUBICTO
  147. * @see #SEG_CLOSE
  148. */
  149. public int currentSegment(double[] coords) {
  150. if (isDone()) {
  151. throw new NoSuchElementException("roundrect iterator out of bounds");
  152. }
  153. double ctrls[] = ctrlpts[index];
  154. int nc = 0;
  155. for (int i = 0; i < ctrls.length; i += 4) {
  156. coords[nc++] = (x + ctrls[i + 0] * w + ctrls[i + 1] * aw);
  157. coords[nc++] = (y + ctrls[i + 2] * h + ctrls[i + 3] * ah);
  158. }
  159. if (affine != null) {
  160. affine.transform(coords, 0, coords, 0, nc / 2);
  161. }
  162. return types[index];
  163. }
  164. }