001 /* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2005, 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 * XYBoxAnnotation.java 029 * -------------------- 030 * (C) Copyright 2005, by Object Refinery Limited and Contributors. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): -; 034 * 035 * $Id: XYBoxAnnotation.java,v 1.5.2.2 2005/10/25 16:51:15 mungady Exp $ 036 * 037 * Changes: 038 * -------- 039 * 19-Jan-2005 : Version 1 (DG); 040 * 06-Jun-2005 : Fixed equals() method to handle GradientPaint (DG); 041 * 042 */ 043 044 package org.jfree.chart.annotations; 045 046 import java.awt.BasicStroke; 047 import java.awt.Color; 048 import java.awt.Graphics2D; 049 import java.awt.Paint; 050 import java.awt.Stroke; 051 import java.awt.geom.Rectangle2D; 052 import java.io.IOException; 053 import java.io.ObjectInputStream; 054 import java.io.ObjectOutputStream; 055 import java.io.Serializable; 056 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 box annotation that can be placed on an {@link XYPlot}. The 070 * box coordinates are specified in data space. 071 */ 072 public class XYBoxAnnotation extends AbstractXYAnnotation 073 implements Cloneable, 074 PublicCloneable, 075 Serializable { 076 077 /** For serialization. */ 078 private static final long serialVersionUID = 6764703772526757457L; 079 080 /** The lower x-coordinate. */ 081 private double x0; 082 083 /** The lower y-coordinate. */ 084 private double y0; 085 086 /** The upper x-coordinate. */ 087 private double x1; 088 089 /** The upper y-coordinate. */ 090 private double y1; 091 092 /** The stroke used to draw the box outline. */ 093 private transient Stroke stroke; 094 095 /** The paint used to draw the box outline. */ 096 private transient Paint outlinePaint; 097 098 /** The paint used to fill the box. */ 099 private transient Paint fillPaint; 100 101 /** 102 * Creates a new annotation (where, by default, the box is drawn 103 * with a black outline). 104 * 105 * @param x0 the lower x-coordinate of the box (in data space). 106 * @param y0 the lower y-coordinate of the box (in data space). 107 * @param x1 the upper x-coordinate of the box (in data space). 108 * @param y1 the upper y-coordinate of the box (in data space). 109 */ 110 public XYBoxAnnotation(double x0, double y0, double x1, double y1) { 111 this(x0, y0, x1, y1, new BasicStroke(1.0f), Color.black); 112 } 113 114 /** 115 * Creates a new annotation where the box is drawn as an outline using 116 * the specified <code>stroke</code> and <code>outlinePaint</code>. 117 * 118 * @param x0 the lower x-coordinate of the box (in data space). 119 * @param y0 the lower y-coordinate of the box (in data space). 120 * @param x1 the upper x-coordinate of the box (in data space). 121 * @param y1 the upper y-coordinate of the box (in data space). 122 * @param stroke the shape stroke (<code>null</code> not permitted). 123 * @param outlinePaint the shape color (<code>null</code> not permitted). 124 */ 125 public XYBoxAnnotation(double x0, double y0, double x1, double y1, 126 Stroke stroke, Paint outlinePaint) { 127 this(x0, y0, x1, y1, stroke, outlinePaint, null); 128 } 129 130 /** 131 * Creates a new annotation. 132 * 133 * @param x0 the lower x-coordinate of the box (in data space). 134 * @param y0 the lower y-coordinate of the box (in data space). 135 * @param x1 the upper x-coordinate of the box (in data space). 136 * @param y1 the upper y-coordinate of the box (in data space). 137 * @param stroke the shape stroke (<code>null</code> permitted). 138 * @param outlinePaint the shape color (<code>null</code> permitted). 139 * @param fillPaint the paint used to fill the shape (<code>null</code> 140 * permitted). 141 */ 142 public XYBoxAnnotation(double x0, double y0, double x1, double y1, 143 Stroke stroke, Paint outlinePaint, Paint fillPaint) { 144 this.x0 = x0; 145 this.y0 = y0; 146 this.x1 = x1; 147 this.y1 = y1; 148 this.stroke = stroke; 149 this.outlinePaint = outlinePaint; 150 this.fillPaint = fillPaint; 151 } 152 153 /** 154 * Draws the annotation. This method is usually called by the 155 * {@link XYPlot} class, you shouldn't need to call it directly. 156 * 157 * @param g2 the graphics device. 158 * @param plot the plot. 159 * @param dataArea the data area. 160 * @param domainAxis the domain axis. 161 * @param rangeAxis the range axis. 162 * @param rendererIndex the renderer index. 163 * @param info the plot rendering info. 164 */ 165 public void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea, 166 ValueAxis domainAxis, ValueAxis rangeAxis, 167 int rendererIndex, PlotRenderingInfo info) { 168 169 PlotOrientation orientation = plot.getOrientation(); 170 RectangleEdge domainEdge = Plot.resolveDomainAxisLocation( 171 plot.getDomainAxisLocation(), orientation 172 ); 173 RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation( 174 plot.getRangeAxisLocation(), orientation 175 ); 176 177 double transX0 = domainAxis.valueToJava2D( 178 this.x0, dataArea, domainEdge 179 ); 180 double transY0 = rangeAxis.valueToJava2D(this.y0, dataArea, rangeEdge); 181 double transX1 = domainAxis.valueToJava2D( 182 this.x1, dataArea, domainEdge 183 ); 184 double transY1 = rangeAxis.valueToJava2D(this.y1, dataArea, rangeEdge); 185 186 Rectangle2D box = null; 187 if (orientation == PlotOrientation.HORIZONTAL) { 188 box = new Rectangle2D.Double( 189 transY0, transX1, transY1 - transY0, transX0 - transX1 190 ); 191 } 192 else if (orientation == PlotOrientation.VERTICAL) { 193 box = new Rectangle2D.Double( 194 transX0, transY1, transX1 - transX0, transY0 - transY1 195 ); 196 } 197 198 if (this.fillPaint != null) { 199 g2.setPaint(this.fillPaint); 200 g2.fill(box); 201 } 202 203 if (this.stroke != null && this.outlinePaint != null) { 204 g2.setPaint(this.outlinePaint); 205 g2.setStroke(this.stroke); 206 g2.draw(box); 207 } 208 addEntity(info, box, rendererIndex, getToolTipText(), getURL()); 209 210 } 211 212 /** 213 * Tests this annotation for equality with an arbitrary object. 214 * 215 * @param obj the object (<code>null</code> permitted). 216 * 217 * @return A boolean. 218 */ 219 public boolean equals(Object obj) { 220 if (obj == this) { 221 return true; 222 } 223 // now try to reject equality 224 if (!super.equals(obj)) { 225 return false; 226 } 227 if (!(obj instanceof XYBoxAnnotation)) { 228 return false; 229 } 230 XYBoxAnnotation that = (XYBoxAnnotation) obj; 231 if (!(this.x0 == that.x0)) { 232 return false; 233 } 234 if (!(this.y0 == that.y0)) { 235 return false; 236 } 237 if (!(this.x1 == that.x1)) { 238 return false; 239 } 240 if (!(this.y1 == that.y1)) { 241 return false; 242 } 243 if (!ObjectUtilities.equal(this.stroke, that.stroke)) { 244 return false; 245 } 246 if (!PaintUtilities.equal(this.outlinePaint, that.outlinePaint)) { 247 return false; 248 } 249 if (!PaintUtilities.equal(this.fillPaint, that.fillPaint)) { 250 return false; 251 } 252 // seem to be the same 253 return true; 254 } 255 256 /** 257 * Returns a hash code. 258 * 259 * @return A hash code. 260 */ 261 public int hashCode() { 262 int result; 263 long temp; 264 temp = Double.doubleToLongBits(this.x0); 265 result = (int) (temp ^ (temp >>> 32)); 266 temp = Double.doubleToLongBits(this.x1); 267 result = 29 * result + (int) (temp ^ (temp >>> 32)); 268 temp = Double.doubleToLongBits(this.y0); 269 result = 29 * result + (int) (temp ^ (temp >>> 32)); 270 temp = Double.doubleToLongBits(this.y1); 271 result = 29 * result + (int) (temp ^ (temp >>> 32)); 272 return result; 273 } 274 275 /** 276 * Returns a clone. 277 * 278 * @return A clone. 279 * 280 * @throws CloneNotSupportedException not thrown by this class, but may be 281 * by subclasses. 282 */ 283 public Object clone() throws CloneNotSupportedException { 284 return super.clone(); 285 } 286 287 /** 288 * Provides serialization support. 289 * 290 * @param stream the output stream (<code>null</code> not permitted). 291 * 292 * @throws IOException if there is an I/O error. 293 */ 294 private void writeObject(ObjectOutputStream stream) throws IOException { 295 stream.defaultWriteObject(); 296 SerialUtilities.writeStroke(this.stroke, stream); 297 SerialUtilities.writePaint(this.outlinePaint, stream); 298 SerialUtilities.writePaint(this.fillPaint, stream); 299 } 300 301 /** 302 * Provides serialization support. 303 * 304 * @param stream the input stream (<code>null</code> not permitted). 305 * 306 * @throws IOException if there is an I/O error. 307 * @throws ClassNotFoundException if there is a classpath problem. 308 */ 309 private void readObject(ObjectInputStream stream) 310 throws IOException, ClassNotFoundException { 311 312 stream.defaultReadObject(); 313 this.stroke = SerialUtilities.readStroke(stream); 314 this.outlinePaint = SerialUtilities.readPaint(stream); 315 this.fillPaint = SerialUtilities.readPaint(stream); 316 } 317 318 }