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