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