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