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