java3d 实例_java 3D画图实例
时间: 2023-08-15 14:08:36 浏览: 138
java3D小例子
3星 · 编辑精心推荐
以下是一个简单的Java 3D画图实例,它绘制了一个旋转的立方体:
```java
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.GraphicsConfiguration;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.ColorCube;
import com.sun.j3d.utils.universe.SimpleUniverse;
public class Java3DExample extends Applet {
public Java3DExample() {
setLayout(new BorderLayout());
GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
Canvas3D canvas = new Canvas3D(config);
add("Center", canvas);
BranchGroup scene = createSceneGraph();
scene.compile();
SimpleUniverse simpleU = new SimpleUniverse(canvas);
simpleU.getViewingPlatform().setNominalViewingTransform();
simpleU.addBranchGraph(scene);
}
public BranchGroup createSceneGraph() {
BranchGroup objRoot = new BranchGroup();
Transform3D tr = new Transform3D();
tr.setTranslation(new Vector3f(0.0f, 0.0f, -5.0f));
objRoot.addChild(new ColorCube(0.4));
Alpha rotationAlpha = new Alpha(-1, 4000);
RotationInterpolator rotator = new RotationInterpolator(rotationAlpha, objRoot, tr, 0.0f, (float) Math.PI * 2.0f);
BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
rotator.setSchedulingBounds(bounds);
objRoot.addChild(rotator);
return objRoot;
}
public static void main(String[] args) {
new MainFrame(new Java3DExample(), 640, 480);
}
}
```
这个例子创建了一个Java 3D场景,其中包含一个旋转的彩色立方体。要运行这个例子,请将它保存到名为Java3DExample.java的文件中,并使用Java编译器编译它。然后,从命令行运行以下命令:
```
appletviewer Java3DExample.java
```
这将在窗口中显示一个Java 3D场景,您可以在其中旋转和缩放立方体。
阅读全文