001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2020, 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 * XYImageAnnotation.java
029 * ----------------------
030 * (C) Copyright 2003-2020, by Object Refinery Limited and Contributors.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   Mike Harris;
034 *                   Peter Kolb (patch 2809117);
035 *
036 */
037
038package org.jfree.chart.annotations;
039
040import java.awt.Graphics2D;
041import java.awt.Image;
042import java.awt.geom.Point2D;
043import java.awt.geom.Rectangle2D;
044import java.io.IOException;
045import java.io.ObjectInputStream;
046import java.io.ObjectOutputStream;
047import java.io.Serializable;
048
049import org.jfree.chart.axis.AxisLocation;
050import org.jfree.chart.axis.ValueAxis;
051import org.jfree.chart.plot.Plot;
052import org.jfree.chart.plot.PlotOrientation;
053import org.jfree.chart.plot.PlotRenderingInfo;
054import org.jfree.chart.plot.XYPlot;
055import org.jfree.chart.ui.RectangleAnchor;
056import org.jfree.chart.ui.RectangleEdge;
057import org.jfree.chart.util.ObjectUtils;
058import org.jfree.chart.util.Args;
059import org.jfree.chart.util.PublicCloneable;
060
061/**
062 * An annotation that allows an image to be placed at some location on
063 * an {@link XYPlot}.
064 *
065 * TODO:  implement serialization properly (image is not serializable).
066 */
067public class XYImageAnnotation extends AbstractXYAnnotation
068        implements Cloneable, PublicCloneable, Serializable {
069
070    /** For serialization. */
071    private static final long serialVersionUID = -4364694501921559958L;
072
073    /** The x-coordinate (in data space). */
074    private double x;
075
076    /** The y-coordinate (in data space). */
077    private double y;
078
079    /** The image. */
080    private transient Image image;
081
082    /**
083     * The image anchor point.
084     *
085     * @since 1.0.4
086     */
087    private RectangleAnchor anchor;
088
089    /**
090     * Creates a new annotation to be displayed at the specified (x, y)
091     * location.
092     *
093     * @param x  the x-coordinate (in data space).
094     * @param y  the y-coordinate (in data space).
095     * @param image  the image ({@code null} not permitted).
096     */
097    public XYImageAnnotation(double x, double y, Image image) {
098        this(x, y, image, RectangleAnchor.CENTER);
099    }
100
101    /**
102     * Creates a new annotation to be displayed at the specified (x, y)
103     * location.
104     *
105     * @param x  the x-coordinate (in data space).
106     * @param y  the y-coordinate (in data space).
107     * @param image  the image ({@code null} not permitted).
108     * @param anchor  the image anchor ({@code null} not permitted).
109     *
110     * @since 1.0.4
111     */
112    public XYImageAnnotation(double x, double y, Image image,
113            RectangleAnchor anchor) {
114        super();
115        Args.nullNotPermitted(image, "image");
116        Args.nullNotPermitted(anchor, "anchor");
117        this.x = x;
118        this.y = y;
119        this.image = image;
120        this.anchor = anchor;
121    }
122
123    /**
124     * Returns the x-coordinate (in data space) for the annotation.
125     *
126     * @return The x-coordinate.
127     *
128     * @since 1.0.4
129     */
130    public double getX() {
131        return this.x;
132    }
133
134    /**
135     * Returns the y-coordinate (in data space) for the annotation.
136     *
137     * @return The y-coordinate.
138     *
139     * @since 1.0.4
140     */
141    public double getY() {
142        return this.y;
143    }
144
145    /**
146     * Returns the image for the annotation.
147     *
148     * @return The image.
149     *
150     * @since 1.0.4
151     */
152    public Image getImage() {
153        return this.image;
154    }
155
156    /**
157     * Returns the image anchor for the annotation.
158     *
159     * @return The image anchor.
160     *
161     * @since 1.0.4
162     */
163    public RectangleAnchor getImageAnchor() {
164        return this.anchor;
165    }
166
167    /**
168     * Draws the annotation.  This method is called by the drawing code in the
169     * {@link XYPlot} class, you don't normally need to call this method
170     * directly.
171     *
172     * @param g2  the graphics device.
173     * @param plot  the plot.
174     * @param dataArea  the data area.
175     * @param domainAxis  the domain axis.
176     * @param rangeAxis  the range axis.
177     * @param rendererIndex  the renderer index.
178     * @param info  if supplied, this info object will be populated with
179     *              entity information.
180     */
181    @Override
182    public void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea,
183                     ValueAxis domainAxis, ValueAxis rangeAxis,
184                     int rendererIndex,
185                     PlotRenderingInfo info) {
186
187        PlotOrientation orientation = plot.getOrientation();
188        AxisLocation domainAxisLocation = plot.getDomainAxisLocation();
189        AxisLocation rangeAxisLocation = plot.getRangeAxisLocation();
190        RectangleEdge domainEdge
191            = Plot.resolveDomainAxisLocation(domainAxisLocation, orientation);
192        RectangleEdge rangeEdge
193            = Plot.resolveRangeAxisLocation(rangeAxisLocation, orientation);
194        float j2DX
195            = (float) domainAxis.valueToJava2D(this.x, dataArea, domainEdge);
196        float j2DY
197            = (float) rangeAxis.valueToJava2D(this.y, dataArea, rangeEdge);
198        float xx = 0.0f;
199        float yy = 0.0f;
200        if (orientation == PlotOrientation.HORIZONTAL) {
201            xx = j2DY;
202            yy = j2DX;
203        }
204        else if (orientation == PlotOrientation.VERTICAL) {
205            xx = j2DX;
206            yy = j2DY;
207        }
208        int w = this.image.getWidth(null);
209        int h = this.image.getHeight(null);
210
211        Rectangle2D imageRect = new Rectangle2D.Double(0, 0, w, h);
212        Point2D anchorPoint = this.anchor.getAnchorPoint(imageRect);
213        xx = xx - (float) anchorPoint.getX();
214        yy = yy - (float) anchorPoint.getY();
215        g2.drawImage(this.image, (int) xx, (int) yy, null);
216
217        String toolTip = getToolTipText();
218        String url = getURL();
219        if (toolTip != null || url != null) {
220            addEntity(info, new Rectangle2D.Float(xx, yy, w, h), rendererIndex,
221                    toolTip, url);
222        }
223    }
224
225    /**
226     * Tests this object for equality with an arbitrary object.
227     *
228     * @param obj  the object ({@code null} permitted).
229     *
230     * @return A boolean.
231     */
232    @Override
233    public boolean equals(Object obj) {
234        if (obj == this) {
235            return true;
236        }
237        // now try to reject equality...
238        if (!super.equals(obj)) {
239            return false;
240        }
241        if (!(obj instanceof XYImageAnnotation)) {
242            return false;
243        }
244        XYImageAnnotation that = (XYImageAnnotation) obj;
245        if (this.x != that.x) {
246            return false;
247        }
248        if (this.y != that.y) {
249            return false;
250        }
251        if (!ObjectUtils.equal(this.image, that.image)) {
252            return false;
253        }
254        if (!this.anchor.equals(that.anchor)) {
255            return false;
256        }
257        // seems to be the same...
258        return true;
259    }
260
261    /**
262     * Returns a hash code for this object.
263     *
264     * @return A hash code.
265     */
266    @Override
267    public int hashCode() {
268        return this.image.hashCode();
269    }
270
271    /**
272     * Returns a clone of the annotation.
273     *
274     * @return A clone.
275     *
276     * @throws CloneNotSupportedException  if the annotation can't be cloned.
277     */
278    @Override
279    public Object clone() throws CloneNotSupportedException {
280        return super.clone();
281    }
282
283    /**
284     * Provides serialization support.
285     *
286     * @param stream  the output stream.
287     *
288     * @throws IOException  if there is an I/O error.
289     */
290    private void writeObject(ObjectOutputStream stream) throws IOException {
291        stream.defaultWriteObject();
292        //SerialUtils.writeImage(this.image, stream);
293    }
294
295    /**
296     * Provides serialization support.
297     *
298     * @param stream  the input stream.
299     *
300     * @throws IOException  if there is an I/O error.
301     * @throws ClassNotFoundException  if there is a classpath problem.
302     */
303    private void readObject(ObjectInputStream stream)
304        throws IOException, ClassNotFoundException {
305        stream.defaultReadObject();
306        //this.image = SerialUtils.readImage(stream);
307    }
308
309
310}