PageRenderTime 26ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/android/LGame-Android-0.2.6S/org/loon/framework/android/game/core/graphics/geom/RectangularShape.java

http://loon-simple.googlecode.com/
Java | 421 lines | 98 code | 28 blank | 295 comment | 4 complexity | e9aae5b500c80cf1f1276bf2be1b13f9 MD5 | raw file
  1. /*
  2. * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
  3. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4. *
  5. * This code is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 only, as
  7. * published by the Free Software Foundation. Sun designates this
  8. * particular file as subject to the "Classpath" exception as provided
  9. * by Sun in the LICENSE file that accompanied this code.
  10. *
  11. * This code is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  14. * version 2 for more details (a copy is included in the LICENSE file that
  15. * accompanied this code).
  16. *
  17. * You should have received a copy of the GNU General Public License version
  18. * 2 along with this work; if not, write to the Free Software Foundation,
  19. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  22. * CA 95054 USA or visit www.sun.com if you need additional information or
  23. * have any questions.
  24. */
  25. package org.loon.framework.android.game.core.graphics.geom;
  26. /**
  27. * <code>RectangularShape</code> is the base class for a number of {@link Shape}
  28. * objects whose geometry is defined by a rectangular frame. This class does not
  29. * directly specify any specific geometry by itself, but merely provides
  30. * manipulation methods inherited by a whole category of <code>Shape</code>
  31. * objects. The manipulation methods provided by this class can be used to query
  32. * and modify the rectangular frame, which provides a reference for the
  33. * subclasses to define their geometry.
  34. *
  35. * @author Jim Graham
  36. * @since 1.2
  37. */
  38. public abstract class RectangularShape implements Shape, Cloneable {
  39. /**
  40. * This is an abstract class that cannot be instantiated directly.
  41. *
  42. * @see Arc2D
  43. * @see Ellipse2D
  44. * @see Rectangle2D
  45. * @see RoundRectangle2D
  46. * @since 1.2
  47. */
  48. protected RectangularShape() {
  49. }
  50. /**
  51. * Returns the X coordinate of the upper-left corner of the framing
  52. * rectangle in <code>double</code> precision.
  53. *
  54. * @return the X coordinate of the upper-left corner of the framing
  55. * rectangle.
  56. * @since 1.2
  57. */
  58. public abstract double getX();
  59. /**
  60. * Returns the Y coordinate of the upper-left corner of the framing
  61. * rectangle in <code>double</code> precision.
  62. *
  63. * @return the Y coordinate of the upper-left corner of the framing
  64. * rectangle.
  65. * @since 1.2
  66. */
  67. public abstract double getY();
  68. /**
  69. * Returns the width of the framing rectangle in <code>double</code>
  70. * precision.
  71. *
  72. * @return the width of the framing rectangle.
  73. * @since 1.2
  74. */
  75. public abstract double getWidth();
  76. /**
  77. * Returns the height of the framing rectangle in <code>double</code>
  78. * precision.
  79. *
  80. * @return the height of the framing rectangle.
  81. * @since 1.2
  82. */
  83. public abstract double getHeight();
  84. /**
  85. * Returns the smallest X coordinate of the framing rectangle of the
  86. * <code>Shape</code> in <code>double</code> precision.
  87. *
  88. * @return the smallest X coordinate of the framing rectangle of the
  89. * <code>Shape</code>.
  90. * @since 1.2
  91. */
  92. public double getMinX() {
  93. return getX();
  94. }
  95. /**
  96. * Returns the smallest Y coordinate of the framing rectangle of the
  97. * <code>Shape</code> in <code>double</code> precision.
  98. *
  99. * @return the smallest Y coordinate of the framing rectangle of the
  100. * <code>Shape</code>.
  101. * @since 1.2
  102. */
  103. public double getMinY() {
  104. return getY();
  105. }
  106. /**
  107. * Returns the largest X coordinate of the framing rectangle of the
  108. * <code>Shape</code> in <code>double</code> precision.
  109. *
  110. * @return the largest X coordinate of the framing rectangle of the
  111. * <code>Shape</code>.
  112. * @since 1.2
  113. */
  114. public double getMaxX() {
  115. return getX() + getWidth();
  116. }
  117. /**
  118. * Returns the largest Y coordinate of the framing rectangle of the
  119. * <code>Shape</code> in <code>double</code> precision.
  120. *
  121. * @return the largest Y coordinate of the framing rectangle of the
  122. * <code>Shape</code>.
  123. * @since 1.2
  124. */
  125. public double getMaxY() {
  126. return getY() + getHeight();
  127. }
  128. /**
  129. * Returns the X coordinate of the center of the framing rectangle of the
  130. * <code>Shape</code> in <code>double</code> precision.
  131. *
  132. * @return the X coordinate of the center of the framing rectangle of the
  133. * <code>Shape</code>.
  134. * @since 1.2
  135. */
  136. public double getCenterX() {
  137. return getX() + getWidth() / 2.0;
  138. }
  139. /**
  140. * Returns the Y coordinate of the center of the framing rectangle of the
  141. * <code>Shape</code> in <code>double</code> precision.
  142. *
  143. * @return the Y coordinate of the center of the framing rectangle of the
  144. * <code>Shape</code>.
  145. * @since 1.2
  146. */
  147. public double getCenterY() {
  148. return getY() + getHeight() / 2.0;
  149. }
  150. /**
  151. * Returns the framing {@link Rectangle2D} that defines the overall shape of
  152. * this object.
  153. *
  154. * @return a <code>Rectangle2D</code>, specified in <code>double</code>
  155. * coordinates.
  156. * @see #setFrame(double, double, double, double)
  157. * @see #setFrame(Point2D, Dimension2D)
  158. * @see #setFrame(Rectangle2D)
  159. * @since 1.2
  160. */
  161. public Rectangle2D getFrame() {
  162. return new Rectangle2D.Double(getX(), getY(), getWidth(), getHeight());
  163. }
  164. /**
  165. * Determines whether the <code>RectangularShape</code> is empty. When the
  166. * <code>RectangularShape</code> is empty, it encloses no area.
  167. *
  168. * @return <code>true</code> if the <code>RectangularShape</code> is empty;
  169. * <code>false</code> otherwise.
  170. * @since 1.2
  171. */
  172. public abstract boolean isEmpty();
  173. /**
  174. * Sets the location and size of the framing rectangle of this
  175. * <code>Shape</code> to the specified rectangular values.
  176. *
  177. * @param x
  178. * the X coordinate of the upper-left corner of the specified
  179. * rectangular shape
  180. * @param y
  181. * the Y coordinate of the upper-left corner of the specified
  182. * rectangular shape
  183. * @param w
  184. * the width of the specified rectangular shape
  185. * @param h
  186. * the height of the specified rectangular shape
  187. * @see #getFrame
  188. * @since 1.2
  189. */
  190. public abstract void setFrame(double x, double y, double w, double h);
  191. /**
  192. * Sets the location and size of the framing rectangle of this
  193. * <code>Shape</code> to the specified {@link Point2D} and
  194. * {@link Dimension2D}, respectively. The framing rectangle is used by the
  195. * subclasses of <code>RectangularShape</code> to define their geometry.
  196. *
  197. * @param loc
  198. * the specified <code>Point2D</code>
  199. * @param size
  200. * the specified <code>Dimension2D</code>
  201. * @see #getFrame
  202. * @since 1.2
  203. */
  204. public void setFrame(Point2D loc, Dimension2D size) {
  205. setFrame(loc.getX(), loc.getY(), size.getWidth(), size.getHeight());
  206. }
  207. /**
  208. * Sets the framing rectangle of this <code>Shape</code> to be the specified
  209. * <code>Rectangle2D</code>. The framing rectangle is used by the subclasses
  210. * of <code>RectangularShape</code> to define their geometry.
  211. *
  212. * @param r
  213. * the specified <code>Rectangle2D</code>
  214. * @see #getFrame
  215. * @since 1.2
  216. */
  217. public void setFrame(Rectangle2D r) {
  218. setFrame(r.getX(), r.getY(), r.getWidth(), r.getHeight());
  219. }
  220. /**
  221. * Sets the diagonal of the framing rectangle of this <code>Shape</code>
  222. * based on the two specified coordinates. The framing rectangle is used by
  223. * the subclasses of <code>RectangularShape</code> to define their geometry.
  224. *
  225. * @param x1
  226. * the X coordinate of the start point of the specified diagonal
  227. * @param y1
  228. * the Y coordinate of the start point of the specified diagonal
  229. * @param x2
  230. * the X coordinate of the end point of the specified diagonal
  231. * @param y2
  232. * the Y coordinate of the end point of the specified diagonal
  233. * @since 1.2
  234. */
  235. public void setFrameFromDiagonal(double x1, double y1, double x2, double y2) {
  236. if (x2 < x1) {
  237. double t = x1;
  238. x1 = x2;
  239. x2 = t;
  240. }
  241. if (y2 < y1) {
  242. double t = y1;
  243. y1 = y2;
  244. y2 = t;
  245. }
  246. setFrame(x1, y1, x2 - x1, y2 - y1);
  247. }
  248. /**
  249. * Sets the diagonal of the framing rectangle of this <code>Shape</code>
  250. * based on two specified <code>Point2D</code> objects. The framing
  251. * rectangle is used by the subclasses of <code>RectangularShape</code> to
  252. * define their geometry.
  253. *
  254. * @param p1
  255. * the start <code>Point2D</code> of the specified diagonal
  256. * @param p2
  257. * the end <code>Point2D</code> of the specified diagonal
  258. * @since 1.2
  259. */
  260. public void setFrameFromDiagonal(Point2D p1, Point2D p2) {
  261. setFrameFromDiagonal(p1.getX(), p1.getY(), p2.getX(), p2.getY());
  262. }
  263. /**
  264. * Sets the framing rectangle of this <code>Shape</code> based on the
  265. * specified center point coordinates and corner point coordinates. The
  266. * framing rectangle is used by the subclasses of
  267. * <code>RectangularShape</code> to define their geometry.
  268. *
  269. * @param centerX
  270. * the X coordinate of the specified center point
  271. * @param centerY
  272. * the Y coordinate of the specified center point
  273. * @param cornerX
  274. * the X coordinate of the specified corner point
  275. * @param cornerY
  276. * the Y coordinate of the specified corner point
  277. * @since 1.2
  278. */
  279. public void setFrameFromCenter(double centerX, double centerY,
  280. double cornerX, double cornerY) {
  281. double halfW = Math.abs(cornerX - centerX);
  282. double halfH = Math.abs(cornerY - centerY);
  283. setFrame(centerX - halfW, centerY - halfH, halfW * 2.0, halfH * 2.0);
  284. }
  285. /**
  286. * Sets the framing rectangle of this <code>Shape</code> based on a
  287. * specified center <code>Point2D</code> and corner <code>Point2D</code>.
  288. * The framing rectangle is used by the subclasses of
  289. * <code>RectangularShape</code> to define their geometry.
  290. *
  291. * @param center
  292. * the specified center <code>Point2D</code>
  293. * @param corner
  294. * the specified corner <code>Point2D</code>
  295. * @since 1.2
  296. */
  297. public void setFrameFromCenter(Point2D center, Point2D corner) {
  298. setFrameFromCenter(center.getX(), center.getY(), corner.getX(), corner
  299. .getY());
  300. }
  301. /**
  302. * {@inheritDoc}
  303. *
  304. * @since 1.2
  305. */
  306. public boolean contains(Point2D p) {
  307. return contains(p.getX(), p.getY());
  308. }
  309. /**
  310. * {@inheritDoc}
  311. *
  312. * @since 1.2
  313. */
  314. public boolean intersects(Rectangle2D r) {
  315. return intersects(r.getX(), r.getY(), r.getWidth(), r.getHeight());
  316. }
  317. /**
  318. * {@inheritDoc}
  319. *
  320. * @since 1.2
  321. */
  322. public boolean contains(Rectangle2D r) {
  323. return contains(r.getX(), r.getY(), r.getWidth(), r.getHeight());
  324. }
  325. /**
  326. * {@inheritDoc}
  327. *
  328. * @since 1.2
  329. */
  330. public Rectangle getBounds() {
  331. double width = getWidth();
  332. double height = getHeight();
  333. if (width < 0 || height < 0) {
  334. return new Rectangle();
  335. }
  336. double x = getX();
  337. double y = getY();
  338. double x1 = Math.floor(x);
  339. double y1 = Math.floor(y);
  340. double x2 = Math.ceil(x + width);
  341. double y2 = Math.ceil(y + height);
  342. return new Rectangle((int) x1, (int) y1, (int) (x2 - x1),
  343. (int) (y2 - y1));
  344. }
  345. /**
  346. * Returns an iterator object that iterates along the <code>Shape</code>
  347. * object's boundary and provides access to a flattened view of the outline
  348. * of the <code>Shape</code> object's geometry.
  349. * <p>
  350. * Only SEG_MOVETO, SEG_LINETO, and SEG_CLOSE point types will be returned
  351. * by the iterator.
  352. * <p>
  353. * The amount of subdivision of the curved segments is controlled by the
  354. * <code>flatness</code> parameter, which specifies the maximum distance
  355. * that any point on the unflattened transformed curve can deviate from the
  356. * returned flattened path segments. An optional {@link AffineTransform} can
  357. * be specified so that the coordinates returned in the iteration are
  358. * transformed accordingly.
  359. *
  360. * @param at
  361. * an optional <code>AffineTransform</code> to be applied to the
  362. * coordinates as they are returned in the iteration, or
  363. * <code>null</code> if untransformed coordinates are desired.
  364. * @param flatness
  365. * the maximum distance that the line segments used to
  366. * approximate the curved segments are allowed to deviate from
  367. * any point on the original curve
  368. * @return a <code>PathIterator</code> object that provides access to the
  369. * <code>Shape</code> object's flattened geometry.
  370. * @since 1.2
  371. */
  372. public PathIterator getPathIterator(AffineTransform at, double flatness) {
  373. return new FlatteningPathIterator(getPathIterator(at), flatness);
  374. }
  375. /**
  376. * Creates a new object of the same class and with the same contents as this
  377. * object.
  378. *
  379. * @return a clone of this instance.
  380. * @exception OutOfMemoryError
  381. * if there is not enough memory.
  382. * @see java.lang.Cloneable
  383. * @since 1.2
  384. */
  385. public Object clone() {
  386. try {
  387. return super.clone();
  388. } catch (CloneNotSupportedException e) {
  389. // this shouldn't happen, since we are Cloneable
  390. throw new InternalError();
  391. }
  392. }
  393. }