KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > progra > charting > Title


1 /*
2     JOpenChart Java Charting Library and Toolkit
3     Copyright (C) 2001 Sebastian Müller
4     http://jopenchart.sourceforge.net
5
6     This library is free software; you can redistribute it and/or
7     modify it under the terms of the GNU Lesser General Public
8     License as published by the Free Software Foundation; either
9     version 2.1 of the License, or (at your option) any later version.
10
11     This library is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14     Lesser General Public License for more details.
15
16     You should have received a copy of the GNU Lesser General Public
17     License along with this library; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
20     Title.java
21     Created on 21. Juni 2001, 23:53
22  */

23
24 package de.progra.charting;
25
26 import de.progra.charting.render.AbstractRenderer;
27 import de.progra.charting.render.ChartRenderingHints;
28 import java.awt.font.TextLayout JavaDoc;
29 import java.awt.font.FontRenderContext JavaDoc;
30 import java.awt.geom.Rectangle2D JavaDoc;
31 import java.awt.Graphics2D JavaDoc;
32 import java.awt.Dimension JavaDoc;
33 import java.awt.Color JavaDoc;
34 import java.awt.Font JavaDoc;
35
36 /** This class contains the Chart Title. It's also a Renderer
37  * object with some extra properties.
38  *
39  * @author mueller
40  * @version 1.0
41  */

42 public class Title extends AbstractRenderer {
43
44     protected String JavaDoc text = "Chart Title";
45     protected Font JavaDoc font = new Font JavaDoc("Helvetica", Font.PLAIN, 22);
46     
47     /** Creates a new Title with the default settings*/
48     public Title() {
49     }
50     
51     /** Creates a new Title with the given text.
52      * @param text the Title content
53      */

54     public Title(String JavaDoc text) {
55         setText(text);
56     }
57     
58     /** Creates a new Title with the given text and font.
59      * @param text the Title content
60      * @param font the Title format
61      */

62     public Title(String JavaDoc text, Font JavaDoc font) {
63         setText(text);
64         setFont(font);
65     }
66     
67     /** Sets this title's text.
68      * @param text the new text
69      */

70     public void setText(String JavaDoc text) {
71         this.text = text;
72     }
73     
74     /** Returns this title's text.
75      * @return the String object containing the Title's text
76      */

77     public String JavaDoc getText() {
78         return text;
79     }
80     
81     /** Sets the Font that is used to render the title.
82      * @param f Sets the new font.
83      */

84     public void setFont(Font JavaDoc f) {
85         font = f;
86     }
87     
88     /** Returns this title's Font.
89      * @return the Title's font
90      */

91     public Font JavaDoc getFont() {
92         return font;
93     }
94     
95     /** This method is called by the paint method to do the actual painting.
96      * The painting is supposed to start at point (0,0) and the size is
97      * always the same as the preferred size. The paint method performs
98      * the possible scaling.
99      * @param g the <CODE>Graphics2D</CODE> object to paint in
100      */

101     public void paintDefault(Graphics2D JavaDoc g) {
102         g.setColor(Color.black);
103         TextLayout JavaDoc layout = new TextLayout JavaDoc(getText(), getFont(),
104                                            new FontRenderContext JavaDoc(null, true, false));
105         
106         layout.draw(g, 0f, (float)getPreferredSize().getHeight() - layout.getDescent());
107     }
108     
109     /** Returns the preferred size needed for the renderer.
110      * @return a non-null Dimension object
111      */

112     public Dimension JavaDoc getPreferredSize() {
113         Rectangle2D JavaDoc titleBounds =
114             getFont().getStringBounds(getText(),
115                                       new FontRenderContext JavaDoc(null, true, false));
116         
117         return new Dimension JavaDoc((int)titleBounds.getWidth(),
118                              (int)titleBounds.getHeight());
119     }
120 }
121
Popular Tags