1. /*
  2. * @(#)RectIterator.java 1.11 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 rectangle
  11. * through the PathIterator interface.
  12. *
  13. * @version 10 Feb 1997
  14. * @author Jim Graham
  15. */
  16. class RectIterator implements PathIterator {
  17. double x, y, w, h;
  18. AffineTransform affine;
  19. int index;
  20. RectIterator(Rectangle2D r, AffineTransform at) {
  21. this.x = r.getX();
  22. this.y = r.getY();
  23. this.w = r.getWidth();
  24. this.h = r.getHeight();
  25. this.affine = at;
  26. if (w < 0 || h < 0) {
  27. index = 6;
  28. }
  29. }
  30. /**
  31. * Return the winding rule for determining the insideness of the
  32. * path.
  33. * @see #WIND_EVEN_ODD
  34. * @see #WIND_NON_ZERO
  35. */
  36. public int getWindingRule() {
  37. return WIND_NON_ZERO;
  38. }
  39. /**
  40. * Tests if there are more points to read.
  41. * @return true if there are more points to read
  42. */
  43. public boolean isDone() {
  44. return index > 5;
  45. }
  46. /**
  47. * Moves the iterator to the next segment of the path forwards
  48. * along the primary direction of traversal as long as there are
  49. * more points in that direction.
  50. */
  51. public void next() {
  52. index++;
  53. }
  54. /**
  55. * Returns the coordinates and type of the current path segment in
  56. * the iteration.
  57. * The return value is the path segment type:
  58. * SEG_MOVETO, SEG_LINETO, SEG_QUADTO, SEG_CUBICTO, or SEG_CLOSE.
  59. * A float array of length 6 must be passed in and may be used to
  60. * store the coordinates of the point(s).
  61. * Each point is stored as a pair of float x,y coordinates.
  62. * SEG_MOVETO and SEG_LINETO types will return one point,
  63. * SEG_QUADTO will return two points,
  64. * SEG_CUBICTO will return 3 points
  65. * and SEG_CLOSE will not return any points.
  66. * @see #SEG_MOVETO
  67. * @see #SEG_LINETO
  68. * @see #SEG_QUADTO
  69. * @see #SEG_CUBICTO
  70. * @see #SEG_CLOSE
  71. */
  72. public int currentSegment(float[] coords) {
  73. if (isDone()) {
  74. throw new NoSuchElementException("rect iterator out of bounds");
  75. }
  76. if (index == 5) {
  77. return SEG_CLOSE;
  78. }
  79. coords[0] = (float) x;
  80. coords[1] = (float) y;
  81. if (index == 1 || index == 2) {
  82. coords[0] += (float) w;
  83. }
  84. if (index == 2 || index == 3) {
  85. coords[1] += (float) h;
  86. }
  87. if (affine != null) {
  88. affine.transform(coords, 0, coords, 0, 1);
  89. }
  90. return (index == 0 ? SEG_MOVETO : SEG_LINETO);
  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("rect iterator out of bounds");
  113. }
  114. if (index == 5) {
  115. return SEG_CLOSE;
  116. }
  117. coords[0] = x;
  118. coords[1] = y;
  119. if (index == 1 || index == 2) {
  120. coords[0] += w;
  121. }
  122. if (index == 2 || index == 3) {
  123. coords[1] += h;
  124. }
  125. if (affine != null) {
  126. affine.transform(coords, 0, coords, 0, 1);
  127. }
  128. return (index == 0 ? SEG_MOVETO : SEG_LINETO);
  129. }
  130. }