/Users/lyon/j4p/src/j3d/cr325/text/Text2DApp.java

1    package j3d.cr325.text; 
2     
3    import com.sun.j3d.utils.applet.MainFrame; 
4    import com.sun.j3d.utils.geometry.Text2D; 
5    import com.sun.j3d.utils.universe.SimpleUniverse; 
6     
7    import javax.media.j3d.*; 
8    import javax.vecmath.Color3f; 
9    import java.applet.Applet; 
10   import java.awt.*; 
11    
12    
13   public class Text2DApp extends Applet { 
14    
15       public BranchGroup createSceneGraph() { 
16           BranchGroup branchGroupRoot 
17                   = new BranchGroup(); 
18           TransformGroup tg 
19                   = new TransformGroup(); 
20           tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); 
21           branchGroupRoot.addChild(tg); 
22           Text2D text2d 
23                   = new Text2D("2D text in Java 3D", 
24                           new Color3f(0.9f, 1.0f, 1.0f), 
25                           "Helvetica", 24, Font.ITALIC); 
26           tg.addChild(text2d); 
27    
28           makeText2Sided(text2d.getAppearance()); 
29    
30           // Create a new Behavior object that will perform the desired 
31           // operation on the specified transform object and add it into 
32           // the scene graph. 
33           Alpha rotationAlpha = new Alpha(-1, 8000); 
34    
35           RotationInterpolator ri = 
36                   new RotationInterpolator(rotationAlpha, tg); 
37           boundingSphereAtOriginR100(ri, tg); 
38    
39           Background bkRound = new Background(); 
40           bkRound.setColor(0.4f, 0.4f, 1.0f); 
41           bkRound.setApplicationBounds(new BoundingSphere()); 
42           branchGroupRoot.addChild(bkRound); 
43    
44           return branchGroupRoot; 
45       } // end of CreateSceneGraph method 
46    
47       private void makeText2Sided(Appearance textAppear) { 
48           PolygonAttributes polyAttrib = new PolygonAttributes(); 
49           polyAttrib.setCullFace(PolygonAttributes.CULL_NONE); 
50           polyAttrib.setBackFaceNormalFlip(true); 
51           textAppear.setPolygonAttributes(polyAttrib); 
52       } 
53    
54       private void boundingSphereAtOriginR100(RotationInterpolator ri, 
55                                               TransformGroup tg) { 
56           BoundingSphere bs = new BoundingSphere(); 
57           ri.setSchedulingBounds(bs); 
58           tg.addChild(ri); 
59       } 
60    
61    
62       public Text2DApp() { 
63           setLayout(new BorderLayout()); 
64           GraphicsConfiguration gc = 
65                   SimpleUniverse.getPreferredConfiguration(); 
66    
67           Canvas3D canvas3D = new Canvas3D(gc); 
68           canvas3D.setStereoEnable(false); 
69           add("Center", canvas3D); 
70    
71           BranchGroup bg = createSceneGraph(); 
72           SimpleUniverse su = new SimpleUniverse(canvas3D); 
73    
74           // This will move the ViewPlatform back a bit so the 
75           // objects in the scene can be viewed. 
76           su.getViewingPlatform().setNominalViewingTransform(); 
77    
78           su.addBranchGraph(bg); 
79       } 
80    
81       public static void main(String[] args) { 
82           new MainFrame(new Text2DApp(), 256, 32); 
83       } // end of main (method of Text2DApp) 
84    
85   } // end of class Text2DApp 
86