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

1    package j3d.cr325.text; 
2     
3    import com.sun.j3d.utils.applet.MainFrame; 
4    import com.sun.j3d.utils.universe.SimpleUniverse; 
5     
6    import javax.media.j3d.*; 
7    import javax.vecmath.Color3f; 
8    import javax.vecmath.Vector3f; 
9    import java.applet.Applet; 
10   import java.awt.*; 
11    
12    
13   public class Text3DApp extends Applet { 
14    
15       public BranchGroup createSceneGraph() { 
16           BranchGroup bg 
17                   = new BranchGroup(); 
18           Transform3D t3D 
19                   = new Transform3D(); 
20           t3D.setTranslation(new Vector3f(0.0f, 0.0f, -3.0f)); 
21           TransformGroup tg 
22                   = new TransformGroup(t3D); 
23           bg.addChild(tg); 
24    
25           TransformGroup objSpin 
26                   = addSpinningTransformGroup(tg); 
27    
28           Appearance textAppear = getTextAppearance(); 
29           addText(textAppear, objSpin); 
30    
31           addLightsBoundingSphereAndTransforms(objSpin, tg); 
32    
33           return bg; 
34       } 
35    
36       private void addLightsBoundingSphereAndTransforms(TransformGroup objSpin, 
37                                                         TransformGroup tg) { 
38           Alpha rotationAlpha = new Alpha(-1, 10000); 
39    
40           RotationInterpolator ri = 
41                   new RotationInterpolator(rotationAlpha, objSpin); 
42    
43           // a bounding sphere specifies a region a behavior is active 
44           // create a sphere centered at the origin with radius of 100 
45           BoundingSphere bs 
46                   = new BoundingSphere(); 
47           ri.setSchedulingBounds(bs); 
48           objSpin.addChild(ri); 
49           addDirectionalLight(bs, tg); 
50           addAmbientLighting(bs, tg); 
51       } 
52    
53       private void addDirectionalLight(BoundingSphere bounds, TransformGroup tg) { 
54           DirectionalLight lightD = new DirectionalLight(); 
55           lightD.setInfluencingBounds(bounds); 
56           lightD.setDirection(new Vector3f(0.0f, 0.0f, -1.0f)); 
57           lightD.setColor(new Color3f(1.0f, 0.0f, 1.0f)); 
58           tg.addChild(lightD); 
59       } 
60    
61       private void addAmbientLighting(BoundingSphere bounds, TransformGroup tg) { 
62           AmbientLight al = new AmbientLight(); 
63           al.setInfluencingBounds(bounds); 
64           tg.addChild(al); 
65       } 
66    
67       private void addText(Appearance textAppear, TransformGroup objSpin) { 
68           Font3D font3D 
69                   = new Font3D(new Font("Helvetica", Font.PLAIN, 1), 
70                           new FontExtrusion()); 
71           Text3D textGeom 
72                   = new Text3D(font3D, new String("3DText")); 
73    
74           textGeom.setAlignment(Text3D.ALIGN_CENTER); 
75           Shape3D textShape 
76                   = new Shape3D(); 
77           textShape.setGeometry(textGeom); 
78           textShape.setAppearance(textAppear); 
79           objSpin.addChild(textShape); 
80       } 
81    
82       private TransformGroup addSpinningTransformGroup(TransformGroup tg) { 
83           TransformGroup objSpin = new TransformGroup(); 
84           objSpin.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); 
85           tg.addChild(objSpin); 
86           return objSpin; 
87       } 
88    
89       private Appearance getTextAppearance() { 
90           Appearance textAppear = new Appearance(); 
91           ColoringAttributes textColor = new ColoringAttributes(); 
92           textColor.setColor(1.0f, 0.0f, 0.0f); 
93           textAppear.setColoringAttributes(textColor); 
94           textAppear.setMaterial(new Material()); 
95           return textAppear; 
96       } 
97    
98    
99       public Text3DApp() { 
100          setLayout(new BorderLayout()); 
101          GraphicsConfiguration gc = 
102                  SimpleUniverse.getPreferredConfiguration(); 
103   
104          Canvas3D canvas3D = new Canvas3D(gc); 
105          canvas3D.setStereoEnable(false); 
106          add("Center", canvas3D); 
107   
108          BranchGroup bg = createSceneGraph(); 
109          SimpleUniverse su = new SimpleUniverse(canvas3D); 
110          su.getViewingPlatform().setNominalViewingTransform(); 
111          su.addBranchGraph(bg); 
112      } 
113   
114   
115      public static void main(String[] args) { 
116          new MainFrame(new Text3DApp(), 256, 256); 
117      } 
118   
119  } // end of class Text3DApp 
120