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