001    /* ===========================================================
002     * JFreeChart : a free chart library for the Java(tm) platform
003     * ===========================================================
004     *
005     * (C) Copyright 2000-2008, 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-2008, 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     * Changes
037     * -------
038     * 22-Apr-2003 : Added standard header (DG);
039     * 09-May-2003 : Added AxisLocation (DG);
040     * 11-Sep-2003 : Implemented Cloneable (NB);
041     * 21-Jan-2004 : Update for renamed method in ValueAxis (DG);
042     * ------------- JFREECHART 1.0.x ---------------------------------------------
043     * 31-Jan-2007 : Deprecated (DG);
044     *
045     */
046    
047    package org.jfree.chart;
048    
049    import java.awt.BasicStroke;
050    import java.awt.Composite;
051    import java.awt.Graphics2D;
052    import java.awt.Paint;
053    import java.awt.Stroke;
054    import java.awt.geom.GeneralPath;
055    import java.awt.geom.Rectangle2D;
056    
057    import org.jfree.chart.axis.ValueAxis;
058    import org.jfree.chart.plot.XYPlot;
059    import org.jfree.chart.renderer.xy.XYBlockRenderer;
060    import org.jfree.ui.RectangleEdge;
061    
062    /**
063     * This class would typically be used with a
064     * {@link org.jfree.chart.plot.ContourPlot}.  It allows the user to define a
065     * <code>GeneralPath</code> curve in plot coordinates.  This curve can then be
066     * used mask off or define regions within the contour plot.  The data must be
067     * sorted.
068     *
069     * @deprecated This class is no longer supported (as of version 1.0.4).  If
070     *     you are creating contour plots, please try to use {@link XYPlot} and
071     *     {@link XYBlockRenderer}.
072     */
073    public class ClipPath implements Cloneable {
074    
075        /** The x values. */
076        private double[] xValue = null;
077    
078        /** The y values. */
079        private double[] yValue = null;
080    
081        /** Controls whether drawing will be clipped (
082         * false would still allow the drawing or filling of path */
083        private boolean clip = true;
084    
085        /** Controls whether the path is drawn as an outline. */
086        private boolean drawPath = false;
087    
088        /** Controls whether the path is filled. */
089        private boolean fillPath = false;
090    
091        /** The fill paint. */
092        private Paint fillPaint = null;
093    
094        /** The draw paint. */
095        private Paint drawPaint = null;
096    
097        /** The draw stroke. */
098        private Stroke drawStroke = null;
099    
100        /** The composite. */
101        private Composite composite = null;
102    
103        /**
104         * Constructor for ClipPath.
105         */
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         */
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         */
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         */
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         */
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         */
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         */
267        public Composite getComposite() {
268            return this.composite;
269        }
270    
271        /**
272         * Returns the drawPaint.
273         *
274         * @return Paint
275         */
276        public Paint getDrawPaint() {
277            return this.drawPaint;
278        }
279    
280        /**
281         * Returns the drawPath.
282         *
283         * @return boolean
284         */
285        public boolean isDrawPath() {
286            return this.drawPath;
287        }
288    
289        /**
290         * Returns the drawStroke.
291         *
292         * @return Stroke
293         */
294        public Stroke getDrawStroke() {
295            return this.drawStroke;
296        }
297    
298        /**
299         * Returns the fillPaint.
300         *
301         * @return Paint
302         */
303        public Paint getFillPaint() {
304            return this.fillPaint;
305        }
306    
307        /**
308         * Returns the fillPath.
309         *
310         * @return boolean
311         */
312        public boolean isFillPath() {
313            return this.fillPath;
314        }
315    
316        /**
317         * Returns the xValue.
318         *
319         * @return double[]
320         */
321        public double[] getXValue() {
322            return this.xValue;
323        }
324    
325        /**
326         * Returns the yValue.
327         *
328         * @return double[]
329         */
330        public double[] getYValue() {
331            return this.yValue;
332        }
333    
334        /**
335         * Sets the composite.
336         *
337         * @param composite The composite to set
338         */
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         */
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         */
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         */
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         */
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         */
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         */
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         */
402        public void setYValue(double[] yValue) {
403            this.yValue = yValue;
404        }
405    
406        /**
407         * Returns the clip.
408         *
409         * @return boolean
410         */
411        public boolean isClip() {
412            return this.clip;
413        }
414    
415        /**
416         * Sets the clip.
417         *
418         * @param clip The clip to set
419         */
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        public Object clone() throws CloneNotSupportedException {
433            ClipPath clone = (ClipPath) super.clone();
434            clone.xValue = (double[]) this.xValue.clone();
435            clone.yValue = (double[]) this.yValue.clone();
436            return clone;
437        }
438    
439    }