1. /*
  2. * @(#)Shape.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;
  8. import java.awt.geom.AffineTransform;
  9. import java.awt.geom.PathIterator;
  10. import java.awt.geom.Point2D;
  11. import java.awt.geom.Rectangle2D;
  12. /**
  13. * The <code>Shape</code> interface provides definitions for objects
  14. * that represent some form of geometric shape. The <code>Shape</code>
  15. * is described by a {@link PathIterator} object, which can express the
  16. * outline of the <code>Shape</code> as well as a rule for determining
  17. * how the outline divides the 2D plane into interior and exterior
  18. * points. Each <code>Shape</code> object provides callbacks to get the
  19. * bounding box of the geometry, determine whether points or
  20. * rectangles lie partly or entirely within the interior
  21. * of the <code>Shape</code>, and retrieve a <code>PathIterator</code>
  22. * object that describes the trajectory path of the <code>Shape</code>
  23. * outline.
  24. * <p>
  25. * <b>Definition of insideness:</b>
  26. * A point is considered to lie inside a
  27. * <code>Shape</code> if and only if:
  28. * <ul>
  29. * <li> it lies completely
  30. * inside the<code>Shape</code> boundary <i>or</i>
  31. * <li>
  32. * it lies exactly on the <code>Shape</code> boundary <i>and</i> the
  33. * space immediately adjacent to the
  34. * point in the increasing <code>X</code> direction is
  35. * entirely inside the boundary <i>or</i>
  36. * <li>
  37. * it lies exactly on a horizontal boundary segment <b>and</b> the
  38. * space immediately adjacent to the point in the
  39. * increasing <code>Y</code> direction is inside the boundary.
  40. * </ul>
  41. * <p>The <code>contains</code> and <code>intersects</code> methods
  42. * consider the interior of a <code>Shape</code> to be the area it
  43. * encloses as if it were filled. This means that these methods
  44. * consider
  45. * unclosed shapes to be implicitly closed for the purpose of
  46. * determining if a shape contains or intersects a rectangle or if a
  47. * shape contains a point.
  48. *
  49. * @see java.awt.geom.PathIterator
  50. * @see java.awt.geom.AffineTransform
  51. * @see java.awt.geom.FlatteningPathIterator
  52. * @see java.awt.geom.GeneralPath
  53. *
  54. * @version 1.19 06/24/98
  55. * @author Jim Graham
  56. */
  57. public interface Shape {
  58. /**
  59. * Returns an integer {@link Rectangle} that completely encloses the
  60. * <code>Shape</code>. Note that there is no guarantee that the
  61. * returned <code>Rectangle</code> is the smallest bounding box that
  62. * encloses the <code>Shape</code>, only that the <code>Shape</code>
  63. * lies entirely within the indicated <code>Rectangle</code>. The
  64. * returned <code>Rectangle</code> might also fail to completely
  65. * enclose the <code>Shape</code> if the <code>Shape</code> overflows
  66. * the limited range of the integer data type. The
  67. * <code>getBounds2D</code> method generally returns a
  68. * tighter bounding box due to its greater flexibility in
  69. * representation.
  70. * @return an integer <code>Rectangle</code> that completely encloses
  71. * the <code>Shape</code>.
  72. * @see #getBounds2D
  73. */
  74. public Rectangle getBounds();
  75. /**
  76. * Returns a high precision and more accurate bounding box of
  77. * the <code>Shape</code> than the <code>getBounds</code> method.
  78. * Note that there is no guarantee that the returned
  79. * {@link Rectangle2D} is the smallest bounding box that encloses
  80. * the <code>Shape</code>, only that the <code>Shape</code> lies
  81. * entirely within the indicated <code>Rectangle2D</code>. The
  82. * bounding box returned by this method is usually tighter than that
  83. * returned by the <code>getBounds</code> method and never fails due
  84. * to overflow problems since the return value can be an instance of
  85. * the <code>Rectangle2D</code> that uses double precision values to
  86. * store the dimensions.
  87. * @return an instance of <code>Rectangle2D</code> that is a
  88. * high-precision bounding box of the <code>Shape</code>.
  89. * @see #getBounds
  90. */
  91. public Rectangle2D getBounds2D();
  92. /**
  93. * Tests if the specified coordinates are inside the boundary of the
  94. * <code>Shape</code>.
  95. * @param x the specified x coordinate
  96. * @param y the specified y coordinate
  97. * @return <code>true</code> if the specified coordinates are inside
  98. * the <code>Shape</code> boundary; <code>false</code>
  99. * otherwise.
  100. */
  101. public boolean contains(double x, double y);
  102. /**
  103. * Tests if a specified {@link Point2D} is inside the boundary
  104. * of the <code>Shape</code>.
  105. * @param p a specified <code>Point2D</code>
  106. * @return <code>true</code> if the specified <code>Point2D</code> is
  107. * inside the boundary of the <code>Shape</code>
  108. * <code>false</code> otherwise.
  109. */
  110. public boolean contains(Point2D p);
  111. /**
  112. * Tests if the interior of the <code>Shape</code> intersects the
  113. * interior of a specified rectangular area.
  114. * The rectangular area is considered to intersect the <code>Shape</code>
  115. * if any point is contained in both the interior of the
  116. * <code>Shape</code> and the specified rectangular area.
  117. * <p>
  118. * This method might conservatively return <code>true</code> when:
  119. * <ul>
  120. * <li>
  121. * there is a high probability that the rectangular area and the
  122. * <code>Shape</code> intersect, but
  123. * <li>
  124. * the calculations to accurately determine this intersection
  125. * are prohibitively expensive.
  126. * </ul>
  127. * This means that this method might return <code>true</code> even
  128. * though the rectangular area does not intersect the <code>Shape</code>.
  129. * The {@link java.awt.geom.Area Area} class can be used to perform
  130. * more accurate computations of geometric intersection for any
  131. * <code>Shape</code> object if a more precise answer is required.
  132. * @param x the x coordinate of the specified rectangular area
  133. * @param y the y coordinate of the specified rectangular area
  134. * @param w the width of the specified rectangular area
  135. * @param h the height of the specified rectangular area
  136. * @return <code>true</code> if the interior of the <code>Shape</code> and
  137. * the interior of the rectangular area intersect, or are
  138. * both highly likely to intersect and intersection calculations
  139. * would be too expensive to perform; <code>false</code> otherwise.
  140. * @see java.awt.geom.Area
  141. */
  142. public boolean intersects(double x, double y, double w, double h);
  143. /**
  144. * Tests if the interior of the <code>Shape</code> intersects the
  145. * interior of a specified <code>Rectangle2D</code>.
  146. * This method might conservatively return <code>true</code> when:
  147. * <ul>
  148. * <li>
  149. * there is a high probability that the <code>Rectangle2D</code> and the
  150. * <code>Shape</code> intersect, but
  151. * <li>
  152. * the calculations to accurately determine this intersection
  153. * are prohibitively expensive.
  154. * </ul>
  155. * This means that this method might return <code>true</code> even
  156. * though the <code>Rectangle2D</code> does not intersect the
  157. * <code>Shape</code>.
  158. * @param r the specified <code>Rectangle2D</code>
  159. * @return <code>true</code> if the interior of the <code>Shape</code> and
  160. * the interior of the specified <code>Rectangle2D</code>
  161. * intersect, or are both highly likely to intersect and intersection
  162. * calculations would be too expensive to perform; <code>false</code>
  163. * otherwise.
  164. * @see #intersects(double, double, double, double)
  165. */
  166. public boolean intersects(Rectangle2D r);
  167. /**
  168. * Tests if the interior of the <code>Shape</code> entirely contains
  169. * the specified rectangular area. All coordinates that lie inside
  170. * the rectangular area must lie within the <code>Shape</code> for the
  171. * entire rectanglar area to be considered contained within the
  172. * <code>Shape</code>.
  173. * <p>
  174. * This method might conservatively return <code>false</code> when:
  175. * <ul>
  176. * <li>
  177. * the <code>intersect</code> method returns <code>true</code> and
  178. * <li>
  179. * the calculations to determine whether or not the
  180. * <code>Shape</code> entirely contains the rectangular area are
  181. * prohibitively expensive.
  182. * </ul>
  183. * This means that this method might return <code>false</code> even
  184. * though the <code>Shape</code> contains the rectangular area.
  185. * The <code>Area</code> class can be used to perform more accurate
  186. * computations of geometric intersection for any <code>Shape</code>
  187. * object if a more precise answer is required.
  188. * @param x the x coordinate of the specified rectangular area
  189. * @param y the y coordinate of the specified rectangular area
  190. * @param w the width of the specified rectangular area
  191. * @param h the height of the specified rectangular area
  192. * @return <code>true</code> if the interior of the <code>Shape</code>
  193. * entirely contains the specified rectangular area;
  194. * <code>false</code> otherwise or, if the <code>Shape</code>
  195. * contains the rectangular area and the
  196. * <code>intersects</code> method returns <code>true</code>
  197. * and the containment calculations would be too expensive to
  198. * perform.
  199. * @see java.awt.geom.Area
  200. * @see #intersects
  201. */
  202. public boolean contains(double x, double y, double w, double h);
  203. /**
  204. * Tests if the interior of the <code>Shape</code> entirely contains the
  205. * specified <code>Rectangle2D</code>.
  206. * This method might conservatively return <code>false</code> when:
  207. * <ul>
  208. * <li>
  209. * the <code>intersect</code> method returns <code>true</code> and
  210. * <li>
  211. * the calculations to determine whether or not the
  212. * <code>Shape</code> entirely contains the <code>Rectangle2D</code>
  213. * are prohibitively expensive.
  214. * </ul>
  215. * This means that this method might return <code>false</code> even
  216. * though the <code>Shape</code> contains the
  217. * <code>Rectangle2D</code>.
  218. * The <code>Area</code> class can be used to perform more accurate
  219. * computations of geometric intersection for any <code>Shape</code>
  220. * object if a more precise answer is required.
  221. * @param r The specified <code>Rectangle2D</code>
  222. * @return <code>true</code> if the interior of the <code>Shape</code>
  223. * entirely contains the <code>Rectangle2D</code>
  224. * <code>false</code> otherwise or, if the <code>Shape</code>
  225. * contains the <code>Rectangle2D</code> and the
  226. * <code>intersects</code> method returns <code>true</code>
  227. * and the containment calculations would be too expensive to
  228. * perform.
  229. * @see #contains(double, double, double, double)
  230. */
  231. public boolean contains(Rectangle2D r);
  232. /**
  233. * Returns an iterator object that iterates along the
  234. * <code>Shape</code> boundary and provides access to the geometry of the
  235. * <code>Shape</code> outline. If an optional {@link AffineTransform}
  236. * is specified, the coordinates returned in the iteration are
  237. * transformed accordingly.
  238. * <p>
  239. * Each call to this method returns a fresh <code>PathIterator</code>
  240. * object that traverses the geometry of the <code>Shape</code> object
  241. * independently from any other <code>PathIterator</code> objects in use
  242. * at the same time.
  243. * <p>
  244. * It is recommended, but not guaranteed, that objects
  245. * implementing the <code>Shape</code> interface isolate iterations
  246. * that are in process from any changes that might occur to the original
  247. * object's geometry during such iterations.
  248. * <p>
  249. * Before using a particular implementation of the <code>Shape</code>
  250. * interface in more than one thread simultaneously, refer to its
  251. * documentation to verify that it guarantees that iterations are isolated
  252. * from modifications.
  253. * @param at an optional <code>AffineTransform</code> to be applied to the
  254. * coordinates as they are returned in the iteration, or
  255. * <code>null</code> if untransformed coordinates are desired
  256. * @return a new <code>PathIterator</code> object, which independently
  257. * traverses the geometry of the <code>Shape</code>.
  258. */
  259. public PathIterator getPathIterator(AffineTransform at);
  260. /**
  261. * Returns an iterator object that iterates along the <code>Shape</code>
  262. * boundary and provides access to a flattened view of the
  263. * <code>Shape</code> outline geometry.
  264. * <p>
  265. * Only SEG_MOVETO, SEG_LINETO, and SEG_CLOSE point types are
  266. * returned by the iterator.
  267. * <p>
  268. * If an optional <code>AffineTransform</code> is specified,
  269. * the coordinates returned in the iteration are transformed
  270. * accordingly.
  271. * <p>
  272. * The amount of subdivision of the curved segments is controlled
  273. * by the <code>flatness</code> parameter, which specifies the
  274. * maximum distance that any point on the unflattened transformed
  275. * curve can deviate from the returned flattened path segments.
  276. * Note that a limit on the accuracy of the flattened path might be
  277. * silently imposed, causing very small flattening parameters to be
  278. * treated as larger values. This limit, if there is one, is
  279. * defined by the particular implementation that is used.
  280. * <p>
  281. * Each call to this method returns a fresh <code>PathIterator</code>
  282. * object that traverses the <code>Shape</code> object geometry
  283. * independently from any other <code>PathIterator</code> objects in use at
  284. * the same time.
  285. * <p>
  286. * It is recommended, but not guaranteed, that objects
  287. * implementing the <code>Shape</code> interface isolate iterations
  288. * that are in process from any changes that might occur to the original
  289. * object's geometry during such iterations.
  290. * <p>
  291. * Before using a particular implementation of this interface in more
  292. * than one thread simultaneously, refer to its documentation to
  293. * verify that it guarantees that iterations are isolated from
  294. * modifications.
  295. * @param at an optional <code>AffineTransform</code> to be applied to the
  296. * coordinates as they are returned in the iteration, or
  297. * <code>null</code> if untransformed coordinates are desired
  298. * @param flatness the maximum distance that the line segments used to
  299. * approximate the curved segments are allowed to deviate
  300. * from any point on the original curve
  301. * @return a new <code>PathIterator</code> that independently traverses
  302. * the <code>Shape</code> geometry.
  303. */
  304. public PathIterator getPathIterator(AffineTransform at, double flatness);
  305. }