001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2013, 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 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 * ------------------------
028 * XYPolygonAnnotation.java
029 * ------------------------
030 * (C) Copyright 2005-2013, by Object Refinery Limited and Contributors.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   Peter Kolb (patch 2809117);
034 *
035 * Changes:
036 * --------
037 * 09-Feb-2005 : Version 1 (DG);
038 * 02-Jul-2013 : Use ParamChecks (DG);
039 *
040 */
041
042package org.jfree.chart.annotations;
043
044import java.awt.BasicStroke;
045import java.awt.Color;
046import java.awt.Graphics2D;
047import java.awt.Paint;
048import java.awt.Stroke;
049import java.awt.geom.GeneralPath;
050import java.awt.geom.Rectangle2D;
051import java.io.IOException;
052import java.io.ObjectInputStream;
053import java.io.ObjectOutputStream;
054import java.io.Serializable;
055import java.util.Arrays;
056
057import org.jfree.chart.HashUtilities;
058import org.jfree.chart.axis.ValueAxis;
059import org.jfree.chart.plot.Plot;
060import org.jfree.chart.plot.PlotOrientation;
061import org.jfree.chart.plot.PlotRenderingInfo;
062import org.jfree.chart.plot.XYPlot;
063import org.jfree.chart.util.ParamChecks;
064import org.jfree.io.SerialUtilities;
065import org.jfree.ui.RectangleEdge;
066import org.jfree.util.ObjectUtilities;
067import org.jfree.util.PaintUtilities;
068import org.jfree.util.PublicCloneable;
069
070/**
071 * A polygon annotation that can be placed on an {@link XYPlot}.  The
072 * polygon coordinates are specified in data space.
073 */
074public class XYPolygonAnnotation extends AbstractXYAnnotation
075        implements Cloneable, PublicCloneable, Serializable {
076
077    /** For serialization. */
078    private static final long serialVersionUID = -6984203651995900036L;
079
080    /** The polygon. */
081    private double[] polygon;
082
083    /** The stroke used to draw the box outline. */
084    private transient Stroke stroke;
085
086    /** The paint used to draw the box outline. */
087    private transient Paint outlinePaint;
088
089    /** The paint used to fill the box. */
090    private transient Paint fillPaint;
091
092    /**
093     * Creates a new annotation (where, by default, the polygon is drawn
094     * with a black outline).  The array of polygon coordinates must contain
095     * an even number of coordinates (each pair is an (x, y) location on the
096     * plot) and the last point is automatically joined back to the first point.
097     *
098     * @param polygon  the coordinates of the polygon's vertices
099     *     (<code>null</code> not permitted).
100     */
101    public XYPolygonAnnotation(double[] polygon) {
102        this(polygon, new BasicStroke(1.0f), Color.black);
103    }
104
105    /**
106     * Creates a new annotation where the box is drawn as an outline using
107     * the specified <code>stroke</code> and <code>outlinePaint</code>.
108     * The array of polygon coordinates must contain an even number of
109     * coordinates (each pair is an (x, y) location on the plot) and the last
110     * point is automatically joined back to the first point.
111     *
112     * @param polygon  the coordinates of the polygon's vertices
113     *     (<code>null</code> not permitted).
114     * @param stroke  the shape stroke (<code>null</code> permitted).
115     * @param outlinePaint  the shape color (<code>null</code> permitted).
116     */
117    public XYPolygonAnnotation(double[] polygon,
118                               Stroke stroke, Paint outlinePaint) {
119        this(polygon, stroke, outlinePaint, null);
120    }
121
122    /**
123     * Creates a new annotation.  The array of polygon coordinates must
124     * contain an even number of coordinates (each pair is an (x, y) location
125     * on the plot) and the last point is automatically joined back to the
126     * first point.
127     *
128     * @param polygon  the coordinates of the polygon's vertices
129     *     (<code>null</code> not permitted).
130     * @param stroke  the shape stroke (<code>null</code> permitted).
131     * @param outlinePaint  the shape color (<code>null</code> permitted).
132     * @param fillPaint  the paint used to fill the shape (<code>null</code>
133     *                   permitted).
134     */
135    public XYPolygonAnnotation(double[] polygon, Stroke stroke, 
136            Paint outlinePaint, Paint fillPaint) {
137        super();
138        ParamChecks.nullNotPermitted(polygon, "polygon");
139        if (polygon.length % 2 != 0) {
140            throw new IllegalArgumentException("The 'polygon' array must "
141                    + "contain an even number of items.");
142        }
143        this.polygon = (double[]) polygon.clone();
144        this.stroke = stroke;
145        this.outlinePaint = outlinePaint;
146        this.fillPaint = fillPaint;
147    }
148
149    /**
150     * Returns the coordinates of the polygon's vertices.  The returned array
151     * is a copy, so it is safe to modify without altering the annotation's
152     * state.
153     *
154     * @return The coordinates of the polygon's vertices.
155     *
156     * @since 1.0.2
157     */
158    public double[] getPolygonCoordinates() {
159        return (double[]) this.polygon.clone();
160    }
161
162    /**
163     * Returns the fill paint.
164     *
165     * @return The fill paint (possibly <code>null</code>).
166     *
167     * @since 1.0.2
168     */
169    public Paint getFillPaint() {
170        return this.fillPaint;
171    }
172
173    /**
174     * Returns the outline stroke.
175     *
176     * @return The outline stroke (possibly <code>null</code>).
177     *
178     * @since 1.0.2
179     */
180    public Stroke getOutlineStroke() {
181        return this.stroke;
182    }
183
184    /**
185     * Returns the outline paint.
186     *
187     * @return The outline paint (possibly <code>null</code>).
188     *
189     * @since 1.0.2
190     */
191    public Paint getOutlinePaint() {
192        return this.outlinePaint;
193    }
194
195    /**
196     * Draws the annotation.  This method is usually called by the
197     * {@link XYPlot} class, you shouldn't need to call it directly.
198     *
199     * @param g2  the graphics device.
200     * @param plot  the plot.
201     * @param dataArea  the data area.
202     * @param domainAxis  the domain axis.
203     * @param rangeAxis  the range axis.
204     * @param rendererIndex  the renderer index.
205     * @param info  the plot rendering info.
206     */
207    @Override
208    public void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea,
209                     ValueAxis domainAxis, ValueAxis rangeAxis,
210                     int rendererIndex, PlotRenderingInfo info) {
211
212        // if we don't have at least 2 (x, y) coordinates, just return
213        if (this.polygon.length < 4) {
214            return;
215        }
216        PlotOrientation orientation = plot.getOrientation();
217        RectangleEdge domainEdge = Plot.resolveDomainAxisLocation(
218                plot.getDomainAxisLocation(), orientation);
219        RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation(
220                plot.getRangeAxisLocation(), orientation);
221
222        GeneralPath area = new GeneralPath();
223        double x = domainAxis.valueToJava2D(this.polygon[0], dataArea,
224                domainEdge);
225        double y = rangeAxis.valueToJava2D(this.polygon[1], dataArea,
226                rangeEdge);
227        if (orientation == PlotOrientation.HORIZONTAL) {
228            area.moveTo((float) y, (float) x);
229            for (int i = 2; i < this.polygon.length; i += 2) {
230                x = domainAxis.valueToJava2D(this.polygon[i], dataArea,
231                        domainEdge);
232                y = rangeAxis.valueToJava2D(this.polygon[i + 1], dataArea,
233                        rangeEdge);
234                area.lineTo((float) y, (float) x);
235            }
236            area.closePath();
237        }
238        else if (orientation == PlotOrientation.VERTICAL) {
239            area.moveTo((float) x, (float) y);
240            for (int i = 2; i < this.polygon.length; i += 2) {
241                x = domainAxis.valueToJava2D(this.polygon[i], dataArea,
242                        domainEdge);
243                y = rangeAxis.valueToJava2D(this.polygon[i + 1], dataArea,
244                        rangeEdge);
245                area.lineTo((float) x, (float) y);
246            }
247            area.closePath();
248       }
249
250
251        if (this.fillPaint != null) {
252            g2.setPaint(this.fillPaint);
253            g2.fill(area);
254        }
255
256        if (this.stroke != null && this.outlinePaint != null) {
257            g2.setPaint(this.outlinePaint);
258            g2.setStroke(this.stroke);
259            g2.draw(area);
260        }
261        addEntity(info, area, rendererIndex, getToolTipText(), getURL());
262
263    }
264
265    /**
266     * Tests this annotation for equality with an arbitrary object.
267     *
268     * @param obj  the object (<code>null</code> permitted).
269     *
270     * @return A boolean.
271     */
272    @Override
273    public boolean equals(Object obj) {
274        if (obj == this) {
275            return true;
276        }
277        // now try to reject equality
278        if (!super.equals(obj)) {
279            return false;
280        }
281        if (!(obj instanceof XYPolygonAnnotation)) {
282            return false;
283        }
284        XYPolygonAnnotation that = (XYPolygonAnnotation) obj;
285        if (!Arrays.equals(this.polygon, that.polygon)) {
286            return false;
287        }
288        if (!ObjectUtilities.equal(this.stroke, that.stroke)) {
289            return false;
290        }
291        if (!PaintUtilities.equal(this.outlinePaint, that.outlinePaint)) {
292            return false;
293        }
294        if (!PaintUtilities.equal(this.fillPaint, that.fillPaint)) {
295            return false;
296        }
297        // seem to be the same
298        return true;
299    }
300
301    /**
302     * Returns a hash code for this instance.
303     *
304     * @return A hash code.
305     */
306    @Override
307    public int hashCode() {
308        int result = 193;
309        result = 37 * result + HashUtilities.hashCodeForDoubleArray(
310                this.polygon);
311        result = 37 * result + HashUtilities.hashCodeForPaint(this.fillPaint);
312        result = 37 * result + HashUtilities.hashCodeForPaint(
313                this.outlinePaint);
314        if (this.stroke != null) {
315            result = 37 * result + this.stroke.hashCode();
316        }
317        return result;
318    }
319
320    /**
321     * Returns a clone.
322     *
323     * @return A clone.
324     *
325     * @throws CloneNotSupportedException not thrown by this class, but may be
326     *                                    by subclasses.
327     */
328    @Override
329    public Object clone() throws CloneNotSupportedException {
330        return super.clone();
331    }
332
333    /**
334     * Provides serialization support.
335     *
336     * @param stream  the output stream (<code>null</code> not permitted).
337     *
338     * @throws IOException if there is an I/O error.
339     */
340    private void writeObject(ObjectOutputStream stream) throws IOException {
341        stream.defaultWriteObject();
342        SerialUtilities.writeStroke(this.stroke, stream);
343        SerialUtilities.writePaint(this.outlinePaint, stream);
344        SerialUtilities.writePaint(this.fillPaint, stream);
345    }
346
347    /**
348     * Provides serialization support.
349     *
350     * @param stream  the input stream (<code>null</code> not permitted).
351     *
352     * @throws IOException  if there is an I/O error.
353     * @throws ClassNotFoundException  if there is a classpath problem.
354     */
355    private void readObject(ObjectInputStream stream)
356            throws IOException, ClassNotFoundException {
357        stream.defaultReadObject();
358        this.stroke = SerialUtilities.readStroke(stream);
359        this.outlinePaint = SerialUtilities.readPaint(stream);
360        this.fillPaint = SerialUtilities.readPaint(stream);
361    }
362
363}