001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2011, 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-2009, 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 *
039 */
040
041package org.jfree.chart.annotations;
042
043import java.awt.BasicStroke;
044import java.awt.Color;
045import java.awt.Graphics2D;
046import java.awt.Paint;
047import java.awt.Stroke;
048import java.awt.geom.GeneralPath;
049import java.awt.geom.Rectangle2D;
050import java.io.IOException;
051import java.io.ObjectInputStream;
052import java.io.ObjectOutputStream;
053import java.io.Serializable;
054import java.util.Arrays;
055
056import org.jfree.chart.HashUtilities;
057import org.jfree.chart.axis.ValueAxis;
058import org.jfree.chart.plot.Plot;
059import org.jfree.chart.plot.PlotOrientation;
060import org.jfree.chart.plot.PlotRenderingInfo;
061import org.jfree.chart.plot.XYPlot;
062import org.jfree.io.SerialUtilities;
063import org.jfree.ui.RectangleEdge;
064import org.jfree.util.ObjectUtilities;
065import org.jfree.util.PaintUtilities;
066import org.jfree.util.PublicCloneable;
067
068/**
069 * A polygon annotation that can be placed on an {@link XYPlot}.  The
070 * polygon coordinates are specified in data space.
071 */
072public class XYPolygonAnnotation extends AbstractXYAnnotation
073        implements Cloneable, PublicCloneable, Serializable {
074
075    /** For serialization. */
076    private static final long serialVersionUID = -6984203651995900036L;
077
078    /** The polygon. */
079    private double[] polygon;
080
081    /** The stroke used to draw the box outline. */
082    private transient Stroke stroke;
083
084    /** The paint used to draw the box outline. */
085    private transient Paint outlinePaint;
086
087    /** The paint used to fill the box. */
088    private transient Paint fillPaint;
089
090    /**
091     * Creates a new annotation (where, by default, the polygon is drawn
092     * with a black outline).  The array of polygon coordinates must contain
093     * an even number of coordinates (each pair is an (x, y) location on the
094     * plot) and the last point is automatically joined back to the first point.
095     *
096     * @param polygon  the coordinates of the polygon's vertices
097     *     (<code>null</code> not permitted).
098     */
099    public XYPolygonAnnotation(double[] polygon) {
100        this(polygon, new BasicStroke(1.0f), Color.black);
101    }
102
103    /**
104     * Creates a new annotation where the box is drawn as an outline using
105     * the specified <code>stroke</code> and <code>outlinePaint</code>.
106     * The array of polygon coordinates must contain an even number of
107     * coordinates (each pair is an (x, y) location on the plot) and the last
108     * point is automatically joined back to the first point.
109     *
110     * @param polygon  the coordinates of the polygon's vertices
111     *     (<code>null</code> not permitted).
112     * @param stroke  the shape stroke (<code>null</code> permitted).
113     * @param outlinePaint  the shape color (<code>null</code> permitted).
114     */
115    public XYPolygonAnnotation(double[] polygon,
116                               Stroke stroke, Paint outlinePaint) {
117        this(polygon, stroke, outlinePaint, null);
118    }
119
120    /**
121     * Creates a new annotation.  The array of polygon coordinates must
122     * contain an even number of coordinates (each pair is an (x, y) location
123     * on the plot) and the last point is automatically joined back to the
124     * first point.
125     *
126     * @param polygon  the coordinates of the polygon's vertices
127     *     (<code>null</code> not permitted).
128     * @param stroke  the shape stroke (<code>null</code> permitted).
129     * @param outlinePaint  the shape color (<code>null</code> permitted).
130     * @param fillPaint  the paint used to fill the shape (<code>null</code>
131     *                   permitted).
132     */
133    public XYPolygonAnnotation(double[] polygon,
134                               Stroke stroke,
135                               Paint outlinePaint, Paint fillPaint) {
136        super();
137        if (polygon == null) {
138            throw new IllegalArgumentException("Null 'polygon' argument.");
139        }
140        if (polygon.length % 2 != 0) {
141            throw new IllegalArgumentException("The 'polygon' array must "
142                    + "contain an even number of items.");
143        }
144        this.polygon = (double[]) polygon.clone();
145        this.stroke = stroke;
146        this.outlinePaint = outlinePaint;
147        this.fillPaint = fillPaint;
148    }
149
150    /**
151     * Returns the coordinates of the polygon's vertices.  The returned array
152     * is a copy, so it is safe to modify without altering the annotation's
153     * state.
154     *
155     * @return The coordinates of the polygon's vertices.
156     *
157     * @since 1.0.2
158     */
159    public double[] getPolygonCoordinates() {
160        return (double[]) this.polygon.clone();
161    }
162
163    /**
164     * Returns the fill paint.
165     *
166     * @return The fill paint (possibly <code>null</code>).
167     *
168     * @since 1.0.2
169     */
170    public Paint getFillPaint() {
171        return this.fillPaint;
172    }
173
174    /**
175     * Returns the outline stroke.
176     *
177     * @return The outline stroke (possibly <code>null</code>).
178     *
179     * @since 1.0.2
180     */
181    public Stroke getOutlineStroke() {
182        return this.stroke;
183    }
184
185    /**
186     * Returns the outline paint.
187     *
188     * @return The outline paint (possibly <code>null</code>).
189     *
190     * @since 1.0.2
191     */
192    public Paint getOutlinePaint() {
193        return this.outlinePaint;
194    }
195
196    /**
197     * Draws the annotation.  This method is usually called by the
198     * {@link XYPlot} class, you shouldn't need to call it directly.
199     *
200     * @param g2  the graphics device.
201     * @param plot  the plot.
202     * @param dataArea  the data area.
203     * @param domainAxis  the domain axis.
204     * @param rangeAxis  the range axis.
205     * @param rendererIndex  the renderer index.
206     * @param info  the plot rendering info.
207     */
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    public boolean equals(Object obj) {
273        if (obj == this) {
274            return true;
275        }
276        // now try to reject equality
277        if (!super.equals(obj)) {
278            return false;
279        }
280        if (!(obj instanceof XYPolygonAnnotation)) {
281            return false;
282        }
283        XYPolygonAnnotation that = (XYPolygonAnnotation) obj;
284        if (!Arrays.equals(this.polygon, that.polygon)) {
285            return false;
286        }
287        if (!ObjectUtilities.equal(this.stroke, that.stroke)) {
288            return false;
289        }
290        if (!PaintUtilities.equal(this.outlinePaint, that.outlinePaint)) {
291            return false;
292        }
293        if (!PaintUtilities.equal(this.fillPaint, that.fillPaint)) {
294            return false;
295        }
296        // seem to be the same
297        return true;
298    }
299
300    /**
301     * Returns a hash code for this instance.
302     *
303     * @return A hash code.
304     */
305    public int hashCode() {
306        int result = 193;
307        result = 37 * result + HashUtilities.hashCodeForDoubleArray(
308                this.polygon);
309        result = 37 * result + HashUtilities.hashCodeForPaint(this.fillPaint);
310        result = 37 * result + HashUtilities.hashCodeForPaint(
311                this.outlinePaint);
312        if (this.stroke != null) {
313            result = 37 * result + this.stroke.hashCode();
314        }
315        return result;
316    }
317
318    /**
319     * Returns a clone.
320     *
321     * @return A clone.
322     *
323     * @throws CloneNotSupportedException not thrown by this class, but may be
324     *                                    by subclasses.
325     */
326    public Object clone() throws CloneNotSupportedException {
327        return super.clone();
328    }
329
330    /**
331     * Provides serialization support.
332     *
333     * @param stream  the output stream (<code>null</code> not permitted).
334     *
335     * @throws IOException if there is an I/O error.
336     */
337    private void writeObject(ObjectOutputStream stream) throws IOException {
338        stream.defaultWriteObject();
339        SerialUtilities.writeStroke(this.stroke, stream);
340        SerialUtilities.writePaint(this.outlinePaint, stream);
341        SerialUtilities.writePaint(this.fillPaint, stream);
342    }
343
344    /**
345     * Provides serialization support.
346     *
347     * @param stream  the input stream (<code>null</code> not permitted).
348     *
349     * @throws IOException  if there is an I/O error.
350     * @throws ClassNotFoundException  if there is a classpath problem.
351     */
352    private void readObject(ObjectInputStream stream)
353            throws IOException, ClassNotFoundException {
354        stream.defaultReadObject();
355        this.stroke = SerialUtilities.readStroke(stream);
356        this.outlinePaint = SerialUtilities.readPaint(stream);
357        this.fillPaint = SerialUtilities.readPaint(stream);
358    }
359
360}