001    /* ===========================================================
002     * JFreeChart : a free chart library for the Java(tm) platform
003     * ===========================================================
004     *
005     * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
006     *
007     * Project Info:  http://www.jfree.org/jfreechart/index.html
008     *
009     * This library is free software; you can redistribute it and/or modify it 
010     * under the terms of the GNU Lesser General Public License as published by 
011     * the Free Software Foundation; either version 2.1 of the License, or 
012     * (at your option) any later version.
013     *
014     * This library is distributed in the hope that it will be useful, but 
015     * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
016     * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
017     * License for more details.
018     *
019     * You should have received a copy of the GNU Lesser General Public
020     * License along with this library; if not, write to the Free Software
021     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
022     * USA.  
023     *
024     * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
025     * in the United States and other countries.]
026     *
027     * -------------
028     * ClipPath.java
029     * -------------
030     * (C) Copyright 2003, 2004, by David M. O'Donnell and Contributors.
031     *
032     * Original Author:  David M. O'Donnell;
033     * Contributor(s):   David Gilbert (for Object Refinery Limited);
034     *                   Nicolas Brodu;
035     *
036     * $Id: ClipPath.java,v 1.2.2.2 2007/01/31 15:56:18 mungady Exp $
037     *
038     * Changes
039     * -------
040     * 22-Apr-2003 : Added standard header (DG);
041     * 09-May-2003 : Added AxisLocation (DG);
042     * 11-Sep-2003 : Implemented Cloneable (NB);
043     * 21-Jan-2004 : Update for renamed method in ValueAxis (DG);
044     * ------------- JFREECHART 1.0.x ---------------------------------------------
045     * 31-Jan-2007 : Deprecated (DG);
046     *
047     */
048    
049    package org.jfree.chart;
050    
051    import java.awt.BasicStroke;
052    import java.awt.Composite;
053    import java.awt.Graphics2D;
054    import java.awt.Paint;
055    import java.awt.Stroke;
056    import java.awt.geom.GeneralPath;
057    import java.awt.geom.Rectangle2D;
058    
059    import org.jfree.chart.axis.ValueAxis;
060    import org.jfree.chart.plot.XYPlot;
061    import org.jfree.chart.renderer.xy.XYBlockRenderer;
062    import org.jfree.ui.RectangleEdge;
063    
064    /**
065     * This class would typically be used with a 
066     * {@link org.jfree.chart.plot.ContourPlot}.  It allows the user to define a 
067     * <code>GeneralPath</code> curve in plot coordinates.  This curve can then be 
068     * used mask off or define regions within the contour plot.  The data must be 
069     * sorted.
070     * 
071     * @deprecated This class is no longer supported.  If you are creating
072     *     contour plots, please try to use {@link XYPlot} and 
073     *     {@link XYBlockRenderer}.
074     */
075    public class ClipPath implements Cloneable {
076    
077        /** The x values. */
078        private double[] xValue = null;
079        
080        /** The y values. */
081        private double[] yValue = null;
082    
083        /** Controls whether drawing will be clipped (
084         * false would still allow the drawing or filling of path */
085        private boolean clip = true;
086    
087        /** Controls whether the path is drawn as an outline. */
088        private boolean drawPath = false;
089    
090        /** Controls whether the path is filled. */
091        private boolean fillPath = false;
092    
093        /** The fill paint. */
094        private Paint fillPaint = null;
095        
096        /** The draw paint. */
097        private Paint drawPaint = null;
098        
099        /** The draw stroke. */
100        private Stroke drawStroke = null;
101        
102        /** The composite. */
103        private Composite composite = null;
104    
105        /**
106         * Constructor for ClipPath.
107         */
108        public ClipPath() {
109            super();
110        }
111    
112        /**
113         * Constructor for ClipPath.
114         * Default values are assumed for the fillPath and drawPath options as 
115         * false and true respectively.  The fillPaint is set to Color.GRAY, the 
116         * drawColor is Color.BLUE, the stroke is BasicStroke(1)
117         * and the composite is AlphaComposite.Src.
118         *
119         * @param xValue  x coordinates of curved to be created
120         * @param yValue  y coordinates of curved to be created
121         */
122        public ClipPath(double[] xValue, double[] yValue) {
123            this(xValue, yValue, true, false, true);
124        }
125    
126    
127        /**
128         * Constructor for ClipPath.
129         * The fillPaint is set to Color.GRAY, the drawColor is Color.BLUE, the 
130         * stroke is BasicStroke(1) and the composite is AlphaComposite.Src.
131         *
132         * @param xValue  x coordinates of curved to be created
133         * @param yValue  y coordinates of curved to be created
134         * @param clip  clip?
135         * @param fillPath  whether the path is to filled
136         * @param drawPath  whether the path is to drawn as an outline
137         */
138        public ClipPath(double[] xValue, double[] yValue,
139                        boolean clip, boolean fillPath, boolean drawPath) {
140            this.xValue = xValue;
141            this.yValue = yValue;
142    
143            this.clip = clip;
144            this.fillPath = fillPath;
145            this.drawPath = drawPath;
146    
147            this.fillPaint = java.awt.Color.gray;
148            this.drawPaint = java.awt.Color.blue;
149            this.drawStroke = new BasicStroke(1);
150            this.composite = java.awt.AlphaComposite.Src;
151        }
152    
153        /**
154         * Constructor for ClipPath.
155         *
156         * @param xValue  x coordinates of curved to be created
157         * @param yValue  y coordinates of curved to be created
158         * @param fillPath  whether the path is to filled
159         * @param drawPath  whether the path is to drawn as an outline
160         * @param fillPaint  the fill paint
161         * @param drawPaint  the outline stroke color
162         * @param drawStroke  the stroke style
163         * @param composite  the composite rule
164         */
165        public ClipPath(double[] xValue, double[] yValue, boolean fillPath, 
166                        boolean drawPath, Paint fillPaint, Paint drawPaint, 
167                        Stroke drawStroke, Composite composite) {
168    
169            this.xValue = xValue;
170            this.yValue = yValue;
171    
172            this.fillPath = fillPath;
173            this.drawPath = drawPath;
174    
175            this.fillPaint = fillPaint;
176            this.drawPaint = drawPaint;
177            this.drawStroke = drawStroke;
178            this.composite = composite;
179    
180        }
181    
182        /**
183         * Draws the clip path.
184         *
185         * @param g2  current graphics2D.
186         * @param dataArea  the dataArea that the plot is being draw in.
187         * @param horizontalAxis  the horizontal axis.
188         * @param verticalAxis  the vertical axis.
189         *
190         * @return The GeneralPath defining the outline
191         */
192        public GeneralPath draw(Graphics2D g2,
193                                Rectangle2D dataArea,
194                                ValueAxis horizontalAxis, ValueAxis verticalAxis) {
195    
196            GeneralPath generalPath = generateClipPath(
197                dataArea, horizontalAxis, verticalAxis
198            );
199            if (this.fillPath || this.drawPath) {
200                Composite saveComposite = g2.getComposite();
201                Paint savePaint = g2.getPaint();
202                Stroke saveStroke = g2.getStroke();
203    
204                if (this.fillPaint != null) {
205                    g2.setPaint(this.fillPaint);
206                }
207                if (this.composite != null) {
208                    g2.setComposite(this.composite);
209                }
210                if (this.fillPath) {
211                    g2.fill(generalPath);
212                }
213    
214                if (this.drawStroke != null) {
215                    g2.setStroke(this.drawStroke);
216                }
217                if (this.drawPath) {
218                    g2.draw(generalPath);
219                }
220                g2.setPaint(savePaint);
221                g2.setComposite(saveComposite);
222                g2.setStroke(saveStroke);
223            }
224            return generalPath;
225    
226        }
227    
228        /**
229         * Generates the clip path.
230         *
231         * @param dataArea  the dataArea that the plot is being draw in.
232         * @param horizontalAxis  the horizontal axis.
233         * @param verticalAxis  the vertical axis.
234         *
235         * @return The GeneralPath defining the outline
236         */
237        public GeneralPath generateClipPath(Rectangle2D dataArea,
238                                            ValueAxis horizontalAxis, 
239                                            ValueAxis verticalAxis) {
240    
241            GeneralPath generalPath = new GeneralPath();
242            double transX = horizontalAxis.valueToJava2D(
243                this.xValue[0], dataArea, RectangleEdge.BOTTOM
244            );
245            double transY = verticalAxis.valueToJava2D(
246                this.yValue[0], dataArea, RectangleEdge.LEFT
247            );
248            generalPath.moveTo((float) transX, (float) transY);
249            for (int k = 0; k < this.yValue.length; k++) {
250                transX = horizontalAxis.valueToJava2D(
251                    this.xValue[k], dataArea, RectangleEdge.BOTTOM
252                );
253                transY = verticalAxis.valueToJava2D(
254                    this.yValue[k], dataArea, RectangleEdge.LEFT
255                );
256                generalPath.lineTo((float) transX, (float) transY);
257            }
258            generalPath.closePath();
259    
260            return generalPath;
261    
262        }
263    
264        /**
265         * Returns the composite.
266         * 
267         * @return Composite
268         */
269        public Composite getComposite() {
270            return this.composite;
271        }
272    
273        /**
274         * Returns the drawPaint.
275         * 
276         * @return Paint
277         */
278        public Paint getDrawPaint() {
279            return this.drawPaint;
280        }
281    
282        /**
283         * Returns the drawPath.
284         * 
285         * @return boolean
286         */
287        public boolean isDrawPath() {
288            return this.drawPath;
289        }
290    
291        /**
292         * Returns the drawStroke.
293         * 
294         * @return Stroke
295         */
296        public Stroke getDrawStroke() {
297            return this.drawStroke;
298        }
299    
300        /**
301         * Returns the fillPaint.
302         * 
303         * @return Paint
304         */
305        public Paint getFillPaint() {
306            return this.fillPaint;
307        }
308    
309        /**
310         * Returns the fillPath.
311         * 
312         * @return boolean
313         */
314        public boolean isFillPath() {
315            return this.fillPath;
316        }
317    
318        /**
319         * Returns the xValue.
320         * 
321         * @return double[]
322         */
323        public double[] getXValue() {
324            return this.xValue;
325        }
326    
327        /**
328         * Returns the yValue.
329         * 
330         * @return double[]
331         */
332        public double[] getYValue() {
333            return this.yValue;
334        }
335    
336        /**
337         * Sets the composite.
338         * 
339         * @param composite The composite to set
340         */
341        public void setComposite(Composite composite) {
342            this.composite = composite;
343        }
344    
345        /**
346         * Sets the drawPaint.
347         * 
348         * @param drawPaint The drawPaint to set
349         */
350        public void setDrawPaint(Paint drawPaint) {
351            this.drawPaint = drawPaint;
352        }
353    
354        /**
355         * Sets the drawPath.
356         * 
357         * @param drawPath The drawPath to set
358         */
359        public void setDrawPath(boolean drawPath) {
360            this.drawPath = drawPath;
361        }
362    
363        /**
364         * Sets the drawStroke.
365         * 
366         * @param drawStroke The drawStroke to set
367         */
368        public void setDrawStroke(Stroke drawStroke) {
369            this.drawStroke = drawStroke;
370        }
371    
372        /**
373         * Sets the fillPaint.
374         * 
375         * @param fillPaint The fillPaint to set
376         */
377        public void setFillPaint(Paint fillPaint) {
378            this.fillPaint = fillPaint;
379        }
380    
381        /**
382         * Sets the fillPath.
383         * 
384         * @param fillPath The fillPath to set
385         */
386        public void setFillPath(boolean fillPath) {
387            this.fillPath = fillPath;
388        }
389    
390        /**
391         * Sets the xValue.
392         * 
393         * @param xValue The xValue to set
394         */
395        public void setXValue(double[] xValue) {
396            this.xValue = xValue;
397        }
398    
399        /**
400         * Sets the yValue.
401         * 
402         * @param yValue The yValue to set
403         */
404        public void setYValue(double[] yValue) {
405            this.yValue = yValue;
406        }
407    
408        /**
409         * Returns the clip.
410         * 
411         * @return boolean
412         */
413        public boolean isClip() {
414            return this.clip;
415        }
416    
417        /**
418         * Sets the clip.
419         * 
420         * @param clip The clip to set
421         */
422        public void setClip(boolean clip) {
423            this.clip = clip;
424        }
425    
426        /** 
427         * Returns a clone of the object (a deeper clone than default to avoid bugs 
428         * when setting values in cloned object).
429         * 
430         * @return The clone.
431         * 
432         * @throws CloneNotSupportedException if cloning is not supported.
433         */
434        public Object clone() throws CloneNotSupportedException {
435            ClipPath clone = (ClipPath) super.clone();
436            clone.xValue = (double[]) this.xValue.clone();
437            clone.yValue = (double[]) this.yValue.clone();
438            return clone;
439        }
440    
441    }