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