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