1. /*
  2. * @(#)RectangularShape.java 1.18 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.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 1.18, 12/19/03
  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. * @see #setFrame(double, double, double, double)
  126. * @see #setFrame(Point2D, Dimension2D)
  127. * @see #setFrame(Rectangle2D)
  128. */
  129. public Rectangle2D getFrame() {
  130. return new Rectangle2D.Double(getX(), getY(), getWidth(), getHeight());
  131. }
  132. /**
  133. * Determines whether the <code>RectangularShape</code> is empty.
  134. * When the <code>RectangularShape</code> is empty, it encloses no
  135. * area.
  136. * @return <code>true</code> if the <code>RectangularShape</code> is empty;
  137. * <code>false</code> otherwise.
  138. */
  139. public abstract boolean isEmpty();
  140. /**
  141. * Sets the location and size of the framing rectangle of this
  142. * <code>Shape</code> to the specified rectangular values.
  143. * The framing rectangle is used by the subclasses of
  144. * <code>RectangularShape</code> to define their geometry.
  145. * @param x, y the coordinates of the upper-left corner of the
  146. * specified rectangular shape
  147. * @param w the width of the specified rectangular shape
  148. * @param h the height of the specified rectangular shape
  149. * @see #getFrame
  150. */
  151. public abstract void setFrame(double x, double y, double w, double h);
  152. /**
  153. * Sets the location and size of the framing rectangle of this
  154. * <code>Shape</code> to the specified {@link Point2D} and
  155. * {@link Dimension2D}, respectively. The framing rectangle is used
  156. * by the subclasses of <code>RectangularShape</code> to define
  157. * their geometry.
  158. * @param loc the specified <code>Point2D</code>
  159. * @param size the specified <code>Dimension2D</code>
  160. * @see #getFrame
  161. */
  162. public void setFrame(Point2D loc, Dimension2D size) {
  163. setFrame(loc.getX(), loc.getY(), size.getWidth(), size.getHeight());
  164. }
  165. /**
  166. * Sets the framing rectangle of this <code>Shape</code> to
  167. * be the specified <code>Rectangle2D</code>. The framing rectangle is
  168. * used by the subclasses of <code>RectangularShape</code> to define
  169. * their geometry.
  170. * @param r the specified <code>Rectangle2D</code>
  171. * @see #getFrame
  172. */
  173. public void setFrame(Rectangle2D r) {
  174. setFrame(r.getX(), r.getY(), r.getWidth(), r.getHeight());
  175. }
  176. /**
  177. * Sets the diagonal of the framing rectangle of this <code>Shape</code>
  178. * based on the two specified coordinates. The framing rectangle is
  179. * used by the subclasses of <code>RectangularShape</code> to define
  180. * their geometry.
  181. * @param x1, y1 the first specified coordinates
  182. * @param x2, y2 the second specified coordinates
  183. */
  184. public void setFrameFromDiagonal(double x1, double y1,
  185. double x2, double y2) {
  186. if (x2 < x1) {
  187. double t = x1;
  188. x1 = x2;
  189. x2 = t;
  190. }
  191. if (y2 < y1) {
  192. double t = y1;
  193. y1 = y2;
  194. y2 = t;
  195. }
  196. setFrame(x1, y1, x2 - x1, y2 - y1);
  197. }
  198. /**
  199. * Sets the diagonal of the framing rectangle of this <code>Shape</code>
  200. * based on two specified <code>Point2D</code> objects. The framing
  201. * rectangle is used by the subclasses of <code>RectangularShape</code>
  202. * to define their geometry.
  203. * @param p1, p2 the two specified <code>Point2D</code> objects
  204. */
  205. public void setFrameFromDiagonal(Point2D p1, Point2D p2) {
  206. setFrameFromDiagonal(p1.getX(), p1.getY(), p2.getX(), p2.getY());
  207. }
  208. /**
  209. * Sets the framing rectangle of this <code>Shape</code>
  210. * based on the specified center point coordinates and corner point
  211. * coordinates. The framing rectangle is used by the subclasses of
  212. * <code>RectangularShape</code> to define their geometry.
  213. * @param centerX, centerY the center point coordinates
  214. * @param cornerX, cornerY the corner point coordinates
  215. */
  216. public void setFrameFromCenter(double centerX, double centerY,
  217. double cornerX, double cornerY) {
  218. double halfW = Math.abs(cornerX - centerX);
  219. double halfH = Math.abs(cornerY - centerY);
  220. setFrame(centerX - halfW, centerY - halfH, halfW * 2.0, halfH * 2.0);
  221. }
  222. /**
  223. * Sets the framing rectangle of this <code>Shape</code> based on a
  224. * specified center <code>Point2D</code> and corner
  225. * <code>Point2D</code>. The framing rectangle is used by the subclasses
  226. * of <code>RectangularShape</code> to define their geometry.
  227. * @param center the specified center <code>Point2D</code>
  228. * @param corner the specified corner <code>Point2D</code>
  229. */
  230. public void setFrameFromCenter(Point2D center, Point2D corner) {
  231. setFrameFromCenter(center.getX(), center.getY(),
  232. corner.getX(), corner.getY());
  233. }
  234. /**
  235. * Tests if a specified <code>Point2D</code> is inside the boundary
  236. * of the <code>Shape</code>.
  237. * @param p the specified <code>Point2D</code>
  238. * @return <code>true</code> if the <code>Point2D</code> is inside the
  239. * <code>Shape</code> object's boundary;
  240. * <code>false</code> otherwise.
  241. */
  242. public boolean contains(Point2D p) {
  243. return contains(p.getX(), p.getY());
  244. }
  245. /**
  246. * Tests if the interior of the<code>Shape</code> intersects the
  247. * interior of a specified <code>Rectangle2D</code>.
  248. * @param r the specified <code>Rectangle2D</code>
  249. * @return <code>true</code> if the <code>Shape</code> and the
  250. * specified <code>Rectangle2D</code> intersect each other;
  251. * <code>false</code> otherwise.
  252. */
  253. public boolean intersects(Rectangle2D r) {
  254. return intersects(r.getX(), r.getY(), r.getWidth(), r.getHeight());
  255. }
  256. /**
  257. * Tests if the interior of the <code>Shape</code> entirely contains the
  258. * specified <code>Rectangle2D</code>.
  259. * @param r the specified <code>Rectangle2D</code>
  260. * @return <code>true</code> if the <code>Shape</code> entirely contains
  261. * the specified <code>Rectangle2D</code>
  262. * <code>false</code> otherwise.
  263. */
  264. public boolean contains(Rectangle2D r) {
  265. return contains(r.getX(), r.getY(), r.getWidth(), r.getHeight());
  266. }
  267. /**
  268. * Returns the bounding box of the <code>Shape</code>.
  269. * @return a {@link Rectangle} object that bounds the
  270. * <code>Shape</code>.
  271. */
  272. public Rectangle getBounds() {
  273. double width = getWidth();
  274. double height = getHeight();
  275. if (width < 0 || height < 0) {
  276. return new Rectangle();
  277. }
  278. double x = getX();
  279. double y = getY();
  280. double x1 = Math.floor(x);
  281. double y1 = Math.floor(y);
  282. double x2 = Math.ceil(x + width);
  283. double y2 = Math.ceil(y + height);
  284. return new Rectangle((int) x1, (int) y1,
  285. (int) (x2 - x1), (int) (y2 - y1));
  286. }
  287. /**
  288. * Returns an iterator object that iterates along the
  289. * <code>Shape</code> object's boundary and provides access to a
  290. * flattened view of the outline of the <code>Shape</code>
  291. * object's geometry.
  292. * <p>
  293. * Only SEG_MOVETO, SEG_LINETO, and SEG_CLOSE point types will
  294. * be returned by the iterator.
  295. * <p>
  296. * The amount of subdivision of the curved segments is controlled
  297. * by the <code>flatness</code> parameter, which specifies the
  298. * maximum distance that any point on the unflattened transformed
  299. * curve can deviate from the returned flattened path segments.
  300. * An optional {@link AffineTransform} can
  301. * be specified so that the coordinates returned in the iteration are
  302. * transformed accordingly.
  303. * @param at an optional <code>AffineTransform</code> to be applied to the
  304. * coordinates as they are returned in the iteration,
  305. * or <code>null</code> if untransformed coordinates are desired.
  306. * @param flatness the maximum distance that the line segments used to
  307. * approximate the curved segments are allowed to deviate
  308. * from any point on the original curve
  309. * @return a <code>PathIterator</code> object that provides access to
  310. * the <code>Shape</code> object's flattened geometry.
  311. */
  312. public PathIterator getPathIterator(AffineTransform at, double flatness) {
  313. return new FlatteningPathIterator(getPathIterator(at), flatness);
  314. }
  315. /**
  316. * Creates a new object of the same class and with the same
  317. * contents as this object.
  318. * @return a clone of this instance.
  319. * @exception OutOfMemoryError if there is not enough memory.
  320. * @see java.lang.Cloneable
  321. * @since 1.2
  322. */
  323. public Object clone() {
  324. try {
  325. return super.clone();
  326. } catch (CloneNotSupportedException e) {
  327. // this shouldn't happen, since we are Cloneable
  328. throw new InternalError();
  329. }
  330. }
  331. }