1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2013, 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
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
22  * USA.
23  *
24  * [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
25  * Other names may be trademarks of their respective owners.]
26  *
27  * -------------
28  * ClipPath.java
29  * -------------
30  * (C) Copyright 2003-2013, by David M. O'Donnell and Contributors.
31  *
32  * Original Author:  David M. O'Donnell;
33  * Contributor(s):   David Gilbert (for Object Refinery Limited);
34  *                   Nicolas Brodu;
35  *
36  * Changes
37  * -------
38  * 22-Apr-2003 : Added standard header (DG);
39  * 09-May-2003 : Added AxisLocation (DG);
40  * 11-Sep-2003 : Implemented Cloneable (NB);
41  * 21-Jan-2004 : Update for renamed method in ValueAxis (DG);
42  * ------------- JFREECHART 1.0.x ---------------------------------------------
43  * 31-Jan-2007 : Deprecated (DG);
44  *
45  */
46 
47 package org.jfree.chart;
48 
49 import java.awt.BasicStroke;
50 import java.awt.Composite;
51 import java.awt.Graphics2D;
52 import java.awt.Paint;
53 import java.awt.Stroke;
54 import java.awt.geom.GeneralPath;
55 import java.awt.geom.Rectangle2D;
56 
57 import org.jfree.chart.axis.ValueAxis;
58 import org.jfree.chart.plot.XYPlot;
59 import org.jfree.chart.renderer.xy.XYBlockRenderer;
60 import org.jfree.ui.RectangleEdge;
61 
62 /**
63  * This class would typically be used with a
64  * {@link org.jfree.chart.plot.ContourPlot}.  It allows the user to define a
65  * <code>GeneralPath</code> curve in plot coordinates.  This curve can then be
66  * used mask off or define regions within the contour plot.  The data must be
67  * sorted.
68  *
69  * @deprecated This class is no longer supported (as of version 1.0.4).  If
70  *     you are creating contour plots, please try to use {@link XYPlot} and
71  *     {@link XYBlockRenderer}.
72  */
73 public class ClipPath implements Cloneable {
74 
75     /** The x values. */
76     private double[] xValue = null;
77 
78     /** The y values. */
79     private double[] yValue = null;
80 
81     /** Controls whether drawing will be clipped (
82      * false would still allow the drawing or filling of path */
83     private boolean clip = true;
84 
85     /** Controls whether the path is drawn as an outline. */
86     private boolean drawPath = false;
87 
88     /** Controls whether the path is filled. */
89     private boolean fillPath = false;
90 
91     /** The fill paint. */
92     private Paint fillPaint = null;
93 
94     /** The draw paint. */
95     private Paint drawPaint = null;
96 
97     /** The draw stroke. */
98     private Stroke drawStroke = null;
99 
100     /** The composite. */
101     private Composite composite = null;
102 
103     /**
104      * Constructor for ClipPath.
105      */
ClipPath()106     public ClipPath() {
107         super();
108     }
109 
110     /**
111      * Constructor for ClipPath.
112      * Default values are assumed for the fillPath and drawPath options as
113      * false and true respectively.  The fillPaint is set to Color.GRAY, the
114      * drawColor is Color.BLUE, the stroke is BasicStroke(1)
115      * and the composite is AlphaComposite.Src.
116      *
117      * @param xValue  x coordinates of curved to be created
118      * @param yValue  y coordinates of curved to be created
119      */
ClipPath(double[] xValue, double[] yValue)120     public ClipPath(double[] xValue, double[] yValue) {
121         this(xValue, yValue, true, false, true);
122     }
123 
124 
125     /**
126      * Constructor for ClipPath.
127      * The fillPaint is set to Color.GRAY, the drawColor is Color.BLUE, the
128      * stroke is BasicStroke(1) and the composite is AlphaComposite.Src.
129      *
130      * @param xValue  x coordinates of curved to be created
131      * @param yValue  y coordinates of curved to be created
132      * @param clip  clip?
133      * @param fillPath  whether the path is to filled
134      * @param drawPath  whether the path is to drawn as an outline
135      */
ClipPath(double[] xValue, double[] yValue, boolean clip, boolean fillPath, boolean drawPath)136     public ClipPath(double[] xValue, double[] yValue,
137                     boolean clip, boolean fillPath, boolean drawPath) {
138         this.xValue = xValue;
139         this.yValue = yValue;
140 
141         this.clip = clip;
142         this.fillPath = fillPath;
143         this.drawPath = drawPath;
144 
145         this.fillPaint = java.awt.Color.gray;
146         this.drawPaint = java.awt.Color.blue;
147         this.drawStroke = new BasicStroke(1);
148         this.composite = java.awt.AlphaComposite.Src;
149     }
150 
151     /**
152      * Constructor for ClipPath.
153      *
154      * @param xValue  x coordinates of curved to be created
155      * @param yValue  y coordinates of curved to be created
156      * @param fillPath  whether the path is to filled
157      * @param drawPath  whether the path is to drawn as an outline
158      * @param fillPaint  the fill paint
159      * @param drawPaint  the outline stroke color
160      * @param drawStroke  the stroke style
161      * @param composite  the composite rule
162      */
ClipPath(double[] xValue, double[] yValue, boolean fillPath, boolean drawPath, Paint fillPaint, Paint drawPaint, Stroke drawStroke, Composite composite)163     public ClipPath(double[] xValue, double[] yValue, boolean fillPath,
164                     boolean drawPath, Paint fillPaint, Paint drawPaint,
165                     Stroke drawStroke, Composite composite) {
166 
167         this.xValue = xValue;
168         this.yValue = yValue;
169 
170         this.fillPath = fillPath;
171         this.drawPath = drawPath;
172 
173         this.fillPaint = fillPaint;
174         this.drawPaint = drawPaint;
175         this.drawStroke = drawStroke;
176         this.composite = composite;
177 
178     }
179 
180     /**
181      * Draws the clip path.
182      *
183      * @param g2  current graphics2D.
184      * @param dataArea  the dataArea that the plot is being draw in.
185      * @param horizontalAxis  the horizontal axis.
186      * @param verticalAxis  the vertical axis.
187      *
188      * @return The GeneralPath defining the outline
189      */
draw(Graphics2D g2, Rectangle2D dataArea, ValueAxis horizontalAxis, ValueAxis verticalAxis)190     public GeneralPath draw(Graphics2D g2,
191                             Rectangle2D dataArea,
192                             ValueAxis horizontalAxis, ValueAxis verticalAxis) {
193 
194         GeneralPath generalPath = generateClipPath(
195             dataArea, horizontalAxis, verticalAxis
196         );
197         if (this.fillPath || this.drawPath) {
198             Composite saveComposite = g2.getComposite();
199             Paint savePaint = g2.getPaint();
200             Stroke saveStroke = g2.getStroke();
201 
202             if (this.fillPaint != null) {
203                 g2.setPaint(this.fillPaint);
204             }
205             if (this.composite != null) {
206                 g2.setComposite(this.composite);
207             }
208             if (this.fillPath) {
209                 g2.fill(generalPath);
210             }
211 
212             if (this.drawStroke != null) {
213                 g2.setStroke(this.drawStroke);
214             }
215             if (this.drawPath) {
216                 g2.draw(generalPath);
217             }
218             g2.setPaint(savePaint);
219             g2.setComposite(saveComposite);
220             g2.setStroke(saveStroke);
221         }
222         return generalPath;
223 
224     }
225 
226     /**
227      * Generates the clip path.
228      *
229      * @param dataArea  the dataArea that the plot is being draw in.
230      * @param horizontalAxis  the horizontal axis.
231      * @param verticalAxis  the vertical axis.
232      *
233      * @return The GeneralPath defining the outline
234      */
generateClipPath(Rectangle2D dataArea, ValueAxis horizontalAxis, ValueAxis verticalAxis)235     public GeneralPath generateClipPath(Rectangle2D dataArea,
236                                         ValueAxis horizontalAxis,
237                                         ValueAxis verticalAxis) {
238 
239         GeneralPath generalPath = new GeneralPath();
240         double transX = horizontalAxis.valueToJava2D(
241             this.xValue[0], dataArea, RectangleEdge.BOTTOM
242         );
243         double transY = verticalAxis.valueToJava2D(
244             this.yValue[0], dataArea, RectangleEdge.LEFT
245         );
246         generalPath.moveTo((float) transX, (float) transY);
247         for (int k = 0; k < this.yValue.length; k++) {
248             transX = horizontalAxis.valueToJava2D(
249                 this.xValue[k], dataArea, RectangleEdge.BOTTOM
250             );
251             transY = verticalAxis.valueToJava2D(
252                 this.yValue[k], dataArea, RectangleEdge.LEFT
253             );
254             generalPath.lineTo((float) transX, (float) transY);
255         }
256         generalPath.closePath();
257 
258         return generalPath;
259 
260     }
261 
262     /**
263      * Returns the composite.
264      *
265      * @return Composite
266      */
getComposite()267     public Composite getComposite() {
268         return this.composite;
269     }
270 
271     /**
272      * Returns the drawPaint.
273      *
274      * @return Paint
275      */
getDrawPaint()276     public Paint getDrawPaint() {
277         return this.drawPaint;
278     }
279 
280     /**
281      * Returns the drawPath.
282      *
283      * @return boolean
284      */
isDrawPath()285     public boolean isDrawPath() {
286         return this.drawPath;
287     }
288 
289     /**
290      * Returns the drawStroke.
291      *
292      * @return Stroke
293      */
getDrawStroke()294     public Stroke getDrawStroke() {
295         return this.drawStroke;
296     }
297 
298     /**
299      * Returns the fillPaint.
300      *
301      * @return Paint
302      */
getFillPaint()303     public Paint getFillPaint() {
304         return this.fillPaint;
305     }
306 
307     /**
308      * Returns the fillPath.
309      *
310      * @return boolean
311      */
isFillPath()312     public boolean isFillPath() {
313         return this.fillPath;
314     }
315 
316     /**
317      * Returns the xValue.
318      *
319      * @return double[]
320      */
getXValue()321     public double[] getXValue() {
322         return this.xValue;
323     }
324 
325     /**
326      * Returns the yValue.
327      *
328      * @return double[]
329      */
getYValue()330     public double[] getYValue() {
331         return this.yValue;
332     }
333 
334     /**
335      * Sets the composite.
336      *
337      * @param composite The composite to set
338      */
setComposite(Composite composite)339     public void setComposite(Composite composite) {
340         this.composite = composite;
341     }
342 
343     /**
344      * Sets the drawPaint.
345      *
346      * @param drawPaint The drawPaint to set
347      */
setDrawPaint(Paint drawPaint)348     public void setDrawPaint(Paint drawPaint) {
349         this.drawPaint = drawPaint;
350     }
351 
352     /**
353      * Sets the drawPath.
354      *
355      * @param drawPath The drawPath to set
356      */
setDrawPath(boolean drawPath)357     public void setDrawPath(boolean drawPath) {
358         this.drawPath = drawPath;
359     }
360 
361     /**
362      * Sets the drawStroke.
363      *
364      * @param drawStroke The drawStroke to set
365      */
setDrawStroke(Stroke drawStroke)366     public void setDrawStroke(Stroke drawStroke) {
367         this.drawStroke = drawStroke;
368     }
369 
370     /**
371      * Sets the fillPaint.
372      *
373      * @param fillPaint The fillPaint to set
374      */
setFillPaint(Paint fillPaint)375     public void setFillPaint(Paint fillPaint) {
376         this.fillPaint = fillPaint;
377     }
378 
379     /**
380      * Sets the fillPath.
381      *
382      * @param fillPath The fillPath to set
383      */
setFillPath(boolean fillPath)384     public void setFillPath(boolean fillPath) {
385         this.fillPath = fillPath;
386     }
387 
388     /**
389      * Sets the xValue.
390      *
391      * @param xValue The xValue to set
392      */
setXValue(double[] xValue)393     public void setXValue(double[] xValue) {
394         this.xValue = xValue;
395     }
396 
397     /**
398      * Sets the yValue.
399      *
400      * @param yValue The yValue to set
401      */
setYValue(double[] yValue)402     public void setYValue(double[] yValue) {
403         this.yValue = yValue;
404     }
405 
406     /**
407      * Returns the clip.
408      *
409      * @return boolean
410      */
isClip()411     public boolean isClip() {
412         return this.clip;
413     }
414 
415     /**
416      * Sets the clip.
417      *
418      * @param clip The clip to set
419      */
setClip(boolean clip)420     public void setClip(boolean clip) {
421         this.clip = clip;
422     }
423 
424     /**
425      * Returns a clone of the object (a deeper clone than default to avoid bugs
426      * when setting values in cloned object).
427      *
428      * @return The clone.
429      *
430      * @throws CloneNotSupportedException if cloning is not supported.
431      */
432     @Override
clone()433     public Object clone() throws CloneNotSupportedException {
434         ClipPath clone = (ClipPath) super.clone();
435         clone.xValue = (double[]) this.xValue.clone();
436         clone.yValue = (double[]) this.yValue.clone();
437         return clone;
438     }
439 
440 }
441