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