1 48 49 package org.jfree.chart.annotations; 50 51 import java.awt.BasicStroke ; 52 import java.awt.Color ; 53 import java.awt.Graphics2D ; 54 import java.awt.Paint ; 55 import java.awt.Stroke ; 56 import java.awt.geom.Line2D ; 57 import java.awt.geom.Rectangle2D ; 58 import java.io.IOException ; 59 import java.io.ObjectInputStream ; 60 import java.io.ObjectOutputStream ; 61 import java.io.Serializable ; 62 63 import org.jfree.chart.axis.ValueAxis; 64 import org.jfree.chart.plot.Plot; 65 import org.jfree.chart.plot.PlotOrientation; 66 import org.jfree.chart.plot.PlotRenderingInfo; 67 import org.jfree.chart.plot.XYPlot; 68 import org.jfree.io.SerialUtilities; 69 import org.jfree.ui.RectangleEdge; 70 import org.jfree.util.ObjectUtilities; 71 import org.jfree.util.PublicCloneable; 72 import org.jfree.util.ShapeUtilities; 73 74 77 public class XYLineAnnotation extends AbstractXYAnnotation 78 implements Cloneable , PublicCloneable, 79 Serializable { 80 81 82 private static final long serialVersionUID = -80535465244091334L; 83 84 85 private double x1; 86 87 88 private double y1; 89 90 91 private double x2; 92 93 94 private double y2; 95 96 97 private transient Stroke stroke; 98 99 100 private transient Paint paint; 101 102 112 public XYLineAnnotation(double x1, double y1, double x2, double y2) { 113 this(x1, y1, x2, y2, new BasicStroke (1.0f), Color.black); 114 } 115 116 128 public XYLineAnnotation(double x1, double y1, double x2, double y2, 129 Stroke stroke, Paint paint) { 130 131 if (stroke == null) { 132 throw new IllegalArgumentException ("Null 'stroke' argument."); 133 } 134 if (paint == null) { 135 throw new IllegalArgumentException ("Null 'paint' argument."); 136 } 137 this.x1 = x1; 138 this.y1 = y1; 139 this.x2 = x2; 140 this.y2 = y2; 141 this.stroke = stroke; 142 this.paint = paint; 143 144 } 145 146 159 public void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea, 160 ValueAxis domainAxis, ValueAxis rangeAxis, 161 int rendererIndex, 162 PlotRenderingInfo info) { 163 164 PlotOrientation orientation = plot.getOrientation(); 165 RectangleEdge domainEdge = Plot.resolveDomainAxisLocation( 166 plot.getDomainAxisLocation(), orientation 167 ); 168 RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation( 169 plot.getRangeAxisLocation(), orientation 170 ); 171 float j2DX1 = 0.0f; 172 float j2DX2 = 0.0f; 173 float j2DY1 = 0.0f; 174 float j2DY2 = 0.0f; 175 if (orientation == PlotOrientation.VERTICAL) { 176 j2DX1 = (float) domainAxis.valueToJava2D( 177 this.x1, dataArea, domainEdge 178 ); 179 j2DY1 = (float) rangeAxis.valueToJava2D( 180 this.y1, dataArea, rangeEdge 181 ); 182 j2DX2 = (float) domainAxis.valueToJava2D( 183 this.x2, dataArea, domainEdge 184 ); 185 j2DY2 = (float) rangeAxis.valueToJava2D( 186 this.y2, dataArea, rangeEdge 187 ); 188 } 189 else if (orientation == PlotOrientation.HORIZONTAL) { 190 j2DY1 = (float) domainAxis.valueToJava2D( 191 this.x1, dataArea, domainEdge 192 ); 193 j2DX1 = (float) rangeAxis.valueToJava2D( 194 this.y1, dataArea, rangeEdge 195 ); 196 j2DY2 = (float) domainAxis.valueToJava2D( 197 this.x2, dataArea, domainEdge 198 ); 199 j2DX2 = (float) rangeAxis.valueToJava2D( 200 this.y2, dataArea, rangeEdge 201 ); 202 } 203 g2.setPaint(this.paint); 204 g2.setStroke(this.stroke); 205 Line2D line = new Line2D.Float (j2DX1, j2DY1, j2DX2, j2DY2); 206 g2.draw(line); 207 208 String toolTip = getToolTipText(); 209 String url = getURL(); 210 if (toolTip != null || url != null) { 211 addEntity( 212 info, ShapeUtilities.createLineRegion(line, 1.0f), 213 rendererIndex, toolTip, url 214 ); 215 } 216 } 217 218 225 public boolean equals(Object obj) { 226 227 if (obj == this) { 228 return true; 229 } 230 if (!super.equals(obj)) { 231 return false; 232 } 233 if (!(obj instanceof XYLineAnnotation)) { 234 return false; 235 } 236 237 XYLineAnnotation that = (XYLineAnnotation) obj; 238 if (this.x1 != that.x1) { 239 return false; 240 } 241 if (this.y1 != that.y1) { 242 return false; 243 } 244 if (this.x2 != that.x2) { 245 return false; 246 } 247 if (this.y2 != that.y2) { 248 return false; 249 } 250 if (!ObjectUtilities.equal(this.paint, that.paint)) { 251 return false; 252 } 253 if (!ObjectUtilities.equal(this.stroke, that.stroke)) { 254 return false; 255 } 256 return true; 258 259 } 260 261 266 public int hashCode() { 267 int result; 268 long temp; 269 temp = Double.doubleToLongBits(this.x1); 270 result = (int) (temp ^ (temp >>> 32)); 271 temp = Double.doubleToLongBits(this.x2); 272 result = 29 * result + (int) (temp ^ (temp >>> 32)); 273 temp = Double.doubleToLongBits(this.y1); 274 result = 29 * result + (int) (temp ^ (temp >>> 32)); 275 temp = Double.doubleToLongBits(this.y2); 276 result = 29 * result + (int) (temp ^ (temp >>> 32)); 277 return result; 278 } 279 280 287 public Object clone() throws CloneNotSupportedException { 288 return super.clone(); 289 } 290 291 298 private void writeObject(ObjectOutputStream stream) throws IOException { 299 300 stream.defaultWriteObject(); 301 SerialUtilities.writePaint(this.paint, stream); 302 SerialUtilities.writeStroke(this.stroke, stream); 303 304 } 305 306 314 private void readObject(ObjectInputStream stream) 315 throws IOException , ClassNotFoundException { 316 stream.defaultReadObject(); 317 this.paint = SerialUtilities.readPaint(stream); 318 this.stroke = SerialUtilities.readStroke(stream); 319 } 320 321 } 322 | Popular Tags |