1. /*
  2. * @(#)RectangularShape.java 1.11 01/11/29
  3. *
  4. * Copyright 2002 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.awt.Shape;
  9. import java.awt.Rectangle;
  10. /**
  11. * <code>RectangularShape</code> is the base class for a number of
  12. * {@link Shape} objects whose geometry is defined by a rectangular frame.
  13. * This class does not directly specify any specific geometry by
  14. * itself, but merely provides manipulation methods inherited by
  15. * a whole category of <code>Shape</code> objects.
  16. * The manipulation methods provided by this class can be used to
  17. * query and modify the rectangular frame, which provides a reference
  18. * for the subclasses to define their geometry.
  19. *
  20. * @version 10 Feb 1997
  21. * @author Jim Graham
  22. */
  23. public abstract class RectangularShape implements Shape, Cloneable {
  24. /**
  25. * This is an abstract class that cannot be instantiated directly.
  26. *
  27. * @see Arc2D
  28. * @see Ellipse2D
  29. * @see Rectangle2D
  30. * @see RoundRectangle2D
  31. */
  32. protected RectangularShape() {
  33. }
  34. /**
  35. * Returns the X coordinate of the upper left corner of
  36. * the framing rectangle in <code>double</code> precision.
  37. * @return the x coordinate of the upper left corner of
  38. * the framing rectangle.
  39. */
  40. public abstract double getX();
  41. /**
  42. * Returns the Y coordinate of the upper left corner of
  43. * the framing rectangle in <code>double</code> precision.
  44. * @return the y coordinate of the upper left corner of
  45. * the framing rectangle.
  46. */
  47. public abstract double getY();
  48. /**
  49. * Returns the width of the framing rectangle in
  50. * <code>double</code> precision.
  51. * @return the width of the framing rectangle.
  52. */
  53. public abstract double getWidth();
  54. /**
  55. * Returns the height of the framing rectangle
  56. * in <code>double</code> precision.
  57. * @return the height of the framing rectangle.
  58. */
  59. public abstract double getHeight();
  60. /**
  61. * Returns the smallest X coordinate of the framing
  62. * rectangle of the <code>Shape</code> in <code>double</code>
  63. * precision.
  64. * @return the smallest x coordinate of the framing
  65. * rectangle of the <code>Shape</code>.
  66. */
  67. public double getMinX() {
  68. return getX();
  69. }
  70. /**
  71. * Returns the smallest Y coordinate of the framing
  72. * rectangle of the <code>Shape</code> in <code>double</code>
  73. * precision.
  74. * @return the smallest y coordinate of the framing
  75. * rectangle of the <code>Shape</code>.
  76. */
  77. public double getMinY() {
  78. return getY();
  79. }
  80. /**
  81. * Returns the largest X coordinate of the framing
  82. * rectangle of the <code>Shape</code> in <code>double</code>
  83. * precision.
  84. * @return the largest x coordinate of the framing
  85. * rectangle of the <code>Shape</code>.
  86. */
  87. public double getMaxX() {
  88. return getX() + getWidth();
  89. }
  90. /**
  91. * Returns the largest Y coordinate of the framing
  92. * rectangle of the <code>Shape</code> in <code>double</code>
  93. * precision.
  94. * @return the largest y coordinate of the framing
  95. * rectangle of the <code>Shape</code>.
  96. */
  97. public double getMaxY() {
  98. return getY() + getHeight();
  99. }
  100. /**
  101. * Returns the X coordinate of the center of the framing
  102. * rectangle of the <code>Shape</code> in <code>double</code>
  103. * precision.
  104. * @return the x coordinate of the framing rectangle
  105. * of the <code>Shape</code> object's center.
  106. */
  107. public double getCenterX() {
  108. return getX() + getWidth() / 2.0;
  109. }
  110. /**
  111. * Returns the Y coordinate of the center of the framing
  112. * rectangle of the <code>Shape</code> in <code>double</code>
  113. * precision.
  114. * @return the y coordinate of the framing rectangle
  115. * of the <code>Shape</code> object's center.
  116. */
  117. public double getCenterY() {
  118. return getY() + getHeight() / 2.0;
  119. }
  120. /**
  121. * Returns the framing {@link Rectangle2D}
  122. * that defines the overall shape of this object.
  123. * @return a <code>Rectangle2D</code>, specified in
  124. * <code>double</code> coordinates.
  125. */
  126. public Rectangle2D getFrame() {
  127. return new Rectangle2D.Double(getX(), getY(), getWidth(), getHeight());
  128. }
  129. /**
  130. * Determines whether the <code>RectangularShape</code> is empty.
  131. * When the <code>RectangularShape</code> is empty, it encloses no
  132. * area.
  133. * @return <code>true</code> if the <code>RectangularShape</code> is empty;
  134. * <code>false</code> otherwise.
  135. */
  136. public abstract boolean isEmpty();
  137. /**
  138. * Sets the location and size of the framing rectangle of this
  139. * <code>Shape</code> to the specified rectangular values.
  140. * The framing rectangle is used by the subclasses of
  141. * <code>RectangularShape</code> to define their geometry.
  142. * @param x, y the coordinates of the upper-left corner of the
  143. * specified rectangular shape
  144. * @param w the width of the specified rectangular shape
  145. * @param h the height of the specified rectangular shape
  146. */
  147. public abstract void setFrame(double x, double y, double w, double h);
  148. /**
  149. * Sets the location and size of the framing rectangle of this
  150. * <code>Shape</code> to the specified {@link Point2D} and
  151. * {@link Dimension2D}, respectively. The framing rectangle is used
  152. * by the subclasses of <code>RectangularShape</code> to define
  153. * their geometry.
  154. * @param loc the specified <code>Point2D</code>
  155. * @param size the specified <code>Dimension2D</code>
  156. */
  157. public void setFrame(Point2D loc, Dimension2D size) {
  158. setFrame(loc.getX(), loc.getY(), size.getWidth(), size.getHeight());
  159. }
  160. /**
  161. * Sets the framing rectangle of this <code>Shape</code> to
  162. * be the specified <code>Rectangle2D</code>. The framing rectangle is
  163. * used by the subclasses of <code>RectangularShape</code> to define
  164. * their geometry.
  165. * @param r the specified <code>Rectangle2D</code>
  166. */
  167. public void setFrame(Rectangle2D r) {
  168. setFrame(r.getX(), r.getY(), r.getWidth(), r.getHeight());
  169. }
  170. /**
  171. * Sets the diagonal of the framing rectangle of this <code>Shape</code>
  172. * based on the two specified coordinates. The framing rectangle is
  173. * used by the subclasses of <code>RectangularShape</code> to define
  174. * their geometry.
  175. * @param x1, y1 the first specified coordinates
  176. * @param x2, y2 the second specified coordinates
  177. */
  178. public void setFrameFromDiagonal(double x1, double y1,
  179. double x2, double y2) {
  180. if (x2 < x1) {
  181. double t = x1;
  182. x1 = x2;
  183. x2 = t;
  184. }
  185. if (y2 < y1) {
  186. double t = y1;
  187. y1 = y2;
  188. y2 = t;
  189. }
  190. setFrame(x1, y1, x2 - x1, y2 - y1);
  191. }
  192. /**
  193. * Sets the diagonal of the framing rectangle of this <code>Shape</code>
  194. * based on two specified <code>Point2D</code> objects. The framing
  195. * rectangle is used by the subclasses of <code>RectangularShape</code>
  196. * to define their geometry.
  197. * @param p1, p2 the two specified <code>Point2D</code> objects
  198. */
  199. public void setFrameFromDiagonal(Point2D p1, Point2D p2) {
  200. setFrameFromDiagonal(p1.getX(), p1.getY(), p2.getX(), p2.getY());
  201. }
  202. /**
  203. * Sets the framing rectangle of this <code>Shape</code>
  204. * based on the specified center point coordinates and corner point
  205. * coordinates. The framing rectangle is used by the subclasses of
  206. * <code>RectangularShape</code> to define their geometry.
  207. * @param centerX, centerY the center point coordinates
  208. * @param cornerX, cornerY the corner point coordinates
  209. */
  210. public void setFrameFromCenter(double centerX, double centerY,
  211. double cornerX, double cornerY) {
  212. double halfW = Math.abs(cornerX - centerX);
  213. double halfH = Math.abs(cornerY - centerY);
  214. setFrame(centerX - halfW, centerY - halfH, halfW * 2.0, halfH * 2.0);
  215. }
  216. /**
  217. * Sets the framing rectangle of this <code>Shape</code> based on a
  218. * specified center <code>Point2D</code> and corner
  219. * <code>Point2D</code>. The framing rectangle is used by the subclasses
  220. * of <code>RectangularShape</code> to define their geometry.
  221. * @param center the specified center <code>Point2D</code>
  222. * @param corner the specified corner <code>Point2D</code>
  223. */
  224. public void setFrameFromCenter(Point2D center, Point2D corner) {
  225. setFrameFromCenter(center.getX(), center.getY(),
  226. corner.getX(), corner.getY());
  227. }
  228. /**
  229. * Tests if a specified <code>Point2D</code> is inside the boundary
  230. * of the <code>Shape</code>.
  231. * @param p the specified <code>Point2D</code>
  232. * @return <code>true</code> if the <code>Point2D</code> is inside the
  233. * <code>Shape</code> object's boundary;
  234. * <code>false</code> otherwise.
  235. */
  236. public boolean contains(Point2D p) {
  237. return contains(p.getX(), p.getY());
  238. }
  239. /**
  240. * Tests if the interior of the<code>Shape</code> intersects the
  241. * interior of a specified <code>Rectangle2D</code>.
  242. * @param r the specified <code>Rectangle2D</code>
  243. * @return <code>true</code> if the <code>Shape</code> and the
  244. * specified <code>Rectangle2D</code> intersect each other;
  245. * <code>false</code> otherwise.
  246. */
  247. public boolean intersects(Rectangle2D r) {
  248. return intersects(r.getX(), r.getY(), r.getWidth(), r.getHeight());
  249. }
  250. /**
  251. * Tests if the interior of the <code>Shape</code> entirely contains the
  252. * specified <code>Rectangle2D</code>.
  253. * @param r the specified <code>Rectangle2D</code>
  254. * @return <code>true</code> if the <code>Shape</code> entirely contains
  255. * the specified <code>Rectangle2D</code>
  256. * <code>false</code> otherwise.
  257. */
  258. public boolean contains(Rectangle2D r) {
  259. return contains(r.getX(), r.getY(), r.getWidth(), r.getHeight());
  260. }
  261. /**
  262. * Returns the bounding box of the <code>Shape</code>.
  263. * @return a {@link Rectangle} object that bounds the
  264. * <code>Shape</code>.
  265. */
  266. public Rectangle getBounds() {
  267. double width = getWidth();
  268. double height = getHeight();
  269. if (width < 0 || height < 0) {
  270. return new Rectangle();
  271. }
  272. double x = getX();
  273. double y = getY();
  274. double x1 = Math.floor(x);
  275. double y1 = Math.floor(y);
  276. double x2 = Math.ceil(x + width);
  277. double y2 = Math.ceil(y + height);
  278. return new Rectangle((int) x1, (int) y1,
  279. (int) (x2 - x1), (int) (y2 - y1));
  280. }
  281. /**
  282. * Returns an iterator object that iterates along the
  283. * <code>Shape</code> object's boundary and provides access to a
  284. * flattened view of the outline of the <code>Shape</code>
  285. * object's geometry.
  286. * <p>
  287. * Only SEG_MOVETO, SEG_LINETO, and SEG_CLOSE point types will
  288. * be returned by the iterator.
  289. * <p>
  290. * The amount of subdivision of the curved segments is controlled
  291. * by the <code>flatness</code> parameter, which specifies the
  292. * maximum distance that any point on the unflattened transformed
  293. * curve can deviate from the returned flattened path segments.
  294. * An optional {@link AffineTransform} can
  295. * be specified so that the coordinates returned in the iteration are
  296. * transformed accordingly.
  297. * @param at an optional <code>AffineTransform</code> to be applied to the
  298. * coordinates as they are returned in the iteration,
  299. * or <code>null</code> if untransformed coordinates are desired.
  300. * @param flatness the maximum distance that the line segments used to
  301. * approximate the curved segments are allowed to deviate
  302. * from any point on the original curve
  303. * @return a <code>PathIterator</code> object that provides access to
  304. * the <code>Shape</code> object's flattened geometry.
  305. */
  306. public PathIterator getPathIterator(AffineTransform at, double flatness) {
  307. return new FlatteningPathIterator(getPathIterator(at), flatness);
  308. }
  309. /**
  310. * Creates a new object of the same class and with the same
  311. * contents as this object.
  312. * @return a clone of this instance.
  313. * @exception OutOfMemoryError if there is not enough memory.
  314. * @see java.lang.Cloneable
  315. * @since JDK1.2
  316. */
  317. public Object clone() {
  318. try {
  319. return super.clone();
  320. } catch (CloneNotSupportedException e) {
  321. // this shouldn't happen, since we are Cloneable
  322. throw new InternalError();
  323. }
  324. }
  325. }