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