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