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 * XYPolygonAnnotation.java 029 * ------------------------ 030 * (C) Copyright 2005-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 * 09-Feb-2005 : Version 1 (DG); 038 * 039 */ 040 041 package org.jfree.chart.annotations; 042 043 import java.awt.BasicStroke; 044 import java.awt.Color; 045 import java.awt.Graphics2D; 046 import java.awt.Paint; 047 import java.awt.Stroke; 048 import java.awt.geom.GeneralPath; 049 import java.awt.geom.Rectangle2D; 050 import java.io.IOException; 051 import java.io.ObjectInputStream; 052 import java.io.ObjectOutputStream; 053 import java.io.Serializable; 054 import java.util.Arrays; 055 056 import org.jfree.chart.HashUtilities; 057 import org.jfree.chart.axis.ValueAxis; 058 import org.jfree.chart.plot.Plot; 059 import org.jfree.chart.plot.PlotOrientation; 060 import org.jfree.chart.plot.PlotRenderingInfo; 061 import org.jfree.chart.plot.XYPlot; 062 import org.jfree.io.SerialUtilities; 063 import org.jfree.ui.RectangleEdge; 064 import org.jfree.util.ObjectUtilities; 065 import org.jfree.util.PaintUtilities; 066 import org.jfree.util.PublicCloneable; 067 068 /** 069 * A polygon annotation that can be placed on an {@link XYPlot}. The 070 * polygon coordinates are specified in data space. 071 */ 072 public class XYPolygonAnnotation extends AbstractXYAnnotation 073 implements Cloneable, PublicCloneable, Serializable { 074 075 /** For serialization. */ 076 private static final long serialVersionUID = -6984203651995900036L; 077 078 /** The polygon. */ 079 private double[] polygon; 080 081 /** The stroke used to draw the box outline. */ 082 private transient Stroke stroke; 083 084 /** The paint used to draw the box outline. */ 085 private transient Paint outlinePaint; 086 087 /** The paint used to fill the box. */ 088 private transient Paint fillPaint; 089 090 /** 091 * Creates a new annotation (where, by default, the polygon is drawn 092 * with a black outline). The array of polygon coordinates must contain 093 * an even number of coordinates (each pair is an (x, y) location on the 094 * plot) and the last point is automatically joined back to the first point. 095 * 096 * @param polygon the coordinates of the polygon's vertices 097 * (<code>null</code> not permitted). 098 */ 099 public XYPolygonAnnotation(double[] polygon) { 100 this(polygon, new BasicStroke(1.0f), Color.black); 101 } 102 103 /** 104 * Creates a new annotation where the box is drawn as an outline using 105 * the specified <code>stroke</code> and <code>outlinePaint</code>. 106 * The array of polygon coordinates must contain an even number of 107 * coordinates (each pair is an (x, y) location on the plot) and the last 108 * point is automatically joined back to the first point. 109 * 110 * @param polygon the coordinates of the polygon's vertices 111 * (<code>null</code> not permitted). 112 * @param stroke the shape stroke (<code>null</code> permitted). 113 * @param outlinePaint the shape color (<code>null</code> permitted). 114 */ 115 public XYPolygonAnnotation(double[] polygon, 116 Stroke stroke, Paint outlinePaint) { 117 this(polygon, stroke, outlinePaint, null); 118 } 119 120 /** 121 * Creates a new annotation. The array of polygon coordinates must 122 * contain an even number of coordinates (each pair is an (x, y) location 123 * on the plot) and the last point is automatically joined back to the 124 * first point. 125 * 126 * @param polygon the coordinates of the polygon's vertices 127 * (<code>null</code> not permitted). 128 * @param stroke the shape stroke (<code>null</code> permitted). 129 * @param outlinePaint the shape color (<code>null</code> permitted). 130 * @param fillPaint the paint used to fill the shape (<code>null</code> 131 * permitted). 132 */ 133 public XYPolygonAnnotation(double[] polygon, 134 Stroke stroke, 135 Paint outlinePaint, Paint fillPaint) { 136 if (polygon == null) { 137 throw new IllegalArgumentException("Null 'polygon' argument."); 138 } 139 if (polygon.length % 2 != 0) { 140 throw new IllegalArgumentException("The 'polygon' array must " 141 + "contain an even number of items."); 142 } 143 this.polygon = (double[]) polygon.clone(); 144 this.stroke = stroke; 145 this.outlinePaint = outlinePaint; 146 this.fillPaint = fillPaint; 147 } 148 149 /** 150 * Returns the coordinates of the polygon's vertices. The returned array 151 * is a copy, so it is safe to modify without altering the annotation's 152 * state. 153 * 154 * @return The coordinates of the polygon's vertices. 155 * 156 * @since 1.0.2 157 */ 158 public double[] getPolygonCoordinates() { 159 return (double[]) this.polygon.clone(); 160 } 161 162 /** 163 * Returns the fill paint. 164 * 165 * @return The fill paint (possibly <code>null</code>). 166 * 167 * @since 1.0.2 168 */ 169 public Paint getFillPaint() { 170 return this.fillPaint; 171 } 172 173 /** 174 * Returns the outline stroke. 175 * 176 * @return The outline stroke (possibly <code>null</code>). 177 * 178 * @since 1.0.2 179 */ 180 public Stroke getOutlineStroke() { 181 return this.stroke; 182 } 183 184 /** 185 * Returns the outline paint. 186 * 187 * @return The outline paint (possibly <code>null</code>). 188 * 189 * @since 1.0.2 190 */ 191 public Paint getOutlinePaint() { 192 return this.outlinePaint; 193 } 194 195 /** 196 * Draws the annotation. This method is usually called by the 197 * {@link XYPlot} class, you shouldn't need to call it directly. 198 * 199 * @param g2 the graphics device. 200 * @param plot the plot. 201 * @param dataArea the data area. 202 * @param domainAxis the domain axis. 203 * @param rangeAxis the range axis. 204 * @param rendererIndex the renderer index. 205 * @param info the plot rendering info. 206 */ 207 public void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea, 208 ValueAxis domainAxis, ValueAxis rangeAxis, 209 int rendererIndex, PlotRenderingInfo info) { 210 211 // if we don't have at least 2 (x, y) coordinates, just return 212 if (this.polygon.length < 4) { 213 return; 214 } 215 PlotOrientation orientation = plot.getOrientation(); 216 RectangleEdge domainEdge = Plot.resolveDomainAxisLocation( 217 plot.getDomainAxisLocation(), orientation); 218 RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation( 219 plot.getRangeAxisLocation(), orientation); 220 221 GeneralPath area = new GeneralPath(); 222 double x = domainAxis.valueToJava2D(this.polygon[0], dataArea, 223 domainEdge); 224 double y = rangeAxis.valueToJava2D(this.polygon[1], dataArea, 225 rangeEdge); 226 if (orientation == PlotOrientation.HORIZONTAL) { 227 area.moveTo((float) y, (float) x); 228 for (int i = 2; i < this.polygon.length; i += 2) { 229 x = domainAxis.valueToJava2D(this.polygon[i], dataArea, 230 domainEdge); 231 y = rangeAxis.valueToJava2D(this.polygon[i + 1], dataArea, 232 rangeEdge); 233 area.lineTo((float) y, (float) x); 234 } 235 area.closePath(); 236 } 237 else if (orientation == PlotOrientation.VERTICAL) { 238 area.moveTo((float) x, (float) y); 239 for (int i = 2; i < this.polygon.length; i += 2) { 240 x = domainAxis.valueToJava2D(this.polygon[i], dataArea, 241 domainEdge); 242 y = rangeAxis.valueToJava2D(this.polygon[i + 1], dataArea, 243 rangeEdge); 244 area.lineTo((float) x, (float) y); 245 } 246 area.closePath(); 247 } 248 249 250 if (this.fillPaint != null) { 251 g2.setPaint(this.fillPaint); 252 g2.fill(area); 253 } 254 255 if (this.stroke != null && this.outlinePaint != null) { 256 g2.setPaint(this.outlinePaint); 257 g2.setStroke(this.stroke); 258 g2.draw(area); 259 } 260 addEntity(info, area, rendererIndex, getToolTipText(), getURL()); 261 262 } 263 264 /** 265 * Tests this annotation for equality with an arbitrary object. 266 * 267 * @param obj the object (<code>null</code> permitted). 268 * 269 * @return A boolean. 270 */ 271 public boolean equals(Object obj) { 272 if (obj == this) { 273 return true; 274 } 275 // now try to reject equality 276 if (!super.equals(obj)) { 277 return false; 278 } 279 if (!(obj instanceof XYPolygonAnnotation)) { 280 return false; 281 } 282 XYPolygonAnnotation that = (XYPolygonAnnotation) obj; 283 if (!Arrays.equals(this.polygon, that.polygon)) { 284 return false; 285 } 286 if (!ObjectUtilities.equal(this.stroke, that.stroke)) { 287 return false; 288 } 289 if (!PaintUtilities.equal(this.outlinePaint, that.outlinePaint)) { 290 return false; 291 } 292 if (!PaintUtilities.equal(this.fillPaint, that.fillPaint)) { 293 return false; 294 } 295 // seem to be the same 296 return true; 297 } 298 299 /** 300 * Returns a hash code for this instance. 301 * 302 * @return A hash code. 303 */ 304 public int hashCode() { 305 int result = 193; 306 result = 37 * result + HashUtilities.hashCodeForDoubleArray( 307 this.polygon); 308 result = 37 * result + HashUtilities.hashCodeForPaint(this.fillPaint); 309 result = 37 * result + HashUtilities.hashCodeForPaint( 310 this.outlinePaint); 311 if (this.stroke != null) { 312 result = 37 * result + this.stroke.hashCode(); 313 } 314 return result; 315 } 316 317 /** 318 * Returns a clone. 319 * 320 * @return A clone. 321 * 322 * @throws CloneNotSupportedException not thrown by this class, but may be 323 * by subclasses. 324 */ 325 public Object clone() throws CloneNotSupportedException { 326 return super.clone(); 327 } 328 329 /** 330 * Provides serialization support. 331 * 332 * @param stream the output stream (<code>null</code> not permitted). 333 * 334 * @throws IOException if there is an I/O error. 335 */ 336 private void writeObject(ObjectOutputStream stream) throws IOException { 337 stream.defaultWriteObject(); 338 SerialUtilities.writeStroke(this.stroke, stream); 339 SerialUtilities.writePaint(this.outlinePaint, stream); 340 SerialUtilities.writePaint(this.fillPaint, stream); 341 } 342 343 /** 344 * Provides serialization support. 345 * 346 * @param stream the input stream (<code>null</code> not permitted). 347 * 348 * @throws IOException if there is an I/O error. 349 * @throws ClassNotFoundException if there is a classpath problem. 350 */ 351 private void readObject(ObjectInputStream stream) 352 throws IOException, ClassNotFoundException { 353 stream.defaultReadObject(); 354 this.stroke = SerialUtilities.readStroke(stream); 355 this.outlinePaint = SerialUtilities.readPaint(stream); 356 this.fillPaint = SerialUtilities.readPaint(stream); 357 } 358 359 }