📄 basicscenewithmousecontrolandlights.java
字号:
import javax.swing.JFrame;
import java.awt.BorderLayout;
import java.awt.GraphicsConfiguration;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import com.sun.j3d.utils.behaviors.mouse.*;
import javax.vecmath.*;
public class BasicSceneWithMouseControlAndLights extends JFrame
{
public static void main(String args[])
{
new BasicSceneWithMouseControlAndLights();
}
public BasicSceneWithMouseControlAndLights()
{
getContentPane().setLayout(new BorderLayout());
GraphicsConfiguration config =
SimpleUniverse.getPreferredConfiguration();
Canvas3D canvas = new Canvas3D(config);
getContentPane().add(canvas, BorderLayout.CENTER);
SimpleUniverse u = new SimpleUniverse(canvas);
u.getViewingPlatform().setNominalViewingTransform();
BranchGroup root = new BranchGroup();
TransformGroup transGroup = new TransformGroup();
transGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
BranchGroup scene = createContentBranch();
transGroup.addChild(scene);
root.addChild(transGroup);
MouseRotate rotate = new MouseRotate();
rotate.setTransformGroup(transGroup);
transGroup.addChild(rotate);
rotate.setSchedulingBounds(new BoundingSphere(new Point3d(), 1000.0));
MouseZoom zoom = new MouseZoom();
zoom.setTransformGroup(transGroup);
transGroup.addChild(zoom);
zoom.setSchedulingBounds(new BoundingSphere(new Point3d(), 1000.0));
MouseTranslate translate = new MouseTranslate();
translate.setTransformGroup(transGroup);
transGroup.addChild(translate);
translate.setSchedulingBounds(new BoundingSphere(new Point3d(), 1000.0));
DirectionalLight light = new DirectionalLight(new Color3f(1.0f, 1.0f, 1.0f),
new Vector3f(-1.0f, -1.0f, -1.0f));
light.setInfluencingBounds(new BoundingSphere(new Point3d(),
Double.MAX_VALUE));
root.addChild(light);
AmbientLight ambientLight = new AmbientLight(new Color3f(0.2f, 0.2f, 0.2f));
ambientLight.setInfluencingBounds(new BoundingSphere(new Point3d(),
Double.MAX_VALUE));
root.addChild(ambientLight);
u.addBranchGraph(root);
setSize(256, 256);
setVisible(true);
}
public BranchGroup createContentBranch()
{
BranchGroup root = new BranchGroup();
// The color cube geometry...
ColorCube colorCube = new ColorCube(0.3);
root.addChild(colorCube);
root.compile();
return root;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -