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