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