1 49 50 package org.jfree.chart.annotations; 51 52 import java.awt.BasicStroke ; 53 import java.awt.Color ; 54 import java.awt.Graphics2D ; 55 import java.awt.Paint ; 56 import java.awt.Shape ; 57 import java.awt.Stroke ; 58 import java.awt.geom.AffineTransform ; 59 import java.awt.geom.Rectangle2D ; 60 import java.io.IOException ; 61 import java.io.ObjectInputStream ; 62 import java.io.ObjectOutputStream ; 63 import java.io.Serializable ; 64 65 import org.jfree.chart.axis.ValueAxis; 66 import org.jfree.chart.plot.Plot; 67 import org.jfree.chart.plot.PlotOrientation; 68 import org.jfree.chart.plot.PlotRenderingInfo; 69 import org.jfree.chart.plot.XYPlot; 70 import org.jfree.io.SerialUtilities; 71 import org.jfree.ui.RectangleEdge; 72 import org.jfree.util.ObjectUtilities; 73 import org.jfree.util.PublicCloneable; 74 75 81 public class XYShapeAnnotation extends AbstractXYAnnotation 82 implements Cloneable , PublicCloneable, 83 Serializable { 84 85 86 private static final long serialVersionUID = -8553218317600684041L; 87 88 89 private transient Shape shape; 90 91 92 private transient Stroke stroke; 93 94 95 private transient Paint outlinePaint; 96 97 98 private transient Paint fillPaint; 99 100 106 public XYShapeAnnotation(Shape shape) { 107 this(shape, new BasicStroke (1.0f), Color.black); 108 } 109 110 118 public XYShapeAnnotation(Shape shape, Stroke stroke, Paint outlinePaint) { 119 this(shape, stroke, outlinePaint, null); 120 } 121 122 131 public XYShapeAnnotation(Shape shape, Stroke stroke, Paint outlinePaint, 132 Paint fillPaint) { 133 if (shape == null) { 134 throw new IllegalArgumentException ("Null 'shape' argument."); 135 } 136 this.shape = shape; 137 this.stroke = stroke; 138 this.outlinePaint = outlinePaint; 139 this.fillPaint = fillPaint; 140 } 141 142 154 public void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea, 155 ValueAxis domainAxis, ValueAxis rangeAxis, 156 int rendererIndex, 157 PlotRenderingInfo info) { 158 159 PlotOrientation orientation = plot.getOrientation(); 160 RectangleEdge domainEdge = Plot.resolveDomainAxisLocation( 161 plot.getDomainAxisLocation(), orientation 162 ); 163 RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation( 164 plot.getRangeAxisLocation(), orientation 165 ); 166 167 double m02 = domainAxis.valueToJava2D(0, dataArea, domainEdge); 171 double m12 = rangeAxis.valueToJava2D(0, dataArea, rangeEdge); 173 double m00 = domainAxis.valueToJava2D(1, dataArea, domainEdge) - m02; 175 double m11 = rangeAxis.valueToJava2D(1, dataArea, rangeEdge) - m12; 177 178 Shape s = null; 180 if (orientation == PlotOrientation.HORIZONTAL) { 181 AffineTransform t1 = new AffineTransform ( 182 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f 183 ); 184 AffineTransform t2 = new AffineTransform ( 185 m11, 0.0f, 0.0f, m00, m12, m02 186 ); 187 s = t1.createTransformedShape(this.shape); 188 s = t2.createTransformedShape(s); 189 } 190 else if (orientation == PlotOrientation.VERTICAL) { 191 AffineTransform t = new AffineTransform (m00, 0, 0, m11, m02, m12); 192 s = t.createTransformedShape(this.shape); 193 } 194 195 if (this.fillPaint != null) { 196 g2.setPaint(this.fillPaint); 197 g2.fill(s); 198 } 199 200 if (this.stroke != null && this.outlinePaint != null) { 201 g2.setPaint(this.outlinePaint); 202 g2.setStroke(this.stroke); 203 g2.draw(s); 204 } 205 addEntity(info, s, rendererIndex, getToolTipText(), getURL()); 206 207 } 208 209 216 public boolean equals(Object obj) { 217 if (obj == this) { 218 return true; 219 } 220 if (!super.equals(obj)) { 222 return false; 223 } 224 if (!(obj instanceof XYShapeAnnotation)) { 225 return false; 226 } 227 XYShapeAnnotation that = (XYShapeAnnotation) obj; 228 if (!this.shape.equals(that.shape)) { 229 return false; 230 } 231 if (!ObjectUtilities.equal(this.stroke, that.stroke)) { 232 return false; 233 } 234 if (!ObjectUtilities.equal(this.outlinePaint, that.outlinePaint)) { 235 return false; 236 } 237 if (!ObjectUtilities.equal(this.fillPaint, that.fillPaint)) { 238 return false; 239 } 240 return true; 242 } 243 244 249 public int hashCode() { 250 return this.shape.hashCode(); 252 } 253 254 261 public Object clone() throws CloneNotSupportedException { 262 return super.clone(); 263 } 264 265 272 private void writeObject(ObjectOutputStream stream) throws IOException { 273 stream.defaultWriteObject(); 274 SerialUtilities.writeShape(this.shape, stream); 275 SerialUtilities.writeStroke(this.stroke, stream); 276 SerialUtilities.writePaint(this.outlinePaint, stream); 277 SerialUtilities.writePaint(this.fillPaint, stream); 278 } 279 280 288 private void readObject(ObjectInputStream stream) 289 throws IOException , ClassNotFoundException { 290 stream.defaultReadObject(); 291 this.shape = SerialUtilities.readShape(stream); 292 this.stroke = SerialUtilities.readStroke(stream); 293 this.outlinePaint = SerialUtilities.readPaint(stream); 294 this.fillPaint = SerialUtilities.readPaint(stream); 295 } 296 297 } 298 | Popular Tags |