📄 vview.java
字号:
// Very simple VRML world viewer
// Written by Bernie Roehl, October 1999
//
// CONVERTING TO AN APPLET:
// To convert this application into an applet you must construct
// a standard init() method that contains the code now in the VView
// constructor. From within the init() method you will invoke the VRML
// loader抯 load(URL) method, as the load(String) method currently used
// by this application can抰 be used in an applet (a security exception
// is thrown by the applet when loading a VRML world from a String).
//
// To convert to an applet, simply rename the following method:
// public VView(String avatar)
// to:
// public void init()
//
// Next, use the applet抯 getParameter() method to retrieve the
// name of the VRML file to load and place the resulting string into the
// "avatar" variable. The following code assumes that a PARAM named
// "world" is in the corresponding HTML Web page for this applet:
// String avatar = getParameter("world");
//
// Finally, change the VRML loader抯 load(String) method so that it
// is passed an absolute URL. For example:
// scene = loader.load(new java.net.URL(getCodeBase(), avatar + ".wrl"));
//
// Visit the Core Web3D web site at http://www.CoreWeb3D.com/ to
// obtain code listings from this book, including an applet version of this
// VView program.
import java.awt.*;
import java.applet.Applet;
import java.util.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.loaders.vrml97.VrmlLoader;
import com.sun.j3d.loaders.Scene;
import com.sun.j3d.utils.image.TextureLoader;
public class VView extends Applet {
SimpleUniverse universe; // the universe
Scene scene = null; // the VRML scene that we load
VView(String avatar) {
setLayout(new BorderLayout());
Canvas3D canvas = new Canvas3D(null);
add("Center",canvas);
universe = new SimpleUniverse(canvas);
ViewingPlatform viewingPlatform =
universe.getViewingPlatform();
TransformGroup vpTransGroup =
viewingPlatform.getViewPlatformTransform();
View view = (universe.getViewer()).getView();
VrmlLoader loader = new VrmlLoader();
try { scene = loader.load(avatar + ".wrl"); }
catch (Exception e) {
System.out.println(
"Exception loading file from path:"
+ avatar + ".wrl");
System.exit(1);
}
// get the scene group from the loaded VRML scene
BranchGroup sceneGroup = scene.getSceneGroup();
sceneGroup.setCapability(
BranchGroup.ALLOW_DETACH);
sceneGroup.setCapability(
BranchGroup.ALLOW_BOUNDS_READ);
// list all the nodes found in the file
System.out.println("Named nodes in file:");
Hashtable ht = scene.getNamedObjects();
Enumeration en = ht.keys();
while (en.hasMoreElements())
System.out.println("\t" + en.nextElement());
// make sure left shoulder is accessible
TransformGroup l_shoulder =
TransformGroup) ht.get(avatar +
"_l_shoulder");
l_shoulder.setCapability(
TransformGroup.ALLOW_TRANSFORM_READ);
l_shoulder.setCapability(
TransformGroup.ALLOW_TRANSFORM_WRITE);
// make sure right shoulder is accessible
TransformGroup l_elbow =
(TransformGroup) ht.get(avatar +
"_l_elbow");
l_elbow.setCapability(
TransformGroup.ALLOW_TRANSFORM_READ);
l_elbow.setCapability(
TransformGroup.ALLOW_TRANSFORM_WRITE);
// make the VRML scene live
universe.addBranchGraph(sceneGroup);
// find the radius and center of
// the scene抯 bounding sphere
BoundingSphere sceneBounds =
(BoundingSphere)sceneGroup.getBounds();
double radius = sceneBounds.getRadius();
Point3d center = new Point3d();
sceneBounds.getCenter(center);
// now move the viewpoint back so we can
// see the whole scene
Vector3d temp = new Vector3d(center);
temp.z += 1.4 * radius /
Math.tan(view.getFieldOfView() / 2.0);
// and finally, set that viewpoint into
// the viewing transform
Transform3D viewTransform = new Transform3D();
viewTransform.set(temp);
vpTransGroup.setTransform(viewTransform);
// now rotate the shoulder
Transform3D trans = new Transform3D();
l_shoulder.getTransform(trans);
trans.setRotation(
new AxisAngle4f(0, 0, 1, 2.7057f));
l_shoulder.setTransform(trans);
// and the elbow
l_elbow.getTransform(trans);
trans.setRotation(
new AxisAngle4f(0, 0, 1, 0.7057f));
l_elbow.setTransform(trans);
}
public static void main(String[] args) {
new MainFrame(new VView(args[0]), 320, 400);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -