How do I add text to a movie?
Added: 31 Jan 2009. Working examples for Java
There are two types of text in a movie: static text which is used to display strings and dynamic text which is used to create editable text fields which are used to set variables which can then be used later in the movie or submitted to a server application.
Static text fields are created using the Font and TextTable classes to generate the text and font definitions:
DefineText2 text = textGenerator.defineText(uid++, str, WebPalette.BLACK.color()); DefineFont2 fontDef = font.defineFont(uid++, set.getCharacters());
To create the font definition you define the set of characters that will be used so the font definition is as small as possible:
final CharacterSet set = new CharacterSet(); set.add("The quick, brown fix jumped over the lazy dog."); DefineFont2 fontDef = font.defineFont(uid++, set.getCharacters());
Then a table of characters is created using TextTable:
final TextTable textGenerator = new TextTable(fontDef, fontSize * 20);
The table contains the list of glyphs used in the font and the static text definition uses the index of each glyph to identify which character should be displayed.
Dynamic Text Fields
Dynamic text fields are much easier to use than static ones. Rather than deal with glyphs, the actual string displayed is set in the DefineTextField object. You still need to deal with Font and TextTable classes to calculate the bounding box for the field and to generate the font definition however.
DefineFont2 fontDef = font.defineFont(uid++, set.getCharacters()); final TextTable texter = new TextTable(fontDef, fontSize * 20); DefineTextField field = new DefineTextField(identifier); field.setBounds(texter.boundsForText(str)); field.setFontHeight(fontSize * 20); field.setFontIdentifier(font.getIdentifier()); field.setEmbedded(true); field.setColor(color); field.setInitialText(str);
DefineTextField contains a large number of attributes that control the appearance of the text field.
Attributes | |
---|---|
wordWrap | Indicates whether the text should be wrapped. |
multiline | Indicates whether the text field contains multiple lines. |
password | Indicates whether the text field will be used to display a password. |
readOnly | Indicates whether the text field is read only. |
selectable | Indicates whether the text field is selectable. |
bordered | Indicates whether the text field is bordered. |
HTML | Indicates whether the text field contains HTML. |
autosize | Indicates whether the text field will resize automatically to fit the text entered. |
maxLength | The maximum length of the text field. May be set to zero is not maximum length is defined. |
If you set the following attributes:
field.setReadOnly(true); field.setSelectable(false); field.setBordered(false);
then the dynamic text field will be identical in appearance to a static one. Unless you need control over layout and are displaying more than one font face in a block of text then using dynamic text fields is often easier than using static ones.