KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > ClipPath


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  * ClipPath.java
28  * -------------
29  * (C) Copyright 2003, 2004, by David M. O'Donnell and Contributors.
30  *
31  * Original Author: David M. O'Donnell;
32  * Contributor(s): David Gilbert (for Object Refinery Limited);
33  * Nicolas Brodu;
34  *
35  * $Id: ClipPath.java,v 1.2 2005/03/28 19:38:39 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 22-Apr-2003 : Added standard header (DG);
40  * 09-May-2003 : Added AxisLocation (DG);
41  * 11-Sep-2003 : Implemented Cloneable (NB);
42  * 21-Jan-2004 : Update for renamed method in ValueAxis (DG);
43  *
44  */

45
46 package org.jfree.chart;
47
48 import java.awt.BasicStroke JavaDoc;
49 import java.awt.Composite JavaDoc;
50 import java.awt.Graphics2D JavaDoc;
51 import java.awt.Paint JavaDoc;
52 import java.awt.Stroke JavaDoc;
53 import java.awt.geom.GeneralPath JavaDoc;
54 import java.awt.geom.Rectangle2D JavaDoc;
55
56 import org.jfree.chart.axis.ValueAxis;
57 import org.jfree.ui.RectangleEdge;
58
59 /**
60  * This class would typically be used with a
61  * {@link org.jfree.chart.plot.ContourPlot}. It allows the user to define a
62  * <code>GeneralPath</code> curve in plot coordinates. This curve can then be
63  * used mask off or define regions within the contour plot. The data must be
64  * sorted.
65  *
66  * @author dmo
67  */

68 public class ClipPath implements Cloneable JavaDoc {
69
70     /** The x values. */
71     private double[] xValue = null;
72     
73     /** The y values. */
74     private double[] yValue = null;
75
76     /** Controls whether drawing will be clipped (
77      * false would still allow the drawing or filling of path */

78     private boolean clip = true;
79
80     /** Controls whether the path is drawn as an outline. */
81     private boolean drawPath = false;
82
83     /** Controls whether the path is filled. */
84     private boolean fillPath = false;
85
86     /** The fill paint. */
87     private Paint JavaDoc fillPaint = null;
88     
89     /** The draw paint. */
90     private Paint JavaDoc drawPaint = null;
91     
92     /** The draw stroke. */
93     private Stroke JavaDoc drawStroke = null;
94     
95     /** The composite. */
96     private Composite JavaDoc composite = null;
97
98     /**
99      * Constructor for ClipPath.
100      */

101     public ClipPath() {
102         super();
103     }
104
105     /**
106      * Constructor for ClipPath.
107      * Default values are assumed for the fillPath and drawPath options as
108      * false and true respectively. The fillPaint is set to Color.GRAY, the
109      * drawColor is Color.BLUE, the stroke is BasicStroke(1)
110      * and the composite is AlphaComposite.Src.
111      *
112      * @param xValue x coordinates of curved to be created
113      * @param yValue y coordinates of curved to be created
114      */

115     public ClipPath(double[] xValue, double[] yValue) {
116         this(xValue, yValue, true, false, true);
117     }
118
119
120     /**
121      * Constructor for ClipPath.
122      * The fillPaint is set to Color.GRAY, the drawColor is Color.BLUE, the
123      * stroke is BasicStroke(1) and the composite is AlphaComposite.Src.
124      *
125      * @param xValue x coordinates of curved to be created
126      * @param yValue y coordinates of curved to be created
127      * @param clip clip?
128      * @param fillPath whether the path is to filled
129      * @param drawPath whether the path is to drawn as an outline
130      */

131     public ClipPath(double[] xValue, double[] yValue,
132                     boolean clip, boolean fillPath, boolean drawPath) {
133         this.xValue = xValue;
134         this.yValue = yValue;
135
136         this.clip = clip;
137         this.fillPath = fillPath;
138         this.drawPath = drawPath;
139
140         this.fillPaint = java.awt.Color.gray;
141         this.drawPaint = java.awt.Color.blue;
142         this.drawStroke = new BasicStroke JavaDoc(1);
143         this.composite = java.awt.AlphaComposite.Src;
144     }
145
146     /**
147      * Constructor for ClipPath.
148      *
149      * @param xValue x coordinates of curved to be created
150      * @param yValue y coordinates of curved to be created
151      * @param fillPath whether the path is to filled
152      * @param drawPath whether the path is to drawn as an outline
153      * @param fillPaint the fill paint
154      * @param drawPaint the outline stroke color
155      * @param drawStroke the stroke style
156      * @param composite the composite rule
157      */

158     public ClipPath(double[] xValue, double[] yValue, boolean fillPath,
159                     boolean drawPath, Paint JavaDoc fillPaint, Paint JavaDoc drawPaint,
160                     Stroke JavaDoc drawStroke, Composite JavaDoc composite) {
161
162         this.xValue = xValue;
163         this.yValue = yValue;
164
165         this.fillPath = fillPath;
166         this.drawPath = drawPath;
167
168         this.fillPaint = fillPaint;
169         this.drawPaint = drawPaint;
170         this.drawStroke = drawStroke;
171         this.composite = composite;
172
173     }
174
175     /**
176      * Draws the clip path.
177      *
178      * @param g2 current graphics2D.
179      * @param dataArea the dataArea that the plot is being draw in.
180      * @param horizontalAxis the horizontal axis.
181      * @param verticalAxis the vertical axis.
182      *
183      * @return The GeneralPath defining the outline
184      */

185     public GeneralPath JavaDoc draw(Graphics2D JavaDoc g2,
186                             Rectangle2D JavaDoc dataArea,
187                             ValueAxis horizontalAxis, ValueAxis verticalAxis) {
188
189         GeneralPath JavaDoc generalPath = generateClipPath(
190             dataArea, horizontalAxis, verticalAxis
191         );
192         if (this.fillPath || this.drawPath) {
193             Composite JavaDoc saveComposite = g2.getComposite();
194             Paint JavaDoc savePaint = g2.getPaint();
195             Stroke JavaDoc saveStroke = g2.getStroke();
196
197             if (this.fillPaint != null) {
198                 g2.setPaint(this.fillPaint);
199             }
200             if (this.composite != null) {
201                 g2.setComposite(this.composite);
202             }
203             if (this.fillPath) {
204                 g2.fill(generalPath);
205             }
206
207             if (this.drawStroke != null) {
208                 g2.setStroke(this.drawStroke);
209             }
210             if (this.drawPath) {
211                 g2.draw(generalPath);
212             }
213             g2.setPaint(savePaint);
214             g2.setComposite(saveComposite);
215             g2.setStroke(saveStroke);
216         }
217         return generalPath;
218
219     }
220
221     /**
222      * Generates the clip path.
223      *
224      * @param dataArea the dataArea that the plot is being draw in.
225      * @param horizontalAxis the horizontal axis.
226      * @param verticalAxis the vertical axis.
227      *
228      * @return The GeneralPath defining the outline
229      */

230     public GeneralPath JavaDoc generateClipPath(Rectangle2D JavaDoc dataArea,
231                                         ValueAxis horizontalAxis,
232                                         ValueAxis verticalAxis) {
233
234         GeneralPath JavaDoc generalPath = new GeneralPath JavaDoc();
235         double transX = horizontalAxis.valueToJava2D(
236             this.xValue[0], dataArea, RectangleEdge.BOTTOM
237         );
238         double transY = verticalAxis.valueToJava2D(
239             this.yValue[0], dataArea, RectangleEdge.LEFT
240         );
241         generalPath.moveTo((float) transX, (float) transY);
242         for (int k = 0; k < this.yValue.length; k++) {
243             transX = horizontalAxis.valueToJava2D(
244                 this.xValue[k], dataArea, RectangleEdge.BOTTOM
245             );
246             transY = verticalAxis.valueToJava2D(
247                 this.yValue[k], dataArea, RectangleEdge.LEFT
248             );
249             generalPath.lineTo((float) transX, (float) transY);
250         }
251         generalPath.closePath();
252
253         return generalPath;
254
255     }
256
257     /**
258      * Returns the composite.
259      *
260      * @return Composite
261      */

262     public Composite JavaDoc getComposite() {
263         return this.composite;
264     }
265
266     /**
267      * Returns the drawPaint.
268      *
269      * @return Paint
270      */

271     public Paint JavaDoc getDrawPaint() {
272         return this.drawPaint;
273     }
274
275     /**
276      * Returns the drawPath.
277      *
278      * @return boolean
279      */

280     public boolean isDrawPath() {
281         return this.drawPath;
282     }
283
284     /**
285      * Returns the drawStroke.
286      *
287      * @return Stroke
288      */

289     public Stroke JavaDoc getDrawStroke() {
290         return this.drawStroke;
291     }
292
293     /**
294      * Returns the fillPaint.
295      *
296      * @return Paint
297      */

298     public Paint JavaDoc getFillPaint() {
299         return this.fillPaint;
300     }
301
302     /**
303      * Returns the fillPath.
304      *
305      * @return boolean
306      */

307     public boolean isFillPath() {
308         return this.fillPath;
309     }
310
311     /**
312      * Returns the xValue.
313      *
314      * @return double[]
315      */

316     public double[] getXValue() {
317         return this.xValue;
318     }
319
320     /**
321      * Returns the yValue.
322      *
323      * @return double[]
324      */

325     public double[] getYValue() {
326         return this.yValue;
327     }
328
329     /**
330      * Sets the composite.
331      *
332      * @param composite The composite to set
333      */

334     public void setComposite(Composite JavaDoc composite) {
335         this.composite = composite;
336     }
337
338     /**
339      * Sets the drawPaint.
340      *
341      * @param drawPaint The drawPaint to set
342      */

343     public void setDrawPaint(Paint JavaDoc drawPaint) {
344         this.drawPaint = drawPaint;
345     }
346
347     /**
348      * Sets the drawPath.
349      *
350      * @param drawPath The drawPath to set
351      */

352     public void setDrawPath(boolean drawPath) {
353         this.drawPath = drawPath;
354     }
355
356     /**
357      * Sets the drawStroke.
358      *
359      * @param drawStroke The drawStroke to set
360      */

361     public void setDrawStroke(Stroke JavaDoc drawStroke) {
362         this.drawStroke = drawStroke;
363     }
364
365     /**
366      * Sets the fillPaint.
367      *
368      * @param fillPaint The fillPaint to set
369      */

370     public void setFillPaint(Paint JavaDoc fillPaint) {
371         this.fillPaint = fillPaint;
372     }
373
374     /**
375      * Sets the fillPath.
376      *
377      * @param fillPath The fillPath to set
378      */

379     public void setFillPath(boolean fillPath) {
380         this.fillPath = fillPath;
381     }
382
383     /**
384      * Sets the xValue.
385      *
386      * @param xValue The xValue to set
387      */

388     public void setXValue(double[] xValue) {
389         this.xValue = xValue;
390     }
391
392     /**
393      * Sets the yValue.
394      *
395      * @param yValue The yValue to set
396      */

397     public void setYValue(double[] yValue) {
398         this.yValue = yValue;
399     }
400
401     /**
402      * Returns the clip.
403      *
404      * @return boolean
405      */

406     public boolean isClip() {
407         return this.clip;
408     }
409
410     /**
411      * Sets the clip.
412      *
413      * @param clip The clip to set
414      */

415     public void setClip(boolean clip) {
416         this.clip = clip;
417     }
418
419     /**
420      * Returns a clone of the object (a deeper clone than default to avoid bugs
421      * when setting values in cloned object).
422      *
423      * @return The clone.
424      *
425      * @throws CloneNotSupportedException if cloning is not supported.
426      */

427     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
428         ClipPath clone = (ClipPath) super.clone();
429         clone.xValue = (double[]) this.xValue.clone();
430         clone.yValue = (double[]) this.yValue.clone();
431         return clone;
432     }
433
434 }
435
Popular Tags