1 42 43 package org.jfree.chart.annotations; 44 45 import java.awt.BasicStroke ; 46 import java.awt.Color ; 47 import java.awt.Graphics2D ; 48 import java.awt.Paint ; 49 import java.awt.Stroke ; 50 import java.awt.geom.GeneralPath ; 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 import java.util.Arrays ; 57 58 import org.jfree.chart.HashUtilities; 59 import org.jfree.chart.axis.ValueAxis; 60 import org.jfree.chart.plot.Plot; 61 import org.jfree.chart.plot.PlotOrientation; 62 import org.jfree.chart.plot.PlotRenderingInfo; 63 import org.jfree.chart.plot.XYPlot; 64 import org.jfree.io.SerialUtilities; 65 import org.jfree.ui.RectangleEdge; 66 import org.jfree.util.ObjectUtilities; 67 import org.jfree.util.PaintUtilities; 68 import org.jfree.util.PublicCloneable; 69 70 74 public class XYPolygonAnnotation extends AbstractXYAnnotation 75 implements Cloneable , 76 PublicCloneable, 77 Serializable { 78 79 80 private static final long serialVersionUID = -6984203651995900036L; 81 82 83 private double[] polygon; 84 85 86 private transient Stroke stroke; 87 88 89 private transient Paint outlinePaint; 90 91 92 private transient Paint fillPaint; 93 94 103 public XYPolygonAnnotation(double[] polygon) { 104 this(polygon, new BasicStroke (1.0f), Color.black); 105 } 106 107 119 public XYPolygonAnnotation(double[] polygon, 120 Stroke stroke, Paint outlinePaint) { 121 this(polygon, stroke, outlinePaint, null); 122 } 123 124 137 public XYPolygonAnnotation(double[] polygon, 138 Stroke stroke, 139 Paint outlinePaint, Paint fillPaint) { 140 if (polygon == null) { 141 throw new IllegalArgumentException ("Null 'polygon' argument."); 142 } 143 if (polygon.length % 2 != 0) { 144 throw new IllegalArgumentException ("The 'polygon' array must " + 145 "contain an even number of items."); 146 } 147 this.polygon = (double[]) polygon.clone(); 148 this.stroke = stroke; 149 this.outlinePaint = outlinePaint; 150 this.fillPaint = fillPaint; 151 } 152 153 162 public double[] getPolygonCoordinates() { 163 return (double[]) this.polygon.clone(); 164 } 165 166 173 public Paint getFillPaint() { 174 return this.fillPaint; 175 } 176 177 184 public Stroke getOutlineStroke() { 185 return this.stroke; 186 } 187 188 195 public Paint getOutlinePaint() { 196 return this.outlinePaint; 197 } 198 199 211 public void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea, 212 ValueAxis domainAxis, ValueAxis rangeAxis, 213 int rendererIndex, PlotRenderingInfo info) { 214 215 if (this.polygon.length < 4) { 217 return; 218 } 219 PlotOrientation orientation = plot.getOrientation(); 220 RectangleEdge domainEdge = Plot.resolveDomainAxisLocation( 221 plot.getDomainAxisLocation(), orientation); 222 RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation( 223 plot.getRangeAxisLocation(), orientation); 224 225 GeneralPath area = new GeneralPath (); 226 double x = domainAxis.valueToJava2D(this.polygon[0], dataArea, 227 domainEdge); 228 double y = rangeAxis.valueToJava2D(this.polygon[1], dataArea, 229 rangeEdge); 230 if (orientation == PlotOrientation.HORIZONTAL) { 231 area.moveTo((float) y, (float) x); 232 for (int i = 2; i < this.polygon.length; i += 2) { 233 x = domainAxis.valueToJava2D(this.polygon[i], dataArea, 234 domainEdge); 235 y = rangeAxis.valueToJava2D(this.polygon[i + 1], dataArea, 236 rangeEdge); 237 area.lineTo((float) y, (float) x); 238 } 239 area.closePath(); 240 } 241 else if (orientation == PlotOrientation.VERTICAL) { 242 area.moveTo((float) x, (float) y); 243 for (int i = 2; i < this.polygon.length; i += 2) { 244 x = domainAxis.valueToJava2D(this.polygon[i], dataArea, 245 domainEdge); 246 y = rangeAxis.valueToJava2D(this.polygon[i + 1], dataArea, 247 rangeEdge); 248 area.lineTo((float) x, (float) y); 249 } 250 area.closePath(); 251 } 252 253 254 if (this.fillPaint != null) { 255 g2.setPaint(this.fillPaint); 256 g2.fill(area); 257 } 258 259 if (this.stroke != null && this.outlinePaint != null) { 260 g2.setPaint(this.outlinePaint); 261 g2.setStroke(this.stroke); 262 g2.draw(area); 263 } 264 addEntity(info, area, rendererIndex, getToolTipText(), getURL()); 265 266 } 267 268 275 public boolean equals(Object obj) { 276 if (obj == this) { 277 return true; 278 } 279 if (!super.equals(obj)) { 281 return false; 282 } 283 if (!(obj instanceof XYPolygonAnnotation)) { 284 return false; 285 } 286 XYPolygonAnnotation that = (XYPolygonAnnotation) obj; 287 if (!Arrays.equals(this.polygon, that.polygon)) { 288 return false; 289 } 290 if (!ObjectUtilities.equal(this.stroke, that.stroke)) { 291 return false; 292 } 293 if (!PaintUtilities.equal(this.outlinePaint, that.outlinePaint)) { 294 return false; 295 } 296 if (!PaintUtilities.equal(this.fillPaint, that.fillPaint)) { 297 return false; 298 } 299 return true; 301 } 302 303 308 public int hashCode() { 309 int result = 193; 310 result = 37 * result + HashUtilities.hashCodeForDoubleArray( 311 this.polygon); 312 result = 37 * result + HashUtilities.hashCodeForPaint(fillPaint); 313 result = 37 * result + HashUtilities.hashCodeForPaint(outlinePaint); 314 if (stroke != null) { 315 result = 37 * result + stroke.hashCode(); 316 } 317 return result; 318 } 319 320 328 public Object clone() throws CloneNotSupportedException { 329 return super.clone(); 330 } 331 332 339 private void writeObject(ObjectOutputStream stream) throws IOException { 340 stream.defaultWriteObject(); 341 SerialUtilities.writeStroke(this.stroke, stream); 342 SerialUtilities.writePaint(this.outlinePaint, stream); 343 SerialUtilities.writePaint(this.fillPaint, stream); 344 } 345 346 354 private void readObject(ObjectInputStream stream) 355 throws IOException , ClassNotFoundException { 356 stream.defaultReadObject(); 357 this.stroke = SerialUtilities.readStroke(stream); 358 this.outlinePaint = SerialUtilities.readPaint(stream); 359 this.fillPaint = SerialUtilities.readPaint(stream); 360 } 361 362 } 363 | Popular Tags |