GeneralPath.java 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /* GeneralPath.java -- represents a shape built from subpaths
  2. Copyright (C) 2002 Free Software Foundation
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.awt.geom;
  32. import java.awt.Rectangle;
  33. import java.awt.Shape;
  34. /**
  35. * STUBS ONLY
  36. * XXX Implement and document. Note that Sun's implementation only expects
  37. * float precision, not double.
  38. */
  39. public final class GeneralPath implements Shape, Cloneable
  40. {
  41. public static final int WIND_EVEN_ODD = PathIterator.WIND_EVEN_ODD;
  42. public static final int WIND_NON_ZERO = PathIterator.WIND_NON_ZERO;
  43. /** Initial size if not specified. */
  44. private static final int INIT_SIZE = 20;
  45. /** The winding rule. */
  46. private int rule;
  47. /**
  48. * The path type in points. Note that points[index] maps to
  49. * types[index >> 1]; the control points of quad and cubic paths map as
  50. * well but are ignored.
  51. */
  52. private byte[] types;
  53. /**
  54. * The list of all points seen. Since you can only append floats, it makes
  55. * sense for this to be a float[]. I have no idea why Sun didn't choose to
  56. * allow a general path of double precision points.
  57. */
  58. private float[] points;
  59. /** The index of the most recent moveto point, or null. */
  60. private int subpath = -1;
  61. /** The next available index into points. */
  62. private int index;
  63. public GeneralPath()
  64. {
  65. this(WIND_NON_ZERO, INIT_SIZE);
  66. }
  67. public GeneralPath(int rule)
  68. {
  69. this(rule, INIT_SIZE);
  70. }
  71. public GeneralPath(int rule, int capacity)
  72. {
  73. if (rule != WIND_EVEN_ODD && rule != WIND_NON_ZERO)
  74. throw new IllegalArgumentException();
  75. this.rule = rule;
  76. if (capacity < INIT_SIZE)
  77. capacity = INIT_SIZE;
  78. types = new byte[capacity >> 1];
  79. points = new float[capacity];
  80. }
  81. public GeneralPath(Shape s)
  82. {
  83. types = new byte[INIT_SIZE >> 1];
  84. points = new float[INIT_SIZE];
  85. PathIterator pi = s.getPathIterator(null);
  86. setWindingRule(pi.getWindingRule());
  87. append(pi, false);
  88. }
  89. public void moveTo(float x, float y)
  90. {
  91. subpath = index;
  92. ensureSize(index + 2);
  93. types[index >> 1] = PathIterator.SEG_MOVETO;
  94. points[index++] = x;
  95. points[index++] = y;
  96. }
  97. public void lineTo(float x, float y)
  98. {
  99. ensureSize(index + 2);
  100. types[index >> 1] = PathIterator.SEG_LINETO;
  101. points[index++] = x;
  102. points[index++] = y;
  103. }
  104. public void quadTo(float x1, float y1, float x2, float y2)
  105. {
  106. ensureSize(index + 4);
  107. types[index >> 1] = PathIterator.SEG_QUADTO;
  108. points[index++] = x1;
  109. points[index++] = y1;
  110. points[index++] = x2;
  111. points[index++] = y2;
  112. }
  113. public void curveTo(float x1, float y1, float x2, float y2,
  114. float x3, float y3)
  115. {
  116. ensureSize(index + 6);
  117. types[index >> 1] = PathIterator.SEG_QUADTO;
  118. points[index++] = x1;
  119. points[index++] = y1;
  120. points[index++] = x2;
  121. points[index++] = y2;
  122. points[index++] = x3;
  123. points[index++] = y3;
  124. }
  125. public void closePath()
  126. {
  127. ensureSize(index + 2);
  128. types[index >> 1] = PathIterator.SEG_CLOSE;
  129. points[index++] = points[subpath];
  130. points[index++] = points[subpath + 1];
  131. }
  132. public void append(Shape s, boolean connect)
  133. {
  134. append(s.getPathIterator(null), connect);
  135. }
  136. public void append(PathIterator i, boolean connect)
  137. {
  138. float[] f = new float[6];
  139. while (! i.isDone())
  140. {
  141. int result = i.currentSegment(f);
  142. switch (result)
  143. {
  144. case PathIterator.SEG_MOVETO:
  145. if (! connect)
  146. {
  147. moveTo(f[0], f[1]);
  148. break;
  149. }
  150. if (subpath >= 0 && f[0] == points[subpath]
  151. && f[1] == points[subpath + 1])
  152. break;
  153. // Fallthrough.
  154. case PathIterator.SEG_LINETO:
  155. lineTo(f[0], f[1]);
  156. break;
  157. case PathIterator.SEG_QUADTO:
  158. quadTo(f[0], f[1], f[2], f[3]);
  159. break;
  160. case PathIterator.SEG_CUBICTO:
  161. curveTo(f[0], f[1], f[2], f[3], f[4], f[5]);
  162. break;
  163. default:
  164. closePath();
  165. }
  166. connect = false;
  167. }
  168. }
  169. public int getWindingRule()
  170. {
  171. return rule;
  172. }
  173. public void setWindingRule(int rule)
  174. {
  175. if (rule != WIND_EVEN_ODD && rule != WIND_NON_ZERO)
  176. throw new IllegalArgumentException();
  177. this.rule = rule;
  178. }
  179. public Point2D getCurrentPoint()
  180. {
  181. if (subpath < 0)
  182. return null;
  183. return new Point2D.Float(points[subpath], points[subpath + 1]);
  184. }
  185. public void reset()
  186. {
  187. subpath = -1;
  188. index = 0;
  189. }
  190. public void transform(AffineTransform xform)
  191. {
  192. xform.transform(points, 0, points, 0, index >> 1);
  193. }
  194. public Shape createTransformedShape(AffineTransform xform)
  195. {
  196. GeneralPath p = new GeneralPath(this);
  197. p.transform(xform);
  198. return p;
  199. }
  200. public Rectangle getBounds()
  201. {
  202. return getBounds2D().getBounds();
  203. }
  204. public Rectangle2D getBounds2D()
  205. {
  206. // XXX Implement.
  207. throw new Error("not implemented");
  208. }
  209. public boolean contains(double x, double y)
  210. {
  211. // XXX Implement.
  212. throw new Error("not implemented");
  213. }
  214. public boolean contains(Point2D p)
  215. {
  216. return contains(p.getX(), p.getY());
  217. }
  218. public boolean contains(double x, double y, double w, double h)
  219. {
  220. // XXX Implement.
  221. throw new Error("not implemented");
  222. }
  223. public boolean contains(Rectangle2D r)
  224. {
  225. return contains(r.getX(), r.getY(), r.getWidth(), r.getHeight());
  226. }
  227. public boolean intersects(double x, double y, double w, double h)
  228. {
  229. // XXX Implement.
  230. throw new Error("not implemented");
  231. }
  232. public boolean intersects(Rectangle2D r)
  233. {
  234. return intersects(r.getX(), r.getY(), r.getWidth(), r.getHeight());
  235. }
  236. public PathIterator getPathIterator(final AffineTransform at)
  237. {
  238. return new PathIterator()
  239. {
  240. int current = 0;
  241. public int getWindingRule()
  242. {
  243. return rule;
  244. }
  245. public boolean isDone()
  246. {
  247. return current >= index;
  248. }
  249. public void next()
  250. {
  251. current++;
  252. }
  253. public int currentSegment(float[] coords)
  254. {
  255. if (current >= index)
  256. return SEG_CLOSE;
  257. int result = types[current >> 1];
  258. int i = 0;
  259. if (result == 3)
  260. {
  261. coords[i++] = points[current++];
  262. coords[i++] = points[current++];
  263. }
  264. if (result == 2)
  265. {
  266. coords[i++] = points[current++];
  267. coords[i++] = points[current++];
  268. }
  269. if (result < 2)
  270. {
  271. coords[i++] = points[current++];
  272. coords[i++] = points[current++];
  273. if (at != null)
  274. at.transform(coords, 0, coords, 0, result == 0 ? 1 : result);
  275. }
  276. return result;
  277. }
  278. public int currentSegment(double[] coords)
  279. {
  280. if (current >= index)
  281. return SEG_CLOSE;
  282. int result = types[current >> 1];
  283. int i = 0;
  284. if (result == 3)
  285. {
  286. coords[i++] = points[current++];
  287. coords[i++] = points[current++];
  288. }
  289. if (result == 2)
  290. {
  291. coords[i++] = points[current++];
  292. coords[i++] = points[current++];
  293. }
  294. if (result < 2)
  295. {
  296. coords[i++] = points[current++];
  297. coords[i++] = points[current++];
  298. if (at != null)
  299. at.transform(coords, 0, coords, 0, result == 0 ? 1 : result);
  300. }
  301. return result;
  302. }
  303. };
  304. }
  305. public PathIterator getPathIterator(AffineTransform at, double flatness)
  306. {
  307. return new FlatteningPathIterator(getPathIterator(at), flatness);
  308. }
  309. /**
  310. * Create a new shape of the same run-time type with the same contents as
  311. * this one.
  312. *
  313. * @return the clone
  314. *
  315. * @exception OutOfMemoryError If there is not enough memory available.
  316. *
  317. * @since 1.2
  318. */
  319. public Object clone()
  320. {
  321. // This class is final; no need to use super.clone().
  322. return new GeneralPath(this);
  323. }
  324. private void ensureSize(int size)
  325. {
  326. if (subpath < 0)
  327. throw new IllegalPathStateException("need initial moveto");
  328. if (size <= points.length)
  329. return;
  330. byte[] b = new byte[points.length];
  331. System.arraycopy(types, 0, b, 0, index >> 1);
  332. types = b;
  333. float[] f = new float[points.length << 1];
  334. System.arraycopy(points, 0, f, 0, index);
  335. points = f;
  336. }
  337. } // class GeneralPath