KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > buchuki > ensmer > text > SquareTextDisplay


1 /*
2  * Copyright 2005 Dusty Phillips
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package com.buchuki.ensmer.text;
18
19 import com.buchuki.ensmer.object.Square;
20 import java.awt.*;
21 import javax.media.j3d.*;
22 import javax.vecmath.Vector3f;
23
24 /**
25  * A utility class to combine the TextDisplay and the Square object to provide
26  * a background for the text. The appearance of the Square object is available
27  * for customization.
28  *
29  * @author Dusty Phillips [dusty@buchuki.com]
30  */

31 public class SquareTextDisplay extends BranchGroup {
32     
33     /**
34      * Creates a new instance of SquareText with a reference to the color
35      * and font to be displayed and the amount of margin to be displayed around
36      * the text against the background. The size of the object can grow without
37      * bound depending on the size of the text, but will not be less than the
38      * minimum width and height provided.
39      *
40      * @param color the color of the text
41      * @param font the font of the text
42      * @param margin the amount of space (world coordinates) between the text and the edge
43      * of the background
44      * @param minWidth the minimum width of the background, regardless of text size
45      * (world coordinates)
46      * @param minHeight the minimum height of the background, regardless of text size
47      * (world coordinates)
48      */

49     public SquareTextDisplay(Color color, Font font, float margin, float minWidth, float minHeight) {
50         text = new TextDisplay(color, font);
51         background = new Square();
52         textTG = new TransformGroup();
53         textTG.addChild(text);
54         addChild(textTG);
55         addChild(background);
56         textTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
57         this.minWidth = minWidth;
58         this.minHeight = minHeight;
59         this.margin = margin;
60         setText("");
61     }
62     
63     /**
64      * Set the text displayed on the object. The size of the object is resized
65      * to fit the text. If the text takes up less space than the minimum width
66      * and height, those sizes are used instead.
67      *
68      * @param newText the textInput to display
69      */

70     public void setText(TextInput newText) {
71         text.setText(newText);
72         float width = text.getWidth();
73         float height = text.getHeight();
74         width = width < minWidth ? minWidth : width;
75         height = height < minHeight ? minHeight : height;
76         Transform3D trans = new Transform3D();
77         trans.setTranslation(new Vector3f(-0.5f * width, 0.5f * height, 0.001f));
78         textTG.setTransform(trans);
79         background.setSize(width + margin, height + margin);
80     }
81     
82     /**
83      * Set the text displayed on teh object to a string. No cursor is displayed.
84      *
85      * @param newText the string to display
86      */

87     public void setText(String JavaDoc newText) {
88         TextInput ti = new TextInput();
89         ti.setText(newText);
90         ti.setFocused(false);
91         setText(ti);
92     }
93     
94     /**
95      * Get the Appearance associated with the background (NOT with the text) so
96      * it can have a texture or material associated with it.
97      *
98      * @return the background's appearance node
99      */

100     public Appearance getAppearance() {
101         return background.getAppearance();
102     }
103     
104     /**
105      * Get the width of the square. This is the width of the text plus
106      * two margins
107      *
108      * @return the width of the square
109      */

110     public float getWidth() {
111         float width = text.getWidth();
112         width = width < minWidth ? minWidth : width;
113         return width + 2*margin;
114     }
115     
116     /**
117      * Get the height of the square. This is the height of the text plus
118      * two margins
119      */

120     public float getHeight() {
121         float height = text.getHeight();
122         height = height < minHeight ? minHeight : height;
123         return height + 2*margin;
124     }
125     
126     /**
127      * A TextDisplay displaying the text.
128      */

129     private TextDisplay text;
130     
131     /**
132      * A Square representing the background
133      */

134     private Square background;
135     
136     /**
137      * A TransformGroup moving the text from the origin to the corners of the
138      * square
139      */

140     private TransformGroup textTG;
141     
142     /**
143      * The margin between the text and the edge of the background
144      */

145     private float margin;
146     
147     /**
148      * The minimum width of the background.
149      */

150     private float minWidth;
151     
152     /**
153      * The minimum height of the background.
154      */

155     private float minHeight;
156     
157 }
158
Popular Tags