?? vrmlloader.java
字號:
import com.sun.j3d.loaders.*;import java.awt.GraphicsConfiguration;import java.applet.Applet;import java.awt.BorderLayout;import java.awt.event.*;import com.sun.j3d.utils.applet.MainFrame;import com.sun.j3d.utils.universe.*;import java.io.*;import javax.imageio.*;import javax.media.j3d.*;import javax.vecmath.*;import com.sun.j3d.loaders.vrml97.VrmlLoader;import com.sun.j3d.loaders.vrml97.*;public class vrmlLoader extends Applet { public BranchGroup createSceneGraph(String filename) { // Create the root of the branch graph BranchGroup sceneRoot = new BranchGroup(); // Create a bounds for the background and lights BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 500.0); // Set up the background Color Color3f bgColor = new Color3f(0.1f, 0.2f, 0.3f); Background bg = new Background(bgColor); bg.setApplicationBounds(bounds); sceneRoot.addChild(bg); // Set up the global lights First, define the color of the lights Color3f light1Color = new Color3f(0.0f, 1.0f,1.0f); Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f); Color3f light2Color = new Color3f(0.3f, 0.3f, 0.4f); Vector3f light2Direction = new Vector3f(-6.0f, -2.0f, -1.0f); Color3f ambientColor = new Color3f(0.1f, 0.1f, 0.1f); // Second, define the ambient light, and insert it in the branch AmbientLight ambientLight = new AmbientLight(ambientColor); ambientLight.setInfluencingBounds(bounds); sceneRoot.addChild(ambientLight); // Lastly, define the directional lights and insert it DirectionalLight light1= new DirectionalLight(light1Color, light1Direction); light1.setInfluencingBounds(bounds); sceneRoot.addChild(light1); DirectionalLight light2= new DirectionalLight(light2Color, light2Direction); light2.setInfluencingBounds(bounds); sceneRoot.addChild(light2); TransformGroup sceneTG = new TransformGroup(); sceneTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); sceneRoot.addChild(sceneTG); TransformGroup objTG = new TransformGroup(); Transform3D objTrans = new Transform3D(); objTG.getTransform(objTrans); objTrans.setScale( 0.2 ); objTG.setTransform(objTrans); sceneTG.addChild(objTG); VrmlLoader l=new VrmlLoader(); Scene s = null; try { s = l.load(filename); } catch (FileNotFoundException e) { System.err.println(e); System.exit(1); } catch (ParsingErrorException e) { System.err.println(e); System.exit(1); } catch (IncorrectFormatException e) { System.err.println(e); System.exit(1); } //sceneTG.addChild(f); objTG.addChild(s.getSceneGroup()); return sceneRoot; } public vrmlLoader(String filename) { filename="b.wrl"; setLayout(new BorderLayout()); GraphicsConfiguration config=SimpleUniverse.getPreferredConfiguration(); Canvas3D c=new Canvas3D(config); add("Center",c); Viewer viewer=new Viewer(c); Vector3d viewpoint=new Vector3d(.0,.0,3.0); Transform3D t=new Transform3D(); t.set(viewpoint); ViewingPlatform v=new ViewingPlatform(); v.getViewPlatformTransform().setTransform(t); // Create a simple scene and attach it to the virtual universe BranchGroup scene = createSceneGraph(filename); SimpleUniverse u = new SimpleUniverse(c); u.getViewingPlatform().setNominalViewingTransform(); u.addBranchGraph(scene); } public static void main(String[] args) { new MainFrame(new vrmlLoader(null), 800,600); }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -