KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > plot > DefaultDrawingSupplier


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this library; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
24  * in the United States and other countries.]
25  *
26  * ---------------------------
27  * DefaultDrawingSupplier.java
28  * ---------------------------
29  * (C) Copyright 2003, 2004, by Object Refinery Limited.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): Jeremy Bowman;
33  *
34  * $Id: DefaultDrawingSupplier.java,v 1.6 2005/05/19 14:03:42 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 16-Jan-2003 : Version 1 (DG);
39  * 17-Jan-2003 : Added stroke method, renamed DefaultPaintSupplier
40  * --> DefaultDrawingSupplier (DG)
41  * 27-Jan-2003 : Incorporated code from SeriesShapeFactory, originally
42  * contributed by Jeremy Bowman (DG);
43  * 25-Mar-2003 : Implemented Serializable (DG);
44  * 20-Aug-2003 : Implemented Cloneable and PublicCloneable (DG);
45  *
46  */

47
48  package org.jfree.chart.plot;
49
50 import java.awt.BasicStroke JavaDoc;
51 import java.awt.Color JavaDoc;
52 import java.awt.Paint JavaDoc;
53 import java.awt.Polygon JavaDoc;
54 import java.awt.Shape JavaDoc;
55 import java.awt.Stroke JavaDoc;
56 import java.awt.geom.Ellipse2D JavaDoc;
57 import java.awt.geom.Rectangle2D JavaDoc;
58 import java.io.IOException JavaDoc;
59 import java.io.ObjectInputStream JavaDoc;
60 import java.io.ObjectOutputStream JavaDoc;
61 import java.io.Serializable JavaDoc;
62 import java.util.Arrays JavaDoc;
63
64 import org.jfree.chart.ChartColor;
65 import org.jfree.io.SerialUtilities;
66 import org.jfree.util.PublicCloneable;
67 import org.jfree.util.ShapeUtilities;
68
69 /**
70  * A default implementation of the {@link DrawingSupplier} interface.
71  *
72  */

73 public class DefaultDrawingSupplier implements DrawingSupplier,
74                                                Cloneable JavaDoc,
75                                                PublicCloneable,
76                                                Serializable JavaDoc {
77
78     /** For serialization. */
79     private static final long serialVersionUID = -7339847061039422538L;
80     
81     /** The default fill paint sequence. */
82     public static final Paint JavaDoc[] DEFAULT_PAINT_SEQUENCE
83         = ChartColor.createDefaultPaintArray();
84
85     /** The default outline paint sequence. */
86     public static final Paint JavaDoc[] DEFAULT_OUTLINE_PAINT_SEQUENCE = new Paint JavaDoc[] {
87                                     Color.lightGray
88                                 };
89
90     /** The default stroke sequence. */
91     public static final Stroke JavaDoc[] DEFAULT_STROKE_SEQUENCE = new Stroke JavaDoc[] {
92                                     new BasicStroke JavaDoc(1.0f,
93                                                     BasicStroke.CAP_SQUARE,
94                                                     BasicStroke.JOIN_BEVEL)
95                                 };
96
97     /** The default outline stroke sequence. */
98     public static final Stroke JavaDoc[] DEFAULT_OUTLINE_STROKE_SEQUENCE
99         = new Stroke JavaDoc[] {
100             new BasicStroke JavaDoc(
101                 1.0f, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_BEVEL
102             )
103         };
104
105     /** The default shape sequence. */
106     public static final Shape JavaDoc[] DEFAULT_SHAPE_SEQUENCE
107         = createStandardSeriesShapes();
108
109     /** The paint sequence. */
110     private transient Paint JavaDoc[] paintSequence;
111
112     /** The current paint index. */
113     private int paintIndex;
114
115     /** The outline paint sequence. */
116     private transient Paint JavaDoc[] outlinePaintSequence;
117
118     /** The current outline paint index. */
119     private int outlinePaintIndex;
120
121     /** The stroke sequence. */
122     private transient Stroke JavaDoc[] strokeSequence;
123
124     /** The current stroke index. */
125     private int strokeIndex;
126
127     /** The outline stroke sequence. */
128     private transient Stroke JavaDoc[] outlineStrokeSequence;
129
130     /** The current outline stroke index. */
131     private int outlineStrokeIndex;
132
133     /** The shape sequence. */
134     private transient Shape JavaDoc[] shapeSequence;
135
136     /** The current shape index. */
137     private int shapeIndex;
138
139     /**
140      * Creates a new supplier, with default sequences for fill paint, outline
141      * paint, stroke and shapes.
142      */

143     public DefaultDrawingSupplier() {
144
145         this(DEFAULT_PAINT_SEQUENCE,
146              DEFAULT_OUTLINE_PAINT_SEQUENCE,
147              DEFAULT_STROKE_SEQUENCE,
148              DEFAULT_OUTLINE_STROKE_SEQUENCE,
149              DEFAULT_SHAPE_SEQUENCE);
150
151     }
152
153     /**
154      * Creates a new supplier.
155      *
156      * @param paintSequence the fill paint sequence.
157      * @param outlinePaintSequence the outline paint sequence.
158      * @param strokeSequence the stroke sequence.
159      * @param outlineStrokeSequence the outline stroke sequence.
160      * @param shapeSequence the shape sequence.
161      */

162     public DefaultDrawingSupplier(Paint JavaDoc[] paintSequence,
163                                   Paint JavaDoc[] outlinePaintSequence,
164                                   Stroke JavaDoc[] strokeSequence,
165                                   Stroke JavaDoc[] outlineStrokeSequence,
166                                   Shape JavaDoc[] shapeSequence) {
167
168         this.paintSequence = paintSequence;
169         this.outlinePaintSequence = outlinePaintSequence;
170         this.strokeSequence = strokeSequence;
171         this.outlineStrokeSequence = outlineStrokeSequence;
172         this.shapeSequence = shapeSequence;
173
174     }
175
176     /**
177      * Returns the next paint in the sequence.
178      *
179      * @return The paint.
180      */

181     public Paint JavaDoc getNextPaint() {
182         Paint JavaDoc result
183             = this.paintSequence[this.paintIndex % this.paintSequence.length];
184         this.paintIndex++;
185         return result;
186     }
187
188     /**
189      * Returns the next outline paint in the sequence.
190      *
191      * @return The paint.
192      */

193     public Paint JavaDoc getNextOutlinePaint() {
194         Paint JavaDoc result = this.outlinePaintSequence[
195             this.outlinePaintIndex % this.outlinePaintSequence.length
196         ];
197         this.outlinePaintIndex++;
198         return result;
199     }
200
201     /**
202      * Returns the next stroke in the sequence.
203      *
204      * @return The stroke.
205      */

206     public Stroke JavaDoc getNextStroke() {
207         Stroke JavaDoc result = this.strokeSequence[
208             this.strokeIndex % this.strokeSequence.length
209         ];
210         this.strokeIndex++;
211         return result;
212     }
213
214     /**
215      * Returns the next outline stroke in the sequence.
216      *
217      * @return The stroke.
218      */

219     public Stroke JavaDoc getNextOutlineStroke() {
220         Stroke JavaDoc result = this.outlineStrokeSequence[
221             this.outlineStrokeIndex % this.outlineStrokeSequence.length
222         ];
223         this.outlineStrokeIndex++;
224         return result;
225     }
226
227     /**
228      * Returns the next shape in the sequence.
229      *
230      * @return The shape.
231      */

232     public Shape JavaDoc getNextShape() {
233         Shape JavaDoc result = this.shapeSequence[
234             this.shapeIndex % this.shapeSequence.length
235         ];
236         this.shapeIndex++;
237         return result;
238     }
239
240     /**
241      * Creates an array of standard shapes to display for the items in series
242      * on charts.
243      *
244      * @return The array of shapes.
245      */

246     public static Shape JavaDoc[] createStandardSeriesShapes() {
247
248         Shape JavaDoc[] result = new Shape JavaDoc[10];
249
250         double size = 6.0;
251         double delta = size / 2.0;
252         int[] xpoints = null;
253         int[] ypoints = null;
254
255         // square
256
result[0] = new Rectangle2D.Double JavaDoc(-delta, -delta, size, size);
257         // circle
258
result[1] = new Ellipse2D.Double JavaDoc(-delta, -delta, size, size);
259
260         // up-pointing triangle
261
xpoints = intArray(0.0, delta, -delta);
262         ypoints = intArray(-delta, delta, delta);
263         result[2] = new Polygon JavaDoc(xpoints, ypoints, 3);
264
265         // diamond
266
xpoints = intArray(0.0, delta, 0.0, -delta);
267         ypoints = intArray(-delta, 0.0, delta, 0.0);
268         result[3] = new Polygon JavaDoc(xpoints, ypoints, 4);
269
270         // horizontal rectangle
271
result[4] = new Rectangle2D.Double JavaDoc(-delta, -delta / 2, size, size / 2);
272
273         // down-pointing triangle
274
xpoints = intArray(-delta, +delta, 0.0);
275         ypoints = intArray(-delta, -delta, delta);
276         result[5] = new Polygon JavaDoc(xpoints, ypoints, 3);
277
278         // horizontal ellipse
279
result[6] = new Ellipse2D.Double JavaDoc(-delta, -delta / 2, size, size / 2);
280
281         // right-pointing triangle
282
xpoints = intArray(-delta, delta, -delta);
283         ypoints = intArray(-delta, 0.0, delta);
284         result[7] = new Polygon JavaDoc(xpoints, ypoints, 3);
285
286         // vertical rectangle
287
result[8] = new Rectangle2D.Double JavaDoc(-delta / 2, -delta, size / 2, size);
288
289         // left-pointing triangle
290
xpoints = intArray(-delta, delta, delta);
291         ypoints = intArray(0.0, -delta, +delta);
292         result[9] = new Polygon JavaDoc(xpoints, ypoints, 3);
293
294         return result;
295
296     }
297
298     /**
299      * Tests this object for equality with another object.
300      *
301      * @param obj the object (<code>null</code> permitted).
302      *
303      * @return A boolean.
304      */

305     public boolean equals(Object JavaDoc obj) {
306
307         if (obj == this) {
308             return true;
309         }
310
311         if (!(obj instanceof DefaultDrawingSupplier)) {
312             return false;
313         }
314
315         DefaultDrawingSupplier that = (DefaultDrawingSupplier) obj;
316
317         if (!Arrays.equals(this.paintSequence, that.paintSequence)) {
318             return false;
319         }
320         if (this.paintIndex != that.paintIndex) {
321             return false;
322         }
323         if (!Arrays.equals(this.outlinePaintSequence,
324                 that.outlinePaintSequence)) {
325             return false;
326         }
327         if (this.outlinePaintIndex != that.outlinePaintIndex) {
328             return false;
329         }
330         if (!Arrays.equals(this.strokeSequence, that.strokeSequence)) {
331             return false;
332         }
333         if (this.strokeIndex != that.strokeIndex) {
334             return false;
335         }
336         if (!Arrays.equals(this.outlineStrokeSequence,
337                 that.outlineStrokeSequence)) {
338             return false;
339         }
340         if (this.outlineStrokeIndex != that.outlineStrokeIndex) {
341             return false;
342         }
343         if (!equalShapes(this.shapeSequence, that.shapeSequence)) {
344             return false;
345         }
346         if (this.shapeIndex != that.shapeIndex) {
347             return false;
348         }
349         return true;
350
351     }
352     
353     /**
354      * A utility method for testing the equality of two arrays of shapes.
355      *
356      * @param s1 the first array (<code>null</code> permitted).
357      * @param s2 the second array (<code>null</code> permitted).
358      *
359      * @return A boolean.
360      */

361     private boolean equalShapes(Shape JavaDoc[] s1, Shape JavaDoc[] s2) {
362         if (s1 == null) {
363             return s2 == null;
364         }
365         if (s2 == null) {
366             return false;
367         }
368         if (s1.length != s2.length) {
369             return false;
370         }
371         for (int i = 0; i < s1.length; i++) {
372             if (!ShapeUtilities.equal(s1[i], s2[i])) {
373                 return false;
374             }
375         }
376         return true;
377     }
378
379     /**
380      * Handles serialization.
381      *
382      * @param stream the output stream.
383      *
384      * @throws IOException if there is an I/O problem.
385      */

386     private void writeObject(ObjectOutputStream JavaDoc stream) throws IOException JavaDoc {
387         stream.defaultWriteObject();
388
389         int paintCount = this.paintSequence.length;
390         stream.writeInt(paintCount);
391         for (int i = 0; i < paintCount; i++) {
392             SerialUtilities.writePaint(this.paintSequence[i], stream);
393         }
394
395         int outlinePaintCount = this.outlinePaintSequence.length;
396         stream.writeInt(outlinePaintCount);
397         for (int i = 0; i < outlinePaintCount; i++) {
398             SerialUtilities.writePaint(this.outlinePaintSequence[i], stream);
399         }
400
401         int strokeCount = this.strokeSequence.length;
402         stream.writeInt(strokeCount);
403         for (int i = 0; i < strokeCount; i++) {
404             SerialUtilities.writeStroke(this.strokeSequence[i], stream);
405         }
406
407         int outlineStrokeCount = this.outlineStrokeSequence.length;
408         stream.writeInt(outlineStrokeCount);
409         for (int i = 0; i < outlineStrokeCount; i++) {
410             SerialUtilities.writeStroke(this.outlineStrokeSequence[i], stream);
411         }
412
413         int shapeCount = this.shapeSequence.length;
414         stream.writeInt(shapeCount);
415         for (int i = 0; i < shapeCount; i++) {
416             SerialUtilities.writeShape(this.shapeSequence[i], stream);
417         }
418
419     }
420
421     /**
422      * Restores a serialized object.
423      *
424      * @param stream the input stream.
425      *
426      * @throws IOException if there is an I/O problem.
427      * @throws ClassNotFoundException if there is a problem loading a class.
428      */

429     private void readObject(ObjectInputStream JavaDoc stream)
430         throws IOException JavaDoc, ClassNotFoundException JavaDoc {
431         stream.defaultReadObject();
432
433         int paintCount = stream.readInt();
434         this.paintSequence = new Paint JavaDoc[paintCount];
435         for (int i = 0; i < paintCount; i++) {
436             this.paintSequence[i] = SerialUtilities.readPaint(stream);
437         }
438
439         int outlinePaintCount = stream.readInt();
440         this.outlinePaintSequence = new Paint JavaDoc[outlinePaintCount];
441         for (int i = 0; i < outlinePaintCount; i++) {
442             this.outlinePaintSequence[i] = SerialUtilities.readPaint(stream);
443         }
444
445         int strokeCount = stream.readInt();
446         this.strokeSequence = new Stroke JavaDoc[strokeCount];
447         for (int i = 0; i < strokeCount; i++) {
448             this.strokeSequence[i] = SerialUtilities.readStroke(stream);
449         }
450
451         int outlineStrokeCount = stream.readInt();
452         this.outlineStrokeSequence = new Stroke JavaDoc[outlineStrokeCount];
453         for (int i = 0; i < outlineStrokeCount; i++) {
454             this.outlineStrokeSequence[i] = SerialUtilities.readStroke(stream);
455         }
456
457         int shapeCount = stream.readInt();
458         this.shapeSequence = new Shape JavaDoc[shapeCount];
459         for (int i = 0; i < shapeCount; i++) {
460             this.shapeSequence[i] = SerialUtilities.readShape(stream);
461         }
462
463     }
464
465     /**
466      * Helper method to avoid lots of explicit casts in getShape(). Returns
467      * an array containing the provided doubles cast to ints.
468      *
469      * @param a x
470      * @param b y
471      * @param c z
472      *
473      * @return int[3] with converted params.
474      */

475     private static int[] intArray(double a, double b, double c) {
476         return new int[] {(int) a, (int) b, (int) c};
477     }
478
479     /**
480      * Helper method to avoid lots of explicit casts in getShape(). Returns
481      * an array containing the provided doubles cast to ints.
482      *
483      * @param a x
484      * @param b y
485      * @param c z
486      * @param d t
487      *
488      * @return int[3] with converted params.
489      */

490     private static int[] intArray(double a, double b, double c, double d) {
491         return new int[] {(int) a, (int) b, (int) c, (int) d};
492     }
493
494     /**
495      * Returns a clone.
496      *
497      * @return A clone.
498      *
499      * @throws CloneNotSupportedException if a component of the supplier does
500      * not support cloning.
501      */

502     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
503         DefaultDrawingSupplier clone = (DefaultDrawingSupplier) super.clone();
504         return clone;
505     }
506 }
507
Popular Tags