View Javadoc

1   /*
2    * SerializableGeneralPath.java
3    * 
4    * Created on December 8, 2001, 12:57 PM Last edited on October 11, 2002
5    */
6   package nl.javel.gisbeans.geom;
7   
8   import java.awt.Shape;
9   import java.awt.geom.GeneralPath;
10  import java.awt.geom.PathIterator;
11  import java.io.IOException;
12  
13  /***
14   * The SerializableGeneralPath class is a serializable version of the
15   * <code>java.awt.geom.GeneralPath</code> class.
16   * 
17   * @author <a href="mailto:peter.jacobs@javel.nl">Peter Jacobs </a> <br>
18   *         <a href="mailto:paul.jacobs@javel.nl">Paul Jacobs </a>
19   * @since JDK 1.2
20   * @version 1.0
21   */
22  public class SerializableGeneralPath implements java.io.Serializable,
23  		java.awt.Shape, java.lang.Cloneable
24  {
25  
26  	/*** the general path */
27  	private GeneralPath generalPath;
28  
29  	/***
30  	 * @see java.awt.geom.GeneralPath#GeneralPath()
31  	 */
32  	public SerializableGeneralPath()
33  	{
34  		this.generalPath = new GeneralPath();
35  	}
36  
37  	/***
38  	 * 
39  	 * constructs a new SerializableGeneralPath
40  	 * 
41  	 * @param rule the windingRule
42  	 */
43  	public SerializableGeneralPath(final int rule)
44  	{
45  		this.generalPath = new GeneralPath(rule);
46  	}
47  
48  	/***
49  	 * constructs a new SerializableGeneralPath
50  	 * 
51  	 * @param rule the windingRule
52  	 * @param initialCapacity the initialCapacity
53  	 */
54  	public SerializableGeneralPath(final int rule, final int initialCapacity)
55  	{
56  		this.generalPath = new GeneralPath(rule, initialCapacity);
57  	}
58  
59  	/***
60  	 * constructs a new SerializableGeneralPath
61  	 * 
62  	 * @param s the shape
63  	 */
64  	public SerializableGeneralPath(final Shape s)
65  	{
66  		this.generalPath = new GeneralPath(s);
67  	}
68  
69  	/***
70  	 * @param pi the pathIterator
71  	 * @param connect the connect
72  	 */
73  	public void append(final PathIterator pi, final boolean connect)
74  	{
75  		this.generalPath.append(pi, connect);
76  	}
77  
78  	/***
79  	 * @param s the shape
80  	 * @param connect whether to connect
81  	 */
82  	public void append(final Shape s, final boolean connect)
83  	{
84  		this.generalPath.append(s, connect);
85  	}
86  
87  	/***
88  	 * @see java.lang.Object#clone()
89  	 */
90  	public Object clone()
91  	{
92  		SerializableGeneralPath clone = new SerializableGeneralPath();
93  		clone.generalPath = (GeneralPath) this.generalPath.clone();
94  		return clone;
95  	}
96  
97  	/***
98  	 * @see java.awt.geom.GeneralPath#closePath()
99  	 */
100 	public void closePath()
101 	{
102 		this.generalPath.closePath();
103 	}
104 
105 	/***
106 	 * @see java.awt.Shape#contains(double, double, double, double)
107 	 */
108 	public boolean contains(final double param, final double param1,
109 			final double param2, final double param3)
110 	{
111 		return this.generalPath.contains(param, param1, param2, param3);
112 	}
113 
114 	/***
115 	 * @see java.awt.Shape#contains(double, double)
116 	 */
117 	public boolean contains(final double param, final double param1)
118 	{
119 		return this.generalPath.contains(param, param1);
120 	}
121 
122 	/***
123 	 * @see java.awt.Shape#contains(java.awt.geom.Rectangle2D)
124 	 */
125 	public boolean contains(final java.awt.geom.Rectangle2D rectangle2D)
126 	{
127 		return this.generalPath.contains(rectangle2D);
128 	}
129 
130 	/***
131 	 * @see java.awt.Shape#contains(java.awt.geom.Point2D)
132 	 */
133 	public boolean contains(final java.awt.geom.Point2D point2D)
134 	{
135 		return this.generalPath.contains(point2D);
136 	}
137 
138 	/***
139 	 * @param at the effinetransform
140 	 * @return the new shape
141 	 */
142 	public java.awt.Shape createTranformedShape(
143 			final java.awt.geom.AffineTransform at)
144 	{
145 		return this.generalPath.createTransformedShape(at);
146 	}
147 
148 	/***
149 	 * curves to
150 	 * 
151 	 * @param x1
152 	 * @param y1
153 	 * @param x2
154 	 * @param y2
155 	 * @param x3
156 	 * @param y3
157 	 */
158 	public void curveTo(final float x1, final float y1, final float x2,
159 			final float y2, final float x3, final float y3)
160 	{
161 		this.generalPath.curveTo(x1, y1, x2, y2, x3, y3);
162 	}
163 
164 	/***
165 	 * @see java.awt.Shape#getBounds()
166 	 */
167 	public java.awt.Rectangle getBounds()
168 	{
169 		return this.generalPath.getBounds();
170 	}
171 
172 	/***
173 	 * @see java.awt.Shape#getBounds2D()
174 	 */
175 	public java.awt.geom.Rectangle2D getBounds2D()
176 	{
177 		return this.generalPath.getBounds2D();
178 	}
179 
180 	/***
181 	 * @return
182 	 */
183 	public java.awt.geom.Point2D getCurrentPoint()
184 	{
185 		return this.generalPath.getCurrentPoint();
186 	}
187 
188 	/***
189 	 * @see java.awt.Shape#getPathIterator(java.awt.geom.AffineTransform)
190 	 */
191 	public java.awt.geom.PathIterator getPathIterator(
192 			final java.awt.geom.AffineTransform affineTransform)
193 	{
194 		return this.generalPath.getPathIterator(affineTransform);
195 	}
196 
197 	/***
198 	 * @see java.awt.Shape#getPathIterator(java.awt.geom.AffineTransform,
199 	 *      double)
200 	 */
201 	public java.awt.geom.PathIterator getPathIterator(
202 			final java.awt.geom.AffineTransform affineTransform,
203 			final double param)
204 	{
205 		return this.generalPath.getPathIterator(affineTransform, param);
206 	}
207 
208 	/***
209 	 * @return
210 	 */
211 	public int getWindingRule()
212 	{
213 		return this.generalPath.getWindingRule();
214 	}
215 
216 	/***
217 	 * @see java.awt.Shape#intersects(double, double, double, double)
218 	 */
219 	public boolean intersects(final double param, final double param1,
220 			final double param2, final double param3)
221 	{
222 		return this.generalPath.intersects(param, param1, param2, param3);
223 	}
224 
225 	/***
226 	 * @see java.awt.Shape#intersects(java.awt.geom.Rectangle2D)
227 	 */
228 	public boolean intersects(final java.awt.geom.Rectangle2D rectangle2D)
229 	{
230 		return this.generalPath.intersects(rectangle2D);
231 	}
232 
233 	/***
234 	 * @param x
235 	 * @param y
236 	 */
237 	public void lineTo(final float x, final float y)
238 	{
239 		this.generalPath.lineTo(x, y);
240 	}
241 
242 	/***
243 	 * @param x
244 	 * @param y
245 	 */
246 	public void moveTo(final float x, final float y)
247 	{
248 		this.generalPath.moveTo(x, y);
249 	}
250 
251 	/***
252 	 * @param x1
253 	 * @param y1
254 	 * @param x2
255 	 * @param y2
256 	 */
257 	public void quadTo(final float x1, final float y1, final float x2,
258 			final float y2)
259 	{
260 		this.generalPath.quadTo(x1, y1, x2, y2);
261 	}
262 
263 	/***
264 	 * @see java.awt.geom.GeneralPath#reset()
265 	 */
266 	public void reset()
267 	{
268 		this.generalPath.reset();
269 	}
270 
271 	/***
272 	 * @param rule
273 	 */
274 	public void setWindingRule(final int rule)
275 	{
276 		this.generalPath.setWindingRule(rule);
277 	}
278 
279 	/***
280 	 * @param at
281 	 */
282 	public void transform(final java.awt.geom.AffineTransform at)
283 	{
284 		this.generalPath.transform(at);
285 	}
286 
287 	/***
288 	 * writes a float array
289 	 * 
290 	 * @param out the output stream
291 	 * @param array the array
292 	 * @param length the length
293 	 * @throws java.io.IOException on exception
294 	 */
295 	private void writeFloatArray(final java.io.ObjectOutputStream out,
296 			final float[] array, final int length) throws java.io.IOException
297 	{
298 		for (int i = 0; i < length; i++)
299 		{
300 			out.writeFloat(array[i]);
301 		}
302 	}
303 
304 	/***
305 	 * writes to the stream
306 	 * 
307 	 * @param out the stream
308 	 * @throws IOException on IOfailure
309 	 */
310 	private void writeObject(final java.io.ObjectOutputStream out)
311 			throws IOException
312 	{
313 		out.writeInt(this.generalPath.getWindingRule());
314 		float[] coords = new float[6];
315 		PathIterator i = this.generalPath.getPathIterator(null);
316 		// Now the Path iterator is present, we simply walk along the shape and
317 		// serialize the points...
318 		while (!i.isDone())
319 		{
320 			int segment = i.currentSegment(coords);
321 			out.writeInt(segment);
322 			switch (segment)
323 			{
324 				case PathIterator.SEG_CLOSE :
325 					writeFloatArray(out, coords, 0);
326 					//no float is serialized.. Keeps the bytestream as
327 					// minimal as possible
328 					break;
329 				case PathIterator.SEG_CUBICTO :
330 					writeFloatArray(out, coords, 6);
331 					//All 6 floats are used and therfore serialized.
332 					break;
333 				case PathIterator.SEG_LINETO :
334 					writeFloatArray(out, coords, 2);
335 					//2 floats are used and serialized.. Keeps the
336 					// bytestream as minimal as possible
337 					break;
338 				case PathIterator.SEG_MOVETO :
339 					writeFloatArray(out, coords, 2);
340 					//2 floats are used and serialized.. Keeps the
341 					// bytestream as minimal as possible
342 					break;
343 				case PathIterator.SEG_QUADTO :
344 					writeFloatArray(out, coords, 4);
345 					//2 floats are used and serialized.. Keeps the
346 					// bytestream as minimal as possible
347 					break;
348 				default :
349 					throw new RuntimeException("unkown segment");
350 			}
351 			i.next();
352 		}
353 		out.writeInt(-1); //We are ready and give an end-signal
354 	}
355 
356 	/***
357 	 * reads an object
358 	 * 
359 	 * @param in the inputstream
360 	 * @throws IOException on IOException
361 	 */
362 	private void readObject(final java.io.ObjectInputStream in)
363 			throws IOException
364 	{
365 		this.generalPath = new GeneralPath(in.readInt());
366 		int segment;
367 		while ((segment = in.readInt()) != -1)
368 		//The -1 value was our ending point...
369 		{
370 			switch (segment)
371 			{
372 				case PathIterator.SEG_CLOSE :
373 					this.generalPath.closePath();
374 					break;
375 				case PathIterator.SEG_CUBICTO :
376 					this.generalPath.curveTo(in.readFloat(), in.readFloat(), in
377 							.readFloat(), in.readFloat(), in.readFloat(), in
378 							.readFloat());
379 					break;
380 				case PathIterator.SEG_LINETO :
381 					this.generalPath.lineTo(in.readFloat(), in.readFloat());
382 					break;
383 				case PathIterator.SEG_MOVETO :
384 					this.generalPath.moveTo(in.readFloat(), in.readFloat());
385 					break;
386 				case PathIterator.SEG_QUADTO :
387 					this.generalPath.quadTo(in.readFloat(), in.readFloat(), in
388 							.readFloat(), in.readFloat());
389 					break;
390 				default :
391 					throw new RuntimeException("unkown segment");
392 			}
393 		}
394 	}
395 }