1. /*
  2. * @(#)CubicIterator.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 cubic curve
  14. * segment through the PathIterator interface.
  15. *
  16. * @version 10 Feb 1997
  17. * @author Jim Graham
  18. */
  19. class CubicIterator implements PathIterator {
  20. CubicCurve2D cubic;
  21. AffineTransform affine;
  22. int index;
  23. CubicIterator(CubicCurve2D q, AffineTransform at) {
  24. this.cubic = 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("cubic iterator iterator out of bounds");
  72. }
  73. int type;
  74. if (index == 0) {
  75. coords[0] = (float) cubic.getX1();
  76. coords[1] = (float) cubic.getY1();
  77. type = SEG_MOVETO;
  78. } else {
  79. coords[0] = (float) cubic.getCtrlX1();
  80. coords[1] = (float) cubic.getCtrlY1();
  81. coords[2] = (float) cubic.getCtrlX2();
  82. coords[3] = (float) cubic.getCtrlY2();
  83. coords[4] = (float) cubic.getX2();
  84. coords[5] = (float) cubic.getY2();
  85. type = SEG_CUBICTO;
  86. }
  87. if (affine != null) {
  88. affine.transform(coords, 0, coords, 0, index == 0 ? 1 : 3);
  89. }
  90. return type;
  91. }
  92. /**
  93. * Returns the coordinates and type of the current path segment in
  94. * the iteration.
  95. * The return value is the path segment type:
  96. * SEG_MOVETO, SEG_LINETO, SEG_QUADTO, SEG_CUBICTO, or SEG_CLOSE.
  97. * A double array of length 6 must be passed in and may be used to
  98. * store the coordinates of the point(s).
  99. * Each point is stored as a pair of double x,y coordinates.
  100. * SEG_MOVETO and SEG_LINETO types will return one point,
  101. * SEG_QUADTO will return two points,
  102. * SEG_CUBICTO will return 3 points
  103. * and SEG_CLOSE will not return any points.
  104. * @see #SEG_MOVETO
  105. * @see #SEG_LINETO
  106. * @see #SEG_QUADTO
  107. * @see #SEG_CUBICTO
  108. * @see #SEG_CLOSE
  109. */
  110. public int currentSegment(double[] coords) {
  111. if (isDone()) {
  112. throw new NoSuchElementException("cubic iterator iterator out of bounds");
  113. }
  114. int type;
  115. if (index == 0) {
  116. coords[0] = cubic.getX1();
  117. coords[1] = cubic.getY1();
  118. type = SEG_MOVETO;
  119. } else {
  120. coords[0] = cubic.getCtrlX1();
  121. coords[1] = cubic.getCtrlY1();
  122. coords[2] = cubic.getCtrlX2();
  123. coords[3] = cubic.getCtrlY2();
  124. coords[4] = cubic.getX2();
  125. coords[5] = cubic.getY2();
  126. type = SEG_CUBICTO;
  127. }
  128. if (affine != null) {
  129. affine.transform(coords, 0, coords, 0, index == 0 ? 1 : 3);
  130. }
  131. return type;
  132. }
  133. }