1. /*
  2. * @(#)GeneralPathIterator.java 1.19 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. /**
  12. * This class represents the iterator for General Paths.
  13. * It can be used to retrieve all of the elements in a GeneralPath.
  14. * The {@link GeneralPath#getPathIterator}
  15. * method is used to create a
  16. * GeneralPathIterator for a particular GeneralPath.
  17. * The iterator can be used to iterator the path only once.
  18. * Subsequent iterations require a new iterator.
  19. *
  20. * @see GeneralPath
  21. *
  22. * @version 10 Feb 1997
  23. * @author Jim Graham
  24. */
  25. class GeneralPathIterator implements PathIterator {
  26. int typeIdx = 0;
  27. int pointIdx = 0;
  28. GeneralPath path;
  29. AffineTransform affine;
  30. private static final int curvesize[] = {2, 2, 4, 6, 0};
  31. /**
  32. * Constructs an iterator given a GeneralPath.
  33. * @see GeneralPath#getPathIterator
  34. */
  35. GeneralPathIterator(GeneralPath path) {
  36. this(path, null);
  37. }
  38. /**
  39. * Constructs an iterator given a GeneralPath and an optional
  40. * AffineTransform.
  41. * @see GeneralPath#getPathIterator
  42. */
  43. GeneralPathIterator(GeneralPath path, AffineTransform at) {
  44. this.path = path;
  45. this.affine = at;
  46. }
  47. /**
  48. * Return the winding rule for determining the interior of the
  49. * path.
  50. * @see PathIterator#WIND_EVEN_ODD
  51. * @see PathIterator#WIND_NON_ZERO
  52. */
  53. public int getWindingRule() {
  54. return path.getWindingRule();
  55. }
  56. /**
  57. * Tests if there are more points to read.
  58. * @return true if there are more points to read
  59. */
  60. public boolean isDone() {
  61. return (typeIdx >= path.numTypes);
  62. }
  63. /**
  64. * Moves the iterator to the next segment of the path forwards
  65. * along the primary direction of traversal as long as there are
  66. * more points in that direction.
  67. */
  68. public void next() {
  69. int type = path.pointTypes[typeIdx++];
  70. pointIdx += curvesize[type];
  71. }
  72. /**
  73. * Returns the coordinates and type of the current path segment in
  74. * the iteration.
  75. * The return value is the path segment type:
  76. * SEG_MOVETO, SEG_LINETO, SEG_QUADTO, SEG_CUBICTO, or SEG_CLOSE.
  77. * A float array of length 6 must be passed in and may be used to
  78. * store the coordinates of the point(s).
  79. * Each point is stored as a pair of float x,y coordinates.
  80. * SEG_MOVETO and SEG_LINETO types will return one point,
  81. * SEG_QUADTO will return two points,
  82. * SEG_CUBICTO will return 3 points
  83. * and SEG_CLOSE will not return any points.
  84. * @see PathIterator#SEG_MOVETO
  85. * @see PathIterator#SEG_LINETO
  86. * @see PathIterator#SEG_QUADTO
  87. * @see PathIterator#SEG_CUBICTO
  88. * @see PathIterator#SEG_CLOSE
  89. */
  90. public int currentSegment(float[] coords) {
  91. int type = path.pointTypes[typeIdx];
  92. int numCoords = curvesize[type];
  93. if (numCoords > 0 && affine != null) {
  94. affine.transform(path.pointCoords, pointIdx,
  95. coords, 0,
  96. numCoords / 2);
  97. } else {
  98. System.arraycopy(path.pointCoords, pointIdx, coords, 0, numCoords);
  99. }
  100. return type;
  101. }
  102. /**
  103. * Returns the coordinates and type of the current path segment in
  104. * the iteration.
  105. * The return value is the path segment type:
  106. * SEG_MOVETO, SEG_LINETO, SEG_QUADTO, SEG_CUBICTO, or SEG_CLOSE.
  107. * A double array of length 6 must be passed in and may be used to
  108. * store the coordinates of the point(s).
  109. * Each point is stored as a pair of double x,y coordinates.
  110. * SEG_MOVETO and SEG_LINETO types will return one point,
  111. * SEG_QUADTO will return two points,
  112. * SEG_CUBICTO will return 3 points
  113. * and SEG_CLOSE will not return any points.
  114. * @see PathIterator#SEG_MOVETO
  115. * @see PathIterator#SEG_LINETO
  116. * @see PathIterator#SEG_QUADTO
  117. * @see PathIterator#SEG_CUBICTO
  118. * @see PathIterator#SEG_CLOSE
  119. */
  120. public int currentSegment(double[] coords) {
  121. int type = path.pointTypes[typeIdx];
  122. int numCoords = curvesize[type];
  123. if (numCoords > 0 && affine != null) {
  124. affine.transform(path.pointCoords, pointIdx,
  125. coords, 0,
  126. numCoords / 2);
  127. } else {
  128. for (int i=0; i < numCoords; i++) {
  129. coords[i] = path.pointCoords[pointIdx + i];
  130. }
  131. }
  132. return type;
  133. }
  134. }