1 46 47 package org.jfree.chart.annotations; 48 49 import java.awt.Graphics2D ; 50 import java.awt.Image ; 51 import java.awt.geom.Rectangle2D ; 52 import java.io.IOException ; 53 import java.io.ObjectInputStream ; 54 import java.io.ObjectOutputStream ; 55 import java.io.Serializable ; 56 57 import org.jfree.chart.axis.AxisLocation; 58 import org.jfree.chart.axis.ValueAxis; 59 import org.jfree.chart.plot.Plot; 60 import org.jfree.chart.plot.PlotOrientation; 61 import org.jfree.chart.plot.PlotRenderingInfo; 62 import org.jfree.chart.plot.XYPlot; 63 import org.jfree.ui.RectangleEdge; 64 import org.jfree.util.ObjectUtilities; 65 import org.jfree.util.PublicCloneable; 66 67 73 public class XYImageAnnotation extends AbstractXYAnnotation 74 implements Cloneable , PublicCloneable, 75 Serializable { 76 77 78 private static final long serialVersionUID = -4364694501921559958L; 79 80 81 private double x; 82 83 84 private double y; 85 86 87 private transient Image image; 88 89 97 public XYImageAnnotation(double x, double y, Image image) { 98 if (image == null) { 99 throw new IllegalArgumentException ("Null 'image' argument."); 100 } 101 this.x = x; 102 this.y = y; 103 this.image = image; 104 } 105 106 120 public void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea, 121 ValueAxis domainAxis, ValueAxis rangeAxis, 122 int rendererIndex, 123 PlotRenderingInfo info) { 124 125 PlotOrientation orientation = plot.getOrientation(); 126 AxisLocation domainAxisLocation = plot.getDomainAxisLocation(); 127 AxisLocation rangeAxisLocation = plot.getRangeAxisLocation(); 128 RectangleEdge domainEdge 129 = Plot.resolveDomainAxisLocation(domainAxisLocation, orientation); 130 RectangleEdge rangeEdge 131 = Plot.resolveRangeAxisLocation(rangeAxisLocation, orientation); 132 float j2DX 133 = (float) domainAxis.valueToJava2D(this.x, dataArea, domainEdge); 134 float j2DY 135 = (float) rangeAxis.valueToJava2D(this.y, dataArea, rangeEdge); 136 float xx = 0.0f; 137 float yy = 0.0f; 138 if (orientation == PlotOrientation.HORIZONTAL) { 139 xx = j2DY; 140 yy = j2DX; 141 } 142 else if (orientation == PlotOrientation.VERTICAL) { 143 xx = j2DX; 144 yy = j2DY; 145 } 146 int w = this.image.getWidth(null); 147 int h = this.image.getHeight(null); 148 xx = xx - w / 2.0f; 149 yy = yy - h / 2.0f; 150 g2.drawImage(this.image, (int) xx, (int) yy, null); 151 152 String toolTip = getToolTipText(); 153 String url = getURL(); 154 if (toolTip != null || url != null) { 155 addEntity( 156 info, new Rectangle2D.Float (xx, yy, w, h), rendererIndex, 157 toolTip, url 158 ); 159 } 160 } 161 162 169 public boolean equals(Object obj) { 170 if (obj == this) { 171 return true; 172 } 173 if (!super.equals(obj)) { 175 return false; 176 } 177 if (!(obj instanceof XYImageAnnotation)) { 178 return false; 179 } 180 XYImageAnnotation that = (XYImageAnnotation) obj; 181 if (this.x != that.x) { 182 return false; 183 } 184 if (this.y != that.y) { 185 return false; 186 } 187 if (!ObjectUtilities.equal(this.image, that.image)) { 188 return false; 189 } 190 return true; 192 } 193 194 199 public int hashCode() { 200 return this.image.hashCode(); 201 } 202 203 210 public Object clone() throws CloneNotSupportedException { 211 return super.clone(); 212 } 213 214 221 private void writeObject(ObjectOutputStream stream) throws IOException { 222 stream.defaultWriteObject(); 223 } 225 226 234 private void readObject(ObjectInputStream stream) 235 throws IOException , ClassNotFoundException { 236 stream.defaultReadObject(); 237 } 239 240 241 } 242 | Popular Tags |