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