1. /*
  2. * @(#)QuadIterator.java 1.9 03/12/19
  3. *
  4. * Copyright 2004 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 a quadratic curve
  11. * segment through the PathIterator interface.
  12. *
  13. * @version 10 Feb 1997
  14. * @author Jim Graham
  15. */
  16. class QuadIterator implements PathIterator {
  17. QuadCurve2D quad;
  18. AffineTransform affine;
  19. int index;
  20. QuadIterator(QuadCurve2D q, AffineTransform at) {
  21. this.quad = q;
  22. this.affine = at;
  23. }
  24. /**
  25. * Return the winding rule for determining the insideness of the
  26. * path.
  27. * @see #WIND_EVEN_ODD
  28. * @see #WIND_NON_ZERO
  29. */
  30. public int getWindingRule() {
  31. return WIND_NON_ZERO;
  32. }
  33. /**
  34. * Tests if there are more points to read.
  35. * @return true if there are more points to read
  36. */
  37. public boolean isDone() {
  38. return (index > 1);
  39. }
  40. /**
  41. * Moves the iterator to the next segment of the path forwards
  42. * along the primary direction of traversal as long as there are
  43. * more points in that direction.
  44. */
  45. public void next() {
  46. index++;
  47. }
  48. /**
  49. * Returns the coordinates and type of the current path segment in
  50. * the iteration.
  51. * The return value is the path segment type:
  52. * SEG_MOVETO, SEG_LINETO, SEG_QUADTO, SEG_CUBICTO, or SEG_CLOSE.
  53. * A float array of length 6 must be passed in and may be used to
  54. * store the coordinates of the point(s).
  55. * Each point is stored as a pair of float x,y coordinates.
  56. * SEG_MOVETO and SEG_LINETO types will return one point,
  57. * SEG_QUADTO will return two points,
  58. * SEG_CUBICTO will return 3 points
  59. * and SEG_CLOSE will not return any points.
  60. * @see #SEG_MOVETO
  61. * @see #SEG_LINETO
  62. * @see #SEG_QUADTO
  63. * @see #SEG_CUBICTO
  64. * @see #SEG_CLOSE
  65. */
  66. public int currentSegment(float[] coords) {
  67. if (isDone()) {
  68. throw new NoSuchElementException("quad iterator iterator out of bounds");
  69. }
  70. int type;
  71. if (index == 0) {
  72. coords[0] = (float) quad.getX1();
  73. coords[1] = (float) quad.getY1();
  74. type = SEG_MOVETO;
  75. } else {
  76. coords[0] = (float) quad.getCtrlX();
  77. coords[1] = (float) quad.getCtrlY();
  78. coords[2] = (float) quad.getX2();
  79. coords[3] = (float) quad.getY2();
  80. type = SEG_QUADTO;
  81. }
  82. if (affine != null) {
  83. affine.transform(coords, 0, coords, 0, index == 0 ? 1 : 2);
  84. }
  85. return type;
  86. }
  87. /**
  88. * Returns the coordinates and type of the current path segment in
  89. * the iteration.
  90. * The return value is the path segment type:
  91. * SEG_MOVETO, SEG_LINETO, SEG_QUADTO, SEG_CUBICTO, or SEG_CLOSE.
  92. * A double array of length 6 must be passed in and may be used to
  93. * store the coordinates of the point(s).
  94. * Each point is stored as a pair of double x,y coordinates.
  95. * SEG_MOVETO and SEG_LINETO types will return one point,
  96. * SEG_QUADTO will return two points,
  97. * SEG_CUBICTO will return 3 points
  98. * and SEG_CLOSE will not return any points.
  99. * @see #SEG_MOVETO
  100. * @see #SEG_LINETO
  101. * @see #SEG_QUADTO
  102. * @see #SEG_CUBICTO
  103. * @see #SEG_CLOSE
  104. */
  105. public int currentSegment(double[] coords) {
  106. if (isDone()) {
  107. throw new NoSuchElementException("quad iterator iterator out of bounds");
  108. }
  109. int type;
  110. if (index == 0) {
  111. coords[0] = quad.getX1();
  112. coords[1] = quad.getY1();
  113. type = SEG_MOVETO;
  114. } else {
  115. coords[0] = quad.getCtrlX();
  116. coords[1] = quad.getCtrlY();
  117. coords[2] = quad.getX2();
  118. coords[3] = quad.getY2();
  119. type = SEG_QUADTO;
  120. }
  121. if (affine != null) {
  122. affine.transform(coords, 0, coords, 0, index == 0 ? 1 : 2);
  123. }
  124. return type;
  125. }
  126. }