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