    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     * XYPolygonAnnotation.java
    029     * ------------------------
    030     * (C) Copyright 2005, by Object Refinery Limited and Contributors.
    031     *
    032     * Original Author:  David Gilbert (for Object Refinery Limited);
    033     * Contributor(s):   -;
    034     *
    035     * $Id: XYPolygonAnnotation.java,v 1.3.2.1 2005/10/25 16:51:15 mungady Exp $
    036     *
    037     * Changes:
    038     * --------
    039     * 09-Feb-2005 : Version 1 (DG);
    040     *
    041     */
    042
    043    package org.jfree.chart.annotations;
    044
    045    import java.awt.BasicStroke;
    046    import java.awt.Color;
    047    import java.awt.Graphics2D;
    048    import java.awt.Paint;
    049    import java.awt.Stroke;
    050    import java.awt.geom.GeneralPath;
    051    import java.awt.geom.Rectangle2D;
    052    import java.io.IOException;
    053    import java.io.ObjectInputStream;
    054    import java.io.ObjectOutputStream;
    055    import java.io.Serializable;
    056    import java.util.Arrays;
    057
    058    import org.jfree.chart.axis.ValueAxis;
    059    import org.jfree.chart.plot.Plot;
    060    import org.jfree.chart.plot.PlotOrientation;
    061    import org.jfree.chart.plot.PlotRenderingInfo;
    062    import org.jfree.chart.plot.XYPlot;
    063    import org.jfree.io.SerialUtilities;
    064    import org.jfree.ui.RectangleEdge;
    065    import org.jfree.util.ObjectUtilities;
    066    import 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     */
    072    public class XYPolygonAnnotation extends AbstractXYAnnotation
    073                                     implements Cloneable,
    074                                                PublicCloneable,
    075                                                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).
    095         *
    096         * @param polygon  the polygon coordinates.
    097         */
    098        public XYPolygonAnnotation(double[] polygon) {
    099            this(polygon, new BasicStroke(1.0f), Color.black);
    100        }
    101
    102        /**
    103         * Creates a new annotation where the box is drawn as an outline using
    104         * the specified <code>stroke</code> and <code>outlinePaint</code>.
    105         *
    106         * @param polygon  the polygon.
    107         * @param stroke  the shape stroke (<code>null</code> not permitted).
    108         * @param outlinePaint  the shape color (<code>null</code> not permitted).
    109         */
    110        public XYPolygonAnnotation(double[] polygon,
    111                                   Stroke stroke, Paint outlinePaint) {
    112            this(polygon, stroke, outlinePaint, null);
    113        }
    114
    115        /**
    116         * Creates a new annotation.
    117         *
    118         * @param polygon  the polygon.
    119         * @param stroke  the shape stroke (<code>null</code> permitted).
    120         * @param outlinePaint  the shape color (<code>null</code> permitted).
    121         * @param fillPaint  the paint used to fill the shape (<code>null</code>
    122         *                   permitted).
    123         */
    124        public XYPolygonAnnotation(double[] polygon,
    125                                   Stroke stroke,
    126                                   Paint outlinePaint, Paint fillPaint) {
    127            this.polygon = polygon;
    128            this.stroke = stroke;
    129            this.outlinePaint = outlinePaint;
    130            this.fillPaint = fillPaint;
    131        }
    132
    133        /**
    134         * Draws the annotation.  This method is usually called by the
    135         * {@link XYPlot} class, you shouldn't need to call it directly.
    136         *
    137         * @param g2  the graphics device.
    138         * @param plot  the plot.
    139         * @param dataArea  the data area.
    140         * @param domainAxis  the domain axis.
    141         * @param rangeAxis  the range axis.
    142         * @param rendererIndex  the renderer index.
    143         * @param info  the plot rendering info.
    144         */
    145        public void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea,
    146                         ValueAxis domainAxis, ValueAxis rangeAxis,
    147                         int rendererIndex, PlotRenderingInfo info) {
    148
    149            PlotOrientation orientation = plot.getOrientation();
    150            RectangleEdge domainEdge = Plot.resolveDomainAxisLocation(
    151                plot.getDomainAxisLocation(), orientation
    152            );
    153            RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation(
    154                plot.getRangeAxisLocation(), orientation
    155            );
    156
    157            GeneralPath area = new GeneralPath();
    158            double x = domainAxis.valueToJava2D(
    159                this.polygon[0], dataArea, domainEdge
    160            );
    161            double y = rangeAxis.valueToJava2D(
    162                this.polygon[1], dataArea, rangeEdge
    163            );
    164            if (orientation == PlotOrientation.HORIZONTAL) {
    165                area.moveTo((float) y, (float) x);
    166                for (int i = 2; i < this.polygon.length; i += 2) {
    167                    x = domainAxis.valueToJava2D(
    168                        this.polygon[i], dataArea, domainEdge
    169                    );
    170                    y = rangeAxis.valueToJava2D(
    171                        this.polygon[i + 1], dataArea, rangeEdge
    172                    );
    173                    area.lineTo((float) y, (float) x);
    174                }
    175                area.closePath();
    176            }
    177            else if (orientation == PlotOrientation.VERTICAL) {
    178                area.moveTo((float) x, (float) y);
    179                for (int i = 2; i < this.polygon.length; i += 2) {
    180                    x = domainAxis.valueToJava2D(
    181                        this.polygon[i], dataArea, domainEdge
    182                    );
    183                    y = rangeAxis.valueToJava2D(
    184                        this.polygon[i + 1], dataArea, rangeEdge
    185                    );
    186                    area.lineTo((float) x, (float) y);
    187                }
    188                area.closePath();
    189           }
    190
    191
    192            if (this.fillPaint != null) {
    193                g2.setPaint(this.fillPaint);
    194                g2.fill(area);
    195            }
    196
    197            if (this.stroke != null && this.outlinePaint != null) {
    198                g2.setPaint(this.outlinePaint);
    199                g2.setStroke(this.stroke);
    200                g2.draw(area);
    201            }
    202            addEntity(info, area, rendererIndex, getToolTipText(), getURL());
    203
    204        }
    205
    206        /**
    207         * Tests this annotation for equality with an arbitrary object.
    208         *
    209         * @param obj  the object (<code>null</code> permitted).
    210         *
    211         * @return A boolean.
    212         */
    213        public boolean equals(Object obj) {
    214            if (obj == this) {
    215                return true;
    216            }
    217            // now try to reject equality
    218            if (!super.equals(obj)) {
    219                return false;
    220            }
    221            if (!(obj instanceof XYPolygonAnnotation)) {
    222                return false;
    223            }
    224            XYPolygonAnnotation that = (XYPolygonAnnotation) obj;
    225            if (!Arrays.equals(this.polygon, that.polygon)) {
    226                return false;
    227            }
    228            if (!ObjectUtilities.equal(this.stroke, that.stroke)) {
    229                return false;
    230            }
    231            if (!ObjectUtilities.equal(this.outlinePaint, that.outlinePaint)) {
    232                return false;
    233            }
    234            if (!ObjectUtilities.equal(this.fillPaint, that.fillPaint)) {
    235                return false;
    236            }
    237            // seem to be the same
    238            return true;
    239        }
    240
    241        /**
    242         * Returns a clone.
    243         *
    244         * @return A clone.
    245         *
    246         * @throws CloneNotSupportedException not thrown by this class, but may be
    247         *                                    by subclasses.
    248         */
    249        public Object clone() throws CloneNotSupportedException {
    250            return super.clone();
    251        }
    252
    253        /**
    254         * Provides serialization support.
    255         *
    256         * @param stream  the output stream (<code>null</code> not permitted).
    257         *
    258         * @throws IOException if there is an I/O error.
    259         */
    260        private void writeObject(ObjectOutputStream stream) throws IOException {
    261            stream.defaultWriteObject();
    262            SerialUtilities.writeStroke(this.stroke, stream);
    263            SerialUtilities.writePaint(this.outlinePaint, stream);
    264            SerialUtilities.writePaint(this.fillPaint, stream);
    265        }
    266
    267        /**
    268         * Provides serialization support.
    269         *
    270         * @param stream  the input stream (<code>null</code> not permitted).
    271         *
    272         * @throws IOException  if there is an I/O error.
    273         * @throws ClassNotFoundException  if there is a classpath problem.
    274         */
    275        private void readObject(ObjectInputStream stream)
    276            throws IOException, ClassNotFoundException {
    277
    278            stream.defaultReadObject();
    279            this.stroke = SerialUtilities.readStroke(stream);
    280            this.outlinePaint = SerialUtilities.readPaint(stream);
    281            this.fillPaint = SerialUtilities.readPaint(stream);
    282        }
    283
    284    }