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