< Zurück | Inhalt | Weiter >

8.5 Text2D












Text2D creates a texture−mapped rectangle that displays a text string. The size of the rectangle and its texture map are customizable through the constructor. The resulting Shape3D object is a transparent (except for the text) rectangle located at 0, 0, 0 and extending up the positive Y−axis and out the positive X−axis.

Text2D essentially creates an image when it is constructed and draws the text string into the image. This texture image is then applied (once) at construction to the rectangular geometry.

NOTE The setRectangleScaleFactor/getRectangleScaleFactor methods may appear to have no effect, but note the following Sun bug report: the

setRectangleScaleFactor method will not change text size unless the setString

method is called. It must therefore be called before any calls to setString are made.


Figure 8.10

Two frames from RasterTest. Each frame contains a rotating cube and a Raster displaying the depth components of the entire frame. The Raster is visible because it also has a depth component

As a Text2D object is merely a rectangle with a texture applied to its front, it is interesting to consider the trade−off between creating the Text2D object with larger font sizes and scaling the Text2D object as a whole. On the one hand, creating a Text2D object with a larger font size will create a larger rectangle, and larger texture image will be applied to the rectangle. Applications require more memory to handle larger texture images, and increased memory requirements could become significant if large numbers of Text2D objects are created with large font sizes. On the other hand, creating a Text2D object with a small font and then scaling the object using a TransformGroup will result in smaller texture images and lower memory requirements, but image resolution will be compromised as the texture image is scaled. These trade−offs are illustrated in figure 8.10.

A more interesting Text2D implementation would have the ability to regenerate its texture image based on the Text2D object’s distance from the viewer. In this way the Text2D could automatically incorporate LOD−type behavior, creating high−resolution text string images when viewed up close, and saving memory by dynamically creating low−resolution text string images when viewed from afar.

Also, be aware that because a Text2D object applies the string as a texture image, the texture image is only viewable from the “right side” of the Text2D object. To use the Text2D object as a label a Billboard type behavior will have to be used to ensure the back face of the Text2D object is never visible to the viewer.