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