KickJava   Java API By Example, From Geeks To Geeks.

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


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  * XYTextAnnotation.java
28  * ---------------------
29  * (C) Copyright 2002-2005, by Object Refinery Limited.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): -;
33  *
34  * $Id: XYTextAnnotation.java,v 1.5 2005/05/19 15:41:53 mungady Exp $
35  *
36  * Changes:
37  * --------
38  * 28-Aug-2002 : Version 1 (DG);
39  * 07-Nov-2002 : Fixed errors reported by Checkstyle (DG);
40  * 13-Jan-2003 : Reviewed Javadocs (DG);
41  * 26-Mar-2003 : Implemented Serializable (DG);
42  * 02-Jul-2003 : Added new text alignment and rotation options (DG);
43  * 19-Aug-2003 : Implemented Cloneable (DG);
44  * 17-Jan-2003 : Added fix for bug 878706, where the annotation is placed
45  * incorrectly for a plot with horizontal orientation (thanks to
46  * Ed Yu for the fix) (DG);
47  * 21-Jan-2004 : Update for renamed method in ValueAxis (DG);
48  *
49  */

50
51 package org.jfree.chart.annotations;
52
53 import java.awt.Color JavaDoc;
54 import java.awt.Font JavaDoc;
55 import java.awt.Graphics2D JavaDoc;
56 import java.awt.Paint JavaDoc;
57 import java.awt.Shape JavaDoc;
58 import java.awt.geom.Rectangle2D JavaDoc;
59 import java.io.IOException JavaDoc;
60 import java.io.ObjectInputStream JavaDoc;
61 import java.io.ObjectOutputStream JavaDoc;
62 import java.io.Serializable JavaDoc;
63
64 import org.jfree.chart.axis.ValueAxis;
65 import org.jfree.chart.plot.Plot;
66 import org.jfree.chart.plot.PlotOrientation;
67 import org.jfree.chart.plot.PlotRenderingInfo;
68 import org.jfree.chart.plot.XYPlot;
69 import org.jfree.io.SerialUtilities;
70 import org.jfree.text.TextUtilities;
71 import org.jfree.ui.RectangleEdge;
72 import org.jfree.ui.TextAnchor;
73 import org.jfree.util.PaintUtilities;
74 import org.jfree.util.PublicCloneable;
75
76 /**
77  * A text annotation that can be placed at a particular (x, y) location on an
78  * {@link XYPlot}.
79  */

80 public class XYTextAnnotation extends AbstractXYAnnotation
81                               implements Cloneable JavaDoc, PublicCloneable,
82                                          Serializable JavaDoc {
83
84     /** For serialization. */
85     private static final long serialVersionUID = -2946063342782506328L;
86     
87     /** The default font. */
88     public static final Font JavaDoc DEFAULT_FONT
89         = new Font JavaDoc("SansSerif", Font.PLAIN, 10);
90
91     /** The default paint. */
92     public static final Paint JavaDoc DEFAULT_PAINT = Color.black;
93     
94     /** The default text anchor. */
95     public static final TextAnchor DEFAULT_TEXT_ANCHOR = TextAnchor.CENTER;
96
97     /** The default rotation anchor. */
98     public static final TextAnchor DEFAULT_ROTATION_ANCHOR = TextAnchor.CENTER;
99     
100     /** The default rotation angle. */
101     public static final double DEFAULT_ROTATION_ANGLE = 0.0;
102
103     /** The text. */
104     private String JavaDoc text;
105
106     /** The font. */
107     private Font JavaDoc font;
108
109     /** The paint. */
110     private transient Paint JavaDoc paint;
111     
112     /** The x-coordinate. */
113     private double x;
114
115     /** The y-coordinate. */
116     private double y;
117
118     /** The text anchor (to be aligned with (x, y)). */
119     private TextAnchor textAnchor;
120     
121     /** The rotation anchor. */
122     private TextAnchor rotationAnchor;
123     
124     /** The rotation angle. */
125     private double rotationAngle;
126     
127     /**
128      * Creates a new annotation to be displayed at the given coordinates. The
129      * coordinates are specified in data space (they will be converted to
130      * Java2D space for display).
131      *
132      * @param text the text (<code>null</code> not permitted).
133      * @param x the x-coordinate (in data space).
134      * @param y the y-coordinate (in data space).
135      */

136     public XYTextAnnotation(String JavaDoc text, double x, double y) {
137         if (text == null) {
138             throw new IllegalArgumentException JavaDoc("Null 'text' argument.");
139         }
140         this.text = text;
141         this.font = DEFAULT_FONT;
142         this.paint = DEFAULT_PAINT;
143         this.x = x;
144         this.y = y;
145         this.textAnchor = DEFAULT_TEXT_ANCHOR;
146         this.rotationAnchor = DEFAULT_ROTATION_ANCHOR;
147         this.rotationAngle = DEFAULT_ROTATION_ANGLE;
148     }
149     
150     /**
151      * Returns the text for the annotation.
152      *
153      * @return The text (never <code>null</code>).
154      */

155     public String JavaDoc getText() {
156         return this.text;
157     }
158
159     /**
160      * Sets the text for the annotation.
161      *
162      * @param text the text (<code>null</code> not permitted).
163      */

164     public void setText(String JavaDoc text) {
165         this.text = text;
166     }
167     
168     /**
169      * Returns the font for the annotation.
170      *
171      * @return The font.
172      */

173     public Font JavaDoc getFont() {
174         return this.font;
175     }
176
177     /**
178      * Sets the font for the annotation.
179      *
180      * @param font the font.
181      */

182     public void setFont(Font JavaDoc font) {
183         this.font = font;
184     }
185     
186     /**
187      * Returns the paint for the annotation.
188      *
189      * @return The paint.
190      */

191     public Paint JavaDoc getPaint() {
192         return this.paint;
193     }
194     
195     /**
196      * Sets the paint for the annotation.
197      *
198      * @param paint the paint.
199      */

200     public void setPaint(Paint JavaDoc paint) {
201         this.paint = paint;
202     }
203
204     /**
205      * Returns the text anchor.
206      *
207      * @return The text anchor.
208      */

209     public TextAnchor getTextAnchor() {
210         return this.textAnchor;
211     }
212     
213     /**
214      * Sets the text anchor (the point on the text bounding rectangle that is
215      * aligned to the (x, y) coordinate of the annotation).
216      *
217      * @param anchor the anchor point.
218      */

219     public void setTextAnchor(TextAnchor anchor) {
220         this.textAnchor = anchor;
221     }
222     
223     /**
224      * Returns the rotation anchor.
225      *
226      * @return The rotation anchor point.
227      */

228     public TextAnchor getRotationAnchor() {
229         return this.rotationAnchor;
230     }
231     
232     /**
233      * Sets the rotation anchor point.
234      *
235      * @param anchor the anchor.
236      */

237     public void setRotationAnchor(TextAnchor anchor) {
238         this.rotationAnchor = anchor;
239     }
240     
241     /**
242      * Returns the rotation angle.
243      *
244      * @return The rotation angle.
245      */

246     public double getRotationAngle() {
247         return this.rotationAngle;
248     }
249     
250     /**
251      * Sets the rotation angle.
252      * <p>
253      * The angle is measured clockwise in radians.
254      *
255      * @param angle the angle (in radians).
256      */

257     public void setRotationAngle(double angle) {
258         this.rotationAngle = angle;
259     }
260     
261     /**
262      * Returns the x coordinate for the text anchor point (measured against the
263      * domain axis).
264      *
265      * @return The x coordinate (in data space).
266      */

267     public double getX() {
268         return this.x;
269     }
270     
271     /**
272      * Sets the x coordinate for the text anchor point (measured against the
273      * domain axis).
274      *
275      * @param x the x coordinate (in data space).
276      */

277     public void setX(double x) {
278         this.x = x;
279     }
280     
281     /**
282      * Returns the y coordinate for the text anchor point (measured against the
283      * range axis).
284      *
285      * @return The y coordinate (in data space).
286      */

287     public double getY() {
288         return this.y;
289     }
290     
291     /**
292      * Sets the y coordinate for the text anchor point (measured against the
293      * range axis).
294      *
295      * @param y the y coordinate.
296      */

297     public void setY(double y) {
298         this.y = y;
299     }
300
301     /**
302      * Draws the annotation.
303      *
304      * @param g2 the graphics device.
305      * @param plot the plot.
306      * @param dataArea the data area.
307      * @param domainAxis the domain axis.
308      * @param rangeAxis the range axis.
309      * @param rendererIndex the renderer index.
310      * @param info an optional info object that will be populated with
311      * entity information.
312      */

313     public void draw(Graphics2D JavaDoc g2, XYPlot plot, Rectangle2D JavaDoc dataArea,
314                      ValueAxis domainAxis, ValueAxis rangeAxis,
315                      int rendererIndex,
316                      PlotRenderingInfo info) {
317
318         PlotOrientation orientation = plot.getOrientation();
319         RectangleEdge domainEdge = Plot.resolveDomainAxisLocation(
320             plot.getDomainAxisLocation(), orientation
321         );
322         RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation(
323             plot.getRangeAxisLocation(), orientation
324         );
325
326         float anchorX = (float) domainAxis.valueToJava2D(
327             this.x, dataArea, domainEdge
328         );
329         float anchorY = (float) rangeAxis.valueToJava2D(
330             this.y, dataArea, rangeEdge
331         );
332
333         if (orientation == PlotOrientation.HORIZONTAL) {
334             float tempAnchor = anchorX;
335             anchorX = anchorY;
336             anchorY = tempAnchor;
337         }
338         
339         g2.setFont(getFont());
340         g2.setPaint(getPaint());
341         TextUtilities.drawRotatedString(
342             getText(),
343             g2,
344             anchorX,
345             anchorY,
346             getTextAnchor(),
347             getRotationAngle(),
348             getRotationAnchor()
349         );
350         Shape JavaDoc hotspot = TextUtilities.calculateRotatedStringBounds(
351             getText(),
352             g2,
353             anchorX,
354             anchorY,
355             getTextAnchor(),
356             getRotationAngle(),
357             getRotationAnchor()
358         );
359         
360         String JavaDoc toolTip = getToolTipText();
361         String JavaDoc url = getURL();
362         if (toolTip != null || url != null) {
363             addEntity(info, hotspot, rendererIndex, toolTip, url);
364         }
365
366     }
367     
368     /**
369      * Tests this annotation for equality with an arbitrary object.
370      *
371      * @param obj the object (<code>null</code> permitted).
372      *
373      * @return A boolean.
374      */

375     public boolean equals(Object JavaDoc obj) {
376         if (obj == this) {
377             return true;
378         }
379         if (!(obj instanceof XYTextAnnotation)) {
380             return false;
381         }
382         if (!super.equals(obj)) {
383             return false;
384         }
385         XYTextAnnotation that = (XYTextAnnotation) obj;
386         if (!this.text.equals(that.text)) {
387             return false;
388         }
389         if (!this.font.equals(that.font)) {
390             return false;
391         }
392         if (!PaintUtilities.equal(this.paint, that.paint)) {
393             return false;
394         }
395         if (!this.rotationAnchor.equals(that.rotationAnchor)) {
396             return false;
397         }
398         if (this.rotationAngle != that.rotationAngle) {
399             return false;
400         }
401         if (!this.textAnchor.equals(that.textAnchor)) {
402             return false;
403         }
404         return true;
405     }
406     
407     /**
408      * Returns a hash code for the object.
409      *
410      * @return A hash code.
411      */

412     public int hashCode() {
413         // TODO: implement this properly.
414
return this.text.hashCode();
415     }
416     
417     /**
418      * Returns a clone of the annotation.
419      *
420      * @return A clone.
421      *
422      * @throws CloneNotSupportedException if the annotation can't be cloned.
423      */

424     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
425         return super.clone();
426     }
427     
428     /**
429      * Provides serialization support.
430      *
431      * @param stream the output stream.
432      *
433      * @throws IOException if there is an I/O error.
434      */

435     private void writeObject(ObjectOutputStream JavaDoc stream) throws IOException JavaDoc {
436         stream.defaultWriteObject();
437         SerialUtilities.writePaint(this.paint, stream);
438     }
439
440     /**
441      * Provides serialization support.
442      *
443      * @param stream the input stream.
444      *
445      * @throws IOException if there is an I/O error.
446      * @throws ClassNotFoundException if there is a classpath problem.
447      */

448     private void readObject(ObjectInputStream JavaDoc stream)
449         throws IOException JavaDoc, ClassNotFoundException JavaDoc {
450         stream.defaultReadObject();
451         this.paint = SerialUtilities.readPaint(stream);
452     }
453
454
455 }
456
Popular Tags