KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > annotations > XYLineAnnotation


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this library; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
24  * in the United States and other countries.]
25  *
26  * ---------------------
27  * XYLineAnnotation.java
28  * ---------------------
29  * (C) Copyright 2003-2005, by Object Refinery Limited.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): -;
33  *
34  * $Id: XYLineAnnotation.java,v 1.7 2005/05/19 15:41:53 mungady Exp $
35  *
36  * Changes:
37  * --------
38  * 02-Apr-2003 : Version 1 (DG);
39  * 19-Aug-2003 : Added equals method, implemented Cloneable, and applied
40  * serialization fixes (DG);
41  * 21-Jan-2004 : Update for renamed method in ValueAxis (DG);
42  * 14-Apr-2004 : Fixed draw() method to handle plot orientation correctly (DG);
43  * 29-Sep-2004 : Added support for tool tips and URLS, now extends
44  * AbstractXYAnnotation (DG);
45  * 04-Oct-2004 : Renamed ShapeUtils --> ShapeUtilities (DG);
46  *
47  */

48
49 package org.jfree.chart.annotations;
50
51 import java.awt.BasicStroke JavaDoc;
52 import java.awt.Color JavaDoc;
53 import java.awt.Graphics2D JavaDoc;
54 import java.awt.Paint JavaDoc;
55 import java.awt.Stroke JavaDoc;
56 import java.awt.geom.Line2D JavaDoc;
57 import java.awt.geom.Rectangle2D JavaDoc;
58 import java.io.IOException JavaDoc;
59 import java.io.ObjectInputStream JavaDoc;
60 import java.io.ObjectOutputStream JavaDoc;
61 import java.io.Serializable JavaDoc;
62
63 import org.jfree.chart.axis.ValueAxis;
64 import org.jfree.chart.plot.Plot;
65 import org.jfree.chart.plot.PlotOrientation;
66 import org.jfree.chart.plot.PlotRenderingInfo;
67 import org.jfree.chart.plot.XYPlot;
68 import org.jfree.io.SerialUtilities;
69 import org.jfree.ui.RectangleEdge;
70 import org.jfree.util.ObjectUtilities;
71 import org.jfree.util.PublicCloneable;
72 import org.jfree.util.ShapeUtilities;
73
74 /**
75  * A simple line annotation that can be placed on an {@link XYPlot}.
76  */

77 public class XYLineAnnotation extends AbstractXYAnnotation
78                               implements Cloneable JavaDoc, PublicCloneable,
79                                          Serializable JavaDoc {
80
81     /** For serialization. */
82     private static final long serialVersionUID = -80535465244091334L;
83     
84     /** The x-coordinate. */
85     private double x1;
86
87     /** The y-coordinate. */
88     private double y1;
89
90     /** The x-coordinate. */
91     private double x2;
92
93     /** The y-coordinate. */
94     private double y2;
95
96     /** The line stroke. */
97     private transient Stroke JavaDoc stroke;
98
99     /** The line color. */
100     private transient Paint JavaDoc 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 JavaDoc(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 JavaDoc stroke, Paint JavaDoc paint) {
130
131         if (stroke == null) {
132             throw new IllegalArgumentException JavaDoc("Null 'stroke' argument.");
133         }
134         if (paint == null) {
135             throw new IllegalArgumentException JavaDoc("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 JavaDoc g2, XYPlot plot, Rectangle2D JavaDoc 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         );
168         RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation(
169             plot.getRangeAxisLocation(), orientation
170         );
171         float j2DX1 = 0.0f;
172         float j2DX2 = 0.0f;
173         float j2DY1 = 0.0f;
174         float j2DY2 = 0.0f;
175         if (orientation == PlotOrientation.VERTICAL) {
176             j2DX1 = (float) domainAxis.valueToJava2D(
177                 this.x1, dataArea, domainEdge
178             );
179             j2DY1 = (float) rangeAxis.valueToJava2D(
180                 this.y1, dataArea, rangeEdge
181             );
182             j2DX2 = (float) domainAxis.valueToJava2D(
183                 this.x2, dataArea, domainEdge
184             );
185             j2DY2 = (float) rangeAxis.valueToJava2D(
186                 this.y2, dataArea, rangeEdge
187             );
188         }
189         else if (orientation == PlotOrientation.HORIZONTAL) {
190             j2DY1 = (float) domainAxis.valueToJava2D(
191                 this.x1, dataArea, domainEdge
192             );
193             j2DX1 = (float) rangeAxis.valueToJava2D(
194                 this.y1, dataArea, rangeEdge
195             );
196             j2DY2 = (float) domainAxis.valueToJava2D(
197                 this.x2, dataArea, domainEdge
198             );
199             j2DX2 = (float) rangeAxis.valueToJava2D(
200                 this.y2, dataArea, rangeEdge
201             );
202         }
203         g2.setPaint(this.paint);
204         g2.setStroke(this.stroke);
205         Line2D JavaDoc line = new Line2D.Float JavaDoc(j2DX1, j2DY1, j2DX2, j2DY2);
206         g2.draw(line);
207
208         String JavaDoc toolTip = getToolTipText();
209         String JavaDoc url = getURL();
210         if (toolTip != null || url != null) {
211             addEntity(
212                 info, ShapeUtilities.createLineRegion(line, 1.0f),
213                 rendererIndex, toolTip, url
214             );
215         }
216     }
217
218     /**
219      * Tests this object for equality with an arbitrary object.
220      *
221      * @param obj the object to test against (<code>null</code> permitted).
222      *
223      * @return <code>true</code> or <code>false</code>.
224      */

225     public boolean equals(Object JavaDoc obj) {
226    
227         if (obj == this) {
228             return true;
229         }
230         if (!super.equals(obj)) {
231             return false;
232         }
233         if (!(obj instanceof XYLineAnnotation)) {
234             return false;
235         }
236         
237         XYLineAnnotation that = (XYLineAnnotation) obj;
238         if (this.x1 != that.x1) {
239             return false;
240         }
241         if (this.y1 != that.y1) {
242             return false;
243         }
244         if (this.x2 != that.x2) {
245             return false;
246         }
247         if (this.y2 != that.y2) {
248             return false;
249         }
250         if (!ObjectUtilities.equal(this.paint, that.paint)) {
251             return false;
252         }
253         if (!ObjectUtilities.equal(this.stroke, that.stroke)) {
254             return false;
255         }
256         // seems to be the same...
257
return true;
258         
259     }
260     
261     /**
262      * Returns a hash code.
263      *
264      * @return A hash code.
265      */

266     public int hashCode() {
267         int result;
268         long temp;
269         temp = Double.doubleToLongBits(this.x1);
270         result = (int) (temp ^ (temp >>> 32));
271         temp = Double.doubleToLongBits(this.x2);
272         result = 29 * result + (int) (temp ^ (temp >>> 32));
273         temp = Double.doubleToLongBits(this.y1);
274         result = 29 * result + (int) (temp ^ (temp >>> 32));
275         temp = Double.doubleToLongBits(this.y2);
276         result = 29 * result + (int) (temp ^ (temp >>> 32));
277         return result;
278     }
279
280     /**
281      * Returns a clone of the annotation.
282      *
283      * @return A clone.
284      *
285      * @throws CloneNotSupportedException if the annotation can't be cloned.
286      */

287     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
288         return super.clone();
289     }
290     
291     /**
292      * Provides serialization support.
293      *
294      * @param stream the output stream.
295      *
296      * @throws IOException if there is an I/O error.
297      */

298     private void writeObject(ObjectOutputStream JavaDoc stream) throws IOException JavaDoc {
299
300         stream.defaultWriteObject();
301         SerialUtilities.writePaint(this.paint, stream);
302         SerialUtilities.writeStroke(this.stroke, stream);
303
304     }
305
306     /**
307      * Provides serialization support.
308      *
309      * @param stream the input stream.
310      *
311      * @throws IOException if there is an I/O error.
312      * @throws ClassNotFoundException if there is a classpath problem.
313      */

314     private void readObject(ObjectInputStream JavaDoc stream)
315         throws IOException JavaDoc, ClassNotFoundException JavaDoc {
316         stream.defaultReadObject();
317         this.paint = SerialUtilities.readPaint(stream);
318         this.stroke = SerialUtilities.readStroke(stream);
319     }
320
321 }
322
Popular Tags