001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-present, by David Gilbert 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 * XYShapeAnnotation.java
029 * ----------------------
030 * (C) Copyright 2003-present, by Ondax, Inc. and Contributors.
031 *
032 * Original Author:  Greg Steckman (for Ondax, Inc.);
033 * Contributor(s):   David Gilbert;
034 *                   Peter Kolb (patch 2809117);
035 *
036 */
037
038package org.jfree.chart.annotations;
039
040import java.awt.BasicStroke;
041import java.awt.Color;
042import java.awt.Graphics2D;
043import java.awt.Paint;
044import java.awt.Shape;
045import java.awt.Stroke;
046import java.awt.geom.AffineTransform;
047import java.awt.geom.Rectangle2D;
048import java.io.IOException;
049import java.io.ObjectInputStream;
050import java.io.ObjectOutputStream;
051import java.io.Serializable;
052import java.util.Objects;
053
054import org.jfree.chart.HashUtils;
055import org.jfree.chart.axis.ValueAxis;
056import org.jfree.chart.plot.Plot;
057import org.jfree.chart.plot.PlotOrientation;
058import org.jfree.chart.plot.PlotRenderingInfo;
059import org.jfree.chart.plot.XYPlot;
060import org.jfree.chart.ui.RectangleEdge;
061import org.jfree.chart.util.PaintUtils;
062import org.jfree.chart.util.Args;
063import org.jfree.chart.util.PublicCloneable;
064import org.jfree.chart.util.SerialUtils;
065
066/**
067 * A simple {@code Shape} annotation that can be placed on an
068 * {@link XYPlot}.  The shape coordinates are specified in data space.
069 */
070public class XYShapeAnnotation extends AbstractXYAnnotation
071        implements Cloneable, PublicCloneable, Serializable {
072
073    /** For serialization. */
074    private static final long serialVersionUID = -8553218317600684041L;
075
076    /** The shape. */
077    private transient Shape shape;
078
079    /** The stroke used to draw the shape's outline. */
080    private transient Stroke stroke;
081
082    /** The paint used to draw the shape's outline. */
083    private transient Paint outlinePaint;
084
085    /** The paint used to fill the shape. */
086    private transient Paint fillPaint;
087
088    /**
089     * Creates a new annotation (where, by default, the shape is drawn
090     * with a black outline).
091     *
092     * @param shape  the shape (coordinates in data space, {@code null}
093     *     not permitted).
094     */
095    public XYShapeAnnotation(Shape shape) {
096        this(shape, new BasicStroke(1.0f), Color.BLACK);
097    }
098
099    /**
100     * Creates a new annotation where the shape is drawn as an outline using
101     * the specified {@code stroke} and {@code outlinePaint}.
102     *
103     * @param shape  the shape ({@code null} not permitted).
104     * @param stroke  the shape stroke ({@code null} permitted).
105     * @param outlinePaint  the shape color ({@code null} permitted).
106     */
107    public XYShapeAnnotation(Shape shape, Stroke stroke, Paint outlinePaint) {
108        this(shape, stroke, outlinePaint, null);
109    }
110
111    /**
112     * Creates a new annotation.
113     *
114     * @param shape  the shape ({@code null} not permitted).
115     * @param stroke  the shape stroke ({@code null} permitted).
116     * @param outlinePaint  the shape color ({@code null} permitted).
117     * @param fillPaint  the paint used to fill the shape ({@code null}
118     *                   permitted.
119     */
120    public XYShapeAnnotation(Shape shape, Stroke stroke, Paint outlinePaint,
121                             Paint fillPaint) {
122        super();
123        Args.nullNotPermitted(shape, "shape");
124        this.shape = shape;
125        this.stroke = stroke;
126        this.outlinePaint = outlinePaint;
127        this.fillPaint = fillPaint;
128    }
129
130    /**
131     * Draws the annotation.  This method is usually called by the
132     * {@link XYPlot} class, you shouldn't need to call it directly.
133     *
134     * @param g2  the graphics device.
135     * @param plot  the plot.
136     * @param dataArea  the data area.
137     * @param domainAxis  the domain axis.
138     * @param rangeAxis  the range axis.
139     * @param rendererIndex  the renderer index.
140     * @param info  the plot rendering info.
141     */
142    @Override
143    public void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea,
144                     ValueAxis domainAxis, ValueAxis rangeAxis,
145                     int rendererIndex,
146                     PlotRenderingInfo info) {
147
148        PlotOrientation orientation = plot.getOrientation();
149        RectangleEdge domainEdge = Plot.resolveDomainAxisLocation(
150                plot.getDomainAxisLocation(), orientation);
151        RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation(
152                plot.getRangeAxisLocation(), orientation);
153
154        // compute transform matrix elements via sample points. Assume no
155        // rotation or shear.
156        Rectangle2D bounds = this.shape.getBounds2D();
157        double x0 = bounds.getMinX();
158        double x1 = bounds.getMaxX();
159        double xx0 = domainAxis.valueToJava2D(x0, dataArea, domainEdge);
160        double xx1 = domainAxis.valueToJava2D(x1, dataArea, domainEdge);
161        double m00 = (xx1 - xx0) / (x1 - x0);
162        double m02 = xx0 - x0 * m00;
163
164        double y0 = bounds.getMaxY();
165        double y1 = bounds.getMinY();
166        double yy0 = rangeAxis.valueToJava2D(y0, dataArea, rangeEdge);
167        double yy1 = rangeAxis.valueToJava2D(y1, dataArea, rangeEdge);
168        double m11 = (yy1 - yy0) / (y1 - y0);
169        double m12 = yy0 - m11 * y0;
170
171        //  create transform & transform shape
172        Shape s = null;
173        if (orientation == PlotOrientation.HORIZONTAL) {
174            AffineTransform t1 = new AffineTransform(0.0f, 1.0f, 1.0f, 0.0f,
175                    0.0f, 0.0f);
176            AffineTransform t2 = new AffineTransform(m11, 0.0f, 0.0f, m00,
177                    m12, m02);
178            s = t1.createTransformedShape(this.shape);
179            s = t2.createTransformedShape(s);
180        }
181        else if (orientation == PlotOrientation.VERTICAL) {
182            AffineTransform t = new AffineTransform(m00, 0, 0, m11, m02, m12);
183            s = t.createTransformedShape(this.shape);
184        }
185
186        if (this.fillPaint != null) {
187            g2.setPaint(this.fillPaint);
188            g2.fill(s);
189        }
190
191        if (this.stroke != null && this.outlinePaint != null) {
192            g2.setPaint(this.outlinePaint);
193            g2.setStroke(this.stroke);
194            g2.draw(s);
195        }
196        addEntity(info, s, rendererIndex, getToolTipText(), getURL());
197
198    }
199
200    /**
201     * Tests this annotation for equality with an arbitrary object.
202     *
203     * @param obj  the object ({@code null} permitted).
204     *
205     * @return A boolean.
206     */
207    @Override
208    public boolean equals(Object obj) {
209        if (obj == this) {
210            return true;
211        }
212        // now try to reject equality
213        if (!super.equals(obj)) {
214            return false;
215        }
216        if (!(obj instanceof XYShapeAnnotation)) {
217            return false;
218        }
219        XYShapeAnnotation that = (XYShapeAnnotation) obj;
220        if (!this.shape.equals(that.shape)) {
221            return false;
222        }
223        if (!Objects.equals(this.stroke, that.stroke)) {
224            return false;
225        }
226        if (!PaintUtils.equal(this.outlinePaint, that.outlinePaint)) {
227            return false;
228        }
229        if (!PaintUtils.equal(this.fillPaint, that.fillPaint)) {
230            return false;
231        }
232        // seem to be the same
233        return true;
234    }
235
236    /**
237     * Returns a hash code for this instance.
238     *
239     * @return A hash code.
240     */
241    @Override
242    public int hashCode() {
243        int result = 193;
244        result = 37 * result + this.shape.hashCode();
245        if (this.stroke != null) {
246            result = 37 * result + this.stroke.hashCode();
247        }
248        result = 37 * result + HashUtils.hashCodeForPaint(
249                this.outlinePaint);
250        result = 37 * result + HashUtils.hashCodeForPaint(this.fillPaint);
251        return result;
252    }
253
254    /**
255     * Returns a clone.
256     *
257     * @return A clone.
258     *
259     * @throws CloneNotSupportedException ???.
260     */
261    @Override
262    public Object clone() throws CloneNotSupportedException {
263        return super.clone();
264    }
265
266    /**
267     * Provides serialization support.
268     *
269     * @param stream  the output stream.
270     *
271     * @throws IOException if there is an I/O error.
272     */
273    private void writeObject(ObjectOutputStream stream) throws IOException {
274        stream.defaultWriteObject();
275        SerialUtils.writeShape(this.shape, stream);
276        SerialUtils.writeStroke(this.stroke, stream);
277        SerialUtils.writePaint(this.outlinePaint, stream);
278        SerialUtils.writePaint(this.fillPaint, stream);
279    }
280
281    /**
282     * Provides serialization support.
283     *
284     * @param stream  the input stream.
285     *
286     * @throws IOException  if there is an I/O error.
287     * @throws ClassNotFoundException  if there is a classpath problem.
288     */
289    private void readObject(ObjectInputStream stream)
290        throws IOException, ClassNotFoundException {
291        stream.defaultReadObject();
292        this.shape = SerialUtils.readShape(stream);
293        this.stroke = SerialUtils.readStroke(stream);
294        this.outlinePaint = SerialUtils.readPaint(stream);
295        this.fillPaint = SerialUtils.readPaint(stream);
296    }
297
298}