001: /*
002: JOpenChart Java Charting Library and Toolkit
003: Copyright (C) 2001 Sebastian Müller
004: http://jopenchart.sourceforge.net
005:
006: This library is free software; you can redistribute it and/or
007: modify it under the terms of the GNU Lesser General Public
008: License as published by the Free Software Foundation; either
009: version 2.1 of the License, or (at your option) any later version.
010:
011: This library is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public
017: License along with this library; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019:
020: Title.java
021: Created on 21. Juni 2001, 23:53
022: */
023:
024: package de.progra.charting;
025:
026: import de.progra.charting.render.AbstractRenderer;
027: import de.progra.charting.render.ChartRenderingHints;
028: import java.awt.font.TextLayout;
029: import java.awt.font.FontRenderContext;
030: import java.awt.geom.Rectangle2D;
031: import java.awt.Graphics2D;
032: import java.awt.Dimension;
033: import java.awt.Color;
034: import java.awt.Font;
035:
036: /** This class contains the Chart Title. It's also a Renderer
037: * object with some extra properties.
038: *
039: * @author mueller
040: * @version 1.0
041: */
042: public class Title extends AbstractRenderer {
043:
044: protected String text = "Chart Title";
045: protected Font font = new Font("Helvetica", Font.PLAIN, 22);
046:
047: /** Creates a new Title with the default settings*/
048: public Title() {
049: }
050:
051: /** Creates a new Title with the given text.
052: * @param text the Title content
053: */
054: public Title(String text) {
055: setText(text);
056: }
057:
058: /** Creates a new Title with the given text and font.
059: * @param text the Title content
060: * @param font the Title format
061: */
062: public Title(String text, Font font) {
063: setText(text);
064: setFont(font);
065: }
066:
067: /** Sets this title's text.
068: * @param text the new text
069: */
070: public void setText(String text) {
071: this .text = text;
072: }
073:
074: /** Returns this title's text.
075: * @return the String object containing the Title's text
076: */
077: public String getText() {
078: return text;
079: }
080:
081: /** Sets the Font that is used to render the title.
082: * @param f Sets the new font.
083: */
084: public void setFont(Font f) {
085: font = f;
086: }
087:
088: /** Returns this title's Font.
089: * @return the Title's font
090: */
091: public Font getFont() {
092: return font;
093: }
094:
095: /** This method is called by the paint method to do the actual painting.
096: * The painting is supposed to start at point (0,0) and the size is
097: * always the same as the preferred size. The paint method performs
098: * the possible scaling.
099: * @param g the <CODE>Graphics2D</CODE> object to paint in
100: */
101: public void paintDefault(Graphics2D g) {
102: g.setColor(Color.black);
103: TextLayout layout = new TextLayout(getText(), getFont(),
104: new FontRenderContext(null, true, false));
105:
106: layout.draw(g, 0f, (float) getPreferredSize().getHeight()
107: - layout.getDescent());
108: }
109:
110: /** Returns the preferred size needed for the renderer.
111: * @return a non-null Dimension object
112: */
113: public Dimension getPreferredSize() {
114: Rectangle2D titleBounds = getFont().getStringBounds(getText(),
115: new FontRenderContext(null, true, false));
116:
117: return new Dimension((int) titleBounds.getWidth(),
118: (int) titleBounds.getHeight());
119: }
120: }
|