KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtriver > open > vectorvisuals > VisualTextObject


1 /*
2  * VisualTextObject.java
3  *
4  * Created on 4 June 2003, 11:30
5  */

6
7 package com.thoughtriver.open.vectorvisuals;
8
9 import java.awt.*;
10 import java.awt.font.*;
11 import java.awt.geom.*;
12
13 /**
14  * Instances of this class can be used to manipulate text in the form of a
15  * <CODE>VisualObject</CODE> with a high degree of detail. <CODE>GlyphVector</CODE>s
16  * are used to perform the rendering, and since these are <CODE>Shape</CODE>
17  * implementations, the text rendering has all of the flexibility conferred by
18  * being a <CODE>VisualObject</CODE>, such as different brushes for outline
19  * and fill.
20  *
21  * @author Brandon Franklin
22  * @version $Date: 2006/11/25 09:18:36 $
23  */

24 public class VisualTextObject extends VisualObject {
25
26     /** The <CODE>FontRenderContext</CODE> used when rendering the text */
27     private static final FontRenderContext context = new FontRenderContext(new AffineTransform(), true, false);
28
29     /** The text being displayed by this object */
30     private String JavaDoc text = null;
31
32     /** The <CODE>Font</CODE> used when rendering the text */
33     private Font font = null;
34
35     /** The <CODE>GlyphVector</CODE> derived from the font and text */
36     private GlyphVector glyphVector = null;
37
38     /**
39      * Creates a new instance of <CODE>VisualTextObject</CODE> with the
40      * specified text, font, and <CODE>Brush</CODE>es.
41      *
42      * @param text the text to render
43      * @param font the <CODE>Font</CODE> to render the text with, or null to
44      * use the default font
45      * @param lineBrush the <CODE>Brush</CODE> to use to draw the outline of
46      * the text
47      * @param fillBrush the <CODE>Brush</CODE> to use to fill the text
48      */

49     public VisualTextObject(final String JavaDoc text, final Font font, final Brush lineBrush, final Brush fillBrush) {
50         super(new Area(), lineBrush, fillBrush);
51         setText(text);
52         setFont(font);
53     }
54
55     /**
56      * Sets the text that will be rendered. If the value provided is null, a
57      * suitable default will be applied instead.
58      *
59      * @param text the text string to render
60      */

61     public void setText(final String JavaDoc text) {
62         if (text == null) {
63             this.text = "";
64         }
65         else {
66             this.text = text;
67         }
68
69         if (font != null) {
70             glyphVector = font.createGlyphVector(context, text);
71             super.setShape(glyphVector.getOutline());
72         }
73     }
74
75     /**
76      * Returns the text that is being rendered.
77      *
78      * @return the text that is being rendered
79      */

80     public String JavaDoc getText() {
81         return text;
82     }
83
84     /**
85      * Sets the <CODE>Font</CODE> that will be used to render the text. If the
86      * value provided is null, a suitable default will be applied instead.
87      *
88      * @param font the <CODE>Font</CODE> to render the text with
89      */

90     public void setFont(final Font font) {
91         if (font == null) {
92             this.font = FontManager.getSharedInstance().getFont("Dialog-PLAIN-12");
93         }
94         else {
95             this.font = font;
96         }
97
98         if (text != null) {
99             glyphVector = this.font.createGlyphVector(context, text);
100             super.setShape(glyphVector.getOutline());
101         }
102     }
103
104     /**
105      * Returns the <CODE>Font</CODE> that is being used to render the text.
106      *
107      * @return the <CODE>Font</CODE> that is being used to render the text
108      */

109     public Font getFont() {
110         return font;
111     }
112 }
113
Popular Tags