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 * XYDataImageAnnotation.java
029 * --------------------------
030 * (C) Copyright 2008-present, by David Gilbert and Contributors.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   Peter Kolb (patch 2809117);
034                     Tracy Hiltbrand (equals/hashCode comply with EqualsVerifier);
035 *
036 */
037
038package org.jfree.chart.annotations;
039
040import java.awt.Graphics2D;
041import java.awt.Image;
042import java.awt.geom.Rectangle2D;
043import java.io.IOException;
044import java.io.ObjectInputStream;
045import java.io.ObjectOutputStream;
046import java.util.Objects;
047
048import org.jfree.chart.axis.AxisLocation;
049import org.jfree.chart.axis.ValueAxis;
050import org.jfree.chart.plot.Plot;
051import org.jfree.chart.plot.PlotOrientation;
052import org.jfree.chart.plot.PlotRenderingInfo;
053import org.jfree.chart.plot.XYPlot;
054import org.jfree.chart.ui.RectangleEdge;
055import org.jfree.chart.util.Args;
056import org.jfree.chart.util.PublicCloneable;
057import org.jfree.data.Range;
058
059/**
060 * An annotation that allows an image to be placed within a rectangle specified
061 * in data coordinates on an {@link XYPlot}.  Note that this annotation
062 * is not currently serializable, so don't use it if you plan on serializing
063 * your chart(s).
064 */
065public class XYDataImageAnnotation extends AbstractXYAnnotation
066        implements Cloneable, PublicCloneable, XYAnnotationBoundsInfo {
067
068    /** The image. */
069    private transient Image image;
070
071    /**
072     * The x-coordinate (in data space).
073     */
074    private double x;
075
076    /**
077     * The y-coordinate (in data space).
078     */
079    private double y;
080
081    /**
082     * The image display area width in data coordinates.
083     */
084    private double w;
085
086    /**
087     * The image display area height in data coordinates.
088     */
089    private double h;
090
091    /**
092     * A flag indicating whether or not the annotation should contribute to
093     * the data range for a plot/renderer.
094     */
095    private boolean includeInDataBounds;
096
097    /**
098     * Creates a new annotation to be displayed within the specified rectangle.
099     *
100     * @param image  the image ({@code null} not permitted).
101     * @param x  the x-coordinate (in data space).
102     * @param y  the y-coordinate (in data space).
103     * @param w  the image display area width.
104     * @param h  the image display area height.
105     */
106    public XYDataImageAnnotation(Image image, double x, double y, double w,
107            double h) {
108        this(image, x, y, w, h, false);
109    }
110
111    /**
112     * Creates a new annotation to be displayed within the specified rectangle.
113     *
114     * @param image  the image ({@code null} not permitted).
115     * @param x  the x-coordinate (in data space).
116     * @param y  the y-coordinate (in data space).
117     * @param w  the image display area width.
118     * @param h  the image display area height.
119     * @param includeInDataBounds  a flag that controls whether or not the
120     *     annotation is included in the data bounds for the axis autoRange.
121     */
122    public XYDataImageAnnotation(Image image, double x, double y, double w,
123            double h, boolean includeInDataBounds) {
124
125        super();
126        Args.nullNotPermitted(image, "image");
127        this.image = image;
128        this.x = x;
129        this.y = y;
130        this.w = w;
131        this.h = h;
132        this.includeInDataBounds = includeInDataBounds;
133    }
134
135    /**
136     * Returns the image for the annotation.
137     *
138     * @return The image.
139     */
140    public Image getImage() {
141        return this.image;
142    }
143
144    /**
145     * Returns the x-coordinate (in data space) for the annotation.
146     *
147     * @return The x-coordinate.
148     */
149    public double getX() {
150        return this.x;
151    }
152
153    /**
154     * Returns the y-coordinate (in data space) for the annotation.
155     *
156     * @return The y-coordinate.
157     */
158    public double getY() {
159        return this.y;
160    }
161
162    /**
163     * Returns the width (in data space) of the data rectangle into which the
164     * image will be drawn.
165     *
166     * @return The width.
167     */
168    public double getWidth() {
169        return this.w;
170    }
171
172    /**
173     * Returns the height (in data space) of the data rectangle into which the
174     * image will be drawn.
175     *
176     * @return The height.
177     */
178    public double getHeight() {
179        return this.h;
180    }
181
182    /**
183     * Returns the flag that controls whether or not the annotation should
184     * contribute to the autoRange for the axis it is plotted against.
185     *
186     * @return A boolean.
187     */
188    @Override
189    public boolean getIncludeInDataBounds() {
190        return this.includeInDataBounds;
191    }
192
193    /**
194     * Returns the x-range for the annotation.
195     *
196     * @return The range.
197     */
198    @Override
199    public Range getXRange() {
200        return new Range(this.x, this.x + this.w);
201    }
202
203    /**
204     * Returns the y-range for the annotation.
205     *
206     * @return The range.
207     */
208    @Override
209    public Range getYRange() {
210        return new Range(this.y, this.y + this.h);
211    }
212
213    /**
214     * Draws the annotation.  This method is called by the drawing code in the
215     * {@link XYPlot} class, you don't normally need to call this method
216     * directly.
217     *
218     * @param g2  the graphics device.
219     * @param plot  the plot.
220     * @param dataArea  the data area.
221     * @param domainAxis  the domain axis.
222     * @param rangeAxis  the range axis.
223     * @param rendererIndex  the renderer index.
224     * @param info  if supplied, this info object will be populated with
225     *              entity information.
226     */
227    @Override
228    public void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea,
229                     ValueAxis domainAxis, ValueAxis rangeAxis,
230                     int rendererIndex,
231                     PlotRenderingInfo info) {
232
233        PlotOrientation orientation = plot.getOrientation();
234        AxisLocation xAxisLocation = plot.getDomainAxisLocation();
235        AxisLocation yAxisLocation = plot.getRangeAxisLocation();
236        RectangleEdge xEdge = Plot.resolveDomainAxisLocation(xAxisLocation,
237                orientation);
238        RectangleEdge yEdge = Plot.resolveRangeAxisLocation(yAxisLocation,
239                orientation);
240        float j2DX0 = (float) domainAxis.valueToJava2D(this.x, dataArea, xEdge);
241        float j2DY0 = (float) rangeAxis.valueToJava2D(this.y, dataArea, yEdge);
242        float j2DX1 = (float) domainAxis.valueToJava2D(this.x + this.w,
243                dataArea, xEdge);
244        float j2DY1 = (float) rangeAxis.valueToJava2D(this.y + this.h,
245                dataArea, yEdge);
246        float xx0 = 0.0f;
247        float yy0 = 0.0f;
248        float xx1 = 0.0f;
249        float yy1 = 0.0f;
250        if (orientation == PlotOrientation.HORIZONTAL) {
251            xx0 = j2DY0;
252            xx1 = j2DY1;
253            yy0 = j2DX0;
254            yy1 = j2DX1;
255        }
256        else if (orientation == PlotOrientation.VERTICAL) {
257            xx0 = j2DX0;
258            xx1 = j2DX1;
259            yy0 = j2DY0;
260            yy1 = j2DY1;
261        }
262        // TODO: rotate the image when drawn with horizontal orientation?
263        g2.drawImage(this.image, (int) xx0, (int) Math.min(yy0, yy1),
264                (int) (xx1 - xx0), (int) Math.abs(yy1 - yy0), null);
265        String toolTip = getToolTipText();
266        String url = getURL();
267        if (toolTip != null || url != null) {
268            addEntity(info, new Rectangle2D.Float(xx0, yy0, (xx1 - xx0),
269                    (yy1 - yy0)), rendererIndex, toolTip, url);
270        }
271    }
272
273    /**
274     * Tests this object for equality with an arbitrary object.
275     *
276     * @param obj  the object ({@code null} permitted).
277     *
278     * @return A boolean.
279     */
280    @Override
281    public boolean equals(Object obj) {
282        if (obj == this) {
283            return true;
284        }
285        if (!(obj instanceof XYDataImageAnnotation)) {
286            return false;
287        }
288        XYDataImageAnnotation that = (XYDataImageAnnotation) obj;
289        if (Double.doubleToLongBits(this.x) != Double.doubleToLongBits(that.x)) {
290            return false;
291        }
292        if (Double.doubleToLongBits(this.y) != Double.doubleToLongBits(that.y)) {
293            return false;
294        }
295        if (Double.doubleToLongBits(this.w) != Double.doubleToLongBits(that.w)) {
296            return false;
297        }
298        if (Double.doubleToLongBits(this.h) != Double.doubleToLongBits(that.h)) {
299            return false;
300        }
301        if (this.includeInDataBounds != that.includeInDataBounds) {
302            return false;
303        }
304        if (!Objects.equals(this.image, that.image)) {
305            return false;
306        }
307
308        // fix the "equals not symmetric" problem
309        if (!that.canEqual(this)) {
310            return false;
311        }
312
313        return super.equals(obj);
314    }
315
316    /**
317     * Ensures symmetry between super/subclass implementations of equals. For
318     * more detail, see http://jqno.nl/equalsverifier/manual/inheritance.
319     *
320     * @param other Object
321     * 
322     * @return true ONLY if the parameter is THIS class type
323     */
324    @Override
325    public boolean canEqual(Object other) {
326        // fix the "equals not symmetric" problem
327        return (other instanceof XYDataImageAnnotation);
328    }
329
330    /**
331     * Returns a hash code for this object.
332     *
333     * @return A hash code.
334     */
335    @Override
336    public int hashCode() {
337        int hash = super.hashCode(); // equals calls superclass, hashCode must also
338        hash = 89 * hash + Objects.hashCode(this.image);
339        hash = 89 * hash + (int) (Double.doubleToLongBits(this.x) ^ 
340                                 (Double.doubleToLongBits(this.x) >>> 32));
341        hash = 89 * hash + (int) (Double.doubleToLongBits(this.y) ^ 
342                                 (Double.doubleToLongBits(this.y) >>> 32));
343        hash = 89 * hash + (int) (Double.doubleToLongBits(this.w) ^ 
344                                 (Double.doubleToLongBits(this.w) >>> 32));
345        hash = 89 * hash + (int) (Double.doubleToLongBits(this.h) ^ 
346                                 (Double.doubleToLongBits(this.h) >>> 32));
347        hash = 89 * hash + (this.includeInDataBounds ? 1 : 0);
348        return hash;
349    }
350
351    /**
352     * Returns a clone of the annotation.
353     *
354     * @return A clone.
355     *
356     * @throws CloneNotSupportedException  if the annotation can't be cloned.
357     */
358    @Override
359    public Object clone() throws CloneNotSupportedException {
360        return super.clone();
361    }
362
363    /**
364     * Provides serialization support.
365     *
366     * @param stream  the output stream.
367     *
368     * @throws IOException  if there is an I/O error.
369     */
370    private void writeObject(ObjectOutputStream stream) throws IOException {
371        stream.defaultWriteObject();
372        // FIXME
373        //SerialUtils.writeImage(this.image, stream);
374    }
375
376    /**
377     * Provides serialization support.
378     *
379     * @param stream  the input stream.
380     *
381     * @throws IOException  if there is an I/O error.
382     * @throws ClassNotFoundException  if there is a classpath problem.
383     */
384    private void readObject(ObjectInputStream stream)
385        throws IOException, ClassNotFoundException {
386        stream.defaultReadObject();
387        // FIXME
388        //this.image = SerialUtils.readImage(stream);
389    }
390
391}