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 * XYLineAnnotation.java
029 * ---------------------
030 * (C) Copyright 2003-2008, by Object Refinery Limited.
031 *
032 * Original Author: David Gilbert (for Object Refinery Limited);
033 * Contributor(s): -;
034 *
035 * Changes:
036 * --------
037 * 02-Apr-2003 : Version 1 (DG);
038 * 19-Aug-2003 : Added equals method, implemented Cloneable, and applied
039 * serialization fixes (DG);
040 * 21-Jan-2004 : Update for renamed method in ValueAxis (DG);
041 * 14-Apr-2004 : Fixed draw() method to handle plot orientation correctly (DG);
042 * 29-Sep-2004 : Added support for tool tips and URLS, now extends
043 * AbstractXYAnnotation (DG);
044 * 04-Oct-2004 : Renamed ShapeUtils --> ShapeUtilities (DG);
045 * 08-Jun-2005 : Fixed equals() method to handle GradientPaint() (DG);
046 *
047 */
048
049 package org.jfree.chart.annotations;
050
051 import java.awt.BasicStroke;
052 import java.awt.Color;
053 import java.awt.Graphics2D;
054 import java.awt.Paint;
055 import java.awt.Stroke;
056 import java.awt.geom.Line2D;
057 import java.awt.geom.Rectangle2D;
058 import java.io.IOException;
059 import java.io.ObjectInputStream;
060 import java.io.ObjectOutputStream;
061 import java.io.Serializable;
062
063 import org.jfree.chart.axis.ValueAxis;
064 import org.jfree.chart.plot.Plot;
065 import org.jfree.chart.plot.PlotOrientation;
066 import org.jfree.chart.plot.PlotRenderingInfo;
067 import org.jfree.chart.plot.XYPlot;
068 import org.jfree.io.SerialUtilities;
069 import org.jfree.ui.RectangleEdge;
070 import org.jfree.util.ObjectUtilities;
071 import org.jfree.util.PaintUtilities;
072 import org.jfree.util.PublicCloneable;
073 import org.jfree.util.ShapeUtilities;
074
075 /**
076 * A simple line annotation that can be placed on an {@link XYPlot}.
077 */
078 public class XYLineAnnotation extends AbstractXYAnnotation
079 implements Cloneable, PublicCloneable, Serializable {
080
081 /** For serialization. */
082 private static final long serialVersionUID = -80535465244091334L;
083
084 /** The x-coordinate. */
085 private double x1;
086
087 /** The y-coordinate. */
088 private double y1;
089
090 /** The x-coordinate. */
091 private double x2;
092
093 /** The y-coordinate. */
094 private double y2;
095
096 /** The line stroke. */
097 private transient Stroke stroke;
098
099 /** The line color. */
100 private transient Paint paint;
101
102 /**
103 * Creates a new annotation that draws a line from (x1, y1) to (x2, y2)
104 * where the coordinates are measured in data space (that is, against the
105 * plot's axes).
106 *
107 * @param x1 the x-coordinate for the start of the line.
108 * @param y1 the y-coordinate for the start of the line.
109 * @param x2 the x-coordinate for the end of the line.
110 * @param y2 the y-coordinate for the end of the line.
111 */
112 public XYLineAnnotation(double x1, double y1, double x2, double y2) {
113 this(x1, y1, x2, y2, new BasicStroke(1.0f), Color.black);
114 }
115
116 /**
117 * Creates a new annotation that draws a line from (x1, y1) to (x2, y2)
118 * where the coordinates are measured in data space (that is, against the
119 * plot's axes).
120 *
121 * @param x1 the x-coordinate for the start of the line.
122 * @param y1 the y-coordinate for the start of the line.
123 * @param x2 the x-coordinate for the end of the line.
124 * @param y2 the y-coordinate for the end of the line.
125 * @param stroke the line stroke (<code>null</code> not permitted).
126 * @param paint the line color (<code>null</code> not permitted).
127 */
128 public XYLineAnnotation(double x1, double y1, double x2, double y2,
129 Stroke stroke, Paint paint) {
130
131 if (stroke == null) {
132 throw new IllegalArgumentException("Null 'stroke' argument.");
133 }
134 if (paint == null) {
135 throw new IllegalArgumentException("Null 'paint' argument.");
136 }
137 this.x1 = x1;
138 this.y1 = y1;
139 this.x2 = x2;
140 this.y2 = y2;
141 this.stroke = stroke;
142 this.paint = paint;
143
144 }
145
146 /**
147 * Draws the annotation. This method is called by the {@link XYPlot}
148 * class, you won't normally need to call it yourself.
149 *
150 * @param g2 the graphics device.
151 * @param plot the plot.
152 * @param dataArea the data area.
153 * @param domainAxis the domain axis.
154 * @param rangeAxis the range axis.
155 * @param rendererIndex the renderer index.
156 * @param info if supplied, this info object will be populated with
157 * entity information.
158 */
159 public void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea,
160 ValueAxis domainAxis, ValueAxis rangeAxis,
161 int rendererIndex,
162 PlotRenderingInfo info) {
163
164 PlotOrientation orientation = plot.getOrientation();
165 RectangleEdge domainEdge = Plot.resolveDomainAxisLocation(
166 plot.getDomainAxisLocation(), orientation);
167 RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation(
168 plot.getRangeAxisLocation(), orientation);
169 float j2DX1 = 0.0f;
170 float j2DX2 = 0.0f;
171 float j2DY1 = 0.0f;
172 float j2DY2 = 0.0f;
173 if (orientation == PlotOrientation.VERTICAL) {
174 j2DX1 = (float) domainAxis.valueToJava2D(this.x1, dataArea,
175 domainEdge);
176 j2DY1 = (float) rangeAxis.valueToJava2D(this.y1, dataArea,
177 rangeEdge);
178 j2DX2 = (float) domainAxis.valueToJava2D(this.x2, dataArea,
179 domainEdge);
180 j2DY2 = (float) rangeAxis.valueToJava2D(this.y2, dataArea,
181 rangeEdge);
182 }
183 else if (orientation == PlotOrientation.HORIZONTAL) {
184 j2DY1 = (float) domainAxis.valueToJava2D(this.x1, dataArea,
185 domainEdge);
186 j2DX1 = (float) rangeAxis.valueToJava2D(this.y1, dataArea,
187 rangeEdge);
188 j2DY2 = (float) domainAxis.valueToJava2D(this.x2, dataArea,
189 domainEdge);
190 j2DX2 = (float) rangeAxis.valueToJava2D(this.y2, dataArea,
191 rangeEdge);
192 }
193 g2.setPaint(this.paint);
194 g2.setStroke(this.stroke);
195 Line2D line = new Line2D.Float(j2DX1, j2DY1, j2DX2, j2DY2);
196 g2.draw(line);
197
198 String toolTip = getToolTipText();
199 String url = getURL();
200 if (toolTip != null || url != null) {
201 addEntity(info, ShapeUtilities.createLineRegion(line, 1.0f),
202 rendererIndex, toolTip, url);
203 }
204 }
205
206 /**
207 * Tests this object for equality with an arbitrary object.
208 *
209 * @param obj the object to test against (<code>null</code> permitted).
210 *
211 * @return <code>true</code> or <code>false</code>.
212 */
213 public boolean equals(Object obj) {
214 if (obj == this) {
215 return true;
216 }
217 if (!super.equals(obj)) {
218 return false;
219 }
220 if (!(obj instanceof XYLineAnnotation)) {
221 return false;
222 }
223 XYLineAnnotation that = (XYLineAnnotation) obj;
224 if (this.x1 != that.x1) {
225 return false;
226 }
227 if (this.y1 != that.y1) {
228 return false;
229 }
230 if (this.x2 != that.x2) {
231 return false;
232 }
233 if (this.y2 != that.y2) {
234 return false;
235 }
236 if (!PaintUtilities.equal(this.paint, that.paint)) {
237 return false;
238 }
239 if (!ObjectUtilities.equal(this.stroke, that.stroke)) {
240 return false;
241 }
242 // seems to be the same...
243 return true;
244 }
245
246 /**
247 * Returns a hash code.
248 *
249 * @return A hash code.
250 */
251 public int hashCode() {
252 int result;
253 long temp;
254 temp = Double.doubleToLongBits(this.x1);
255 result = (int) (temp ^ (temp >>> 32));
256 temp = Double.doubleToLongBits(this.x2);
257 result = 29 * result + (int) (temp ^ (temp >>> 32));
258 temp = Double.doubleToLongBits(this.y1);
259 result = 29 * result + (int) (temp ^ (temp >>> 32));
260 temp = Double.doubleToLongBits(this.y2);
261 result = 29 * result + (int) (temp ^ (temp >>> 32));
262 return result;
263 }
264
265 /**
266 * Returns a clone of the annotation.
267 *
268 * @return A clone.
269 *
270 * @throws CloneNotSupportedException if the annotation can't be cloned.
271 */
272 public Object clone() throws CloneNotSupportedException {
273 return super.clone();
274 }
275
276 /**
277 * Provides serialization support.
278 *
279 * @param stream the output stream.
280 *
281 * @throws IOException if there is an I/O error.
282 */
283 private void writeObject(ObjectOutputStream stream) throws IOException {
284 stream.defaultWriteObject();
285 SerialUtilities.writePaint(this.paint, stream);
286 SerialUtilities.writeStroke(this.stroke, stream);
287 }
288
289 /**
290 * Provides serialization support.
291 *
292 * @param stream the input stream.
293 *
294 * @throws IOException if there is an I/O error.
295 * @throws ClassNotFoundException if there is a classpath problem.
296 */
297 private void readObject(ObjectInputStream stream)
298 throws IOException, ClassNotFoundException {
299 stream.defaultReadObject();
300 this.paint = SerialUtilities.readPaint(stream);
301 this.stroke = SerialUtilities.readStroke(stream);
302 }
303
304 }