📄 simplemorph.java
字号:
//SimpleMorph.java
//演示如何使用Morph对象
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Frame;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import java.util.Enumeration;
public class SimpleMorph extends Applet {
//创建自己的MorphBehavior对象
public class MorphBehavior extends Behavior{
private Morph targetMorph;
private Alpha alpha;
// the following two members are here for effciency (no memory burn)
private double[] weights = {0, 0};
private WakeupCondition trigger = new WakeupOnElapsedFrames(0);
// create MorphBehavior
MorphBehavior(Morph targetMorph, Alpha alpha){
this.targetMorph = targetMorph;
this.alpha = alpha;
}
public void initialize(){
// set initial wakeup condition
this.wakeupOn(trigger);
}
public void processStimulus(Enumeration criteria){
weights[0] = 0;
weights[1] = 1;
float alphaValue = alpha.value();
weights[0] = 1.0 - alphaValue;
weights[1] = alphaValue;
targetMorph.setWeights(weights);
// set next wakeup condition
this.wakeupOn(trigger);
}
}
public GeometryArray createGeomArray0(){
int[] counts = {2,2,2,2,2,2};
LineStripArray geom = new LineStripArray(12, GeometryArray.COORDINATES | GeometryArray.COLOR_3, counts);
float[] coordinates = { -0.30f,0.00f,0f, -0.20f,0.20f,0f,
-0.20f, 0.20f, 0f, 0.20f, 0.20f, 0f,
0.20f, 0.20f, 0f, 0.30f, 0.00f, 0f,
0.20f, -0.20f, 0f, -0.20f, -0.20f, 0f,
0.30f, 0.00f, 0f, 0.20f, -0.20f, 0f,
-0.20f, -0.20f, 0f, -0.30f,0.00f, 0f
};
geom.setCoordinates(0, coordinates);
return geom;
}
public GeometryArray createGeomArray1(){
int[] counts = {2,2,2,2,2,2};
LineStripArray geom = new LineStripArray(12, GeometryArray.COORDINATES | GeometryArray.COLOR_3, counts);
float[] coordinates = {-0.20f,-0.20f,0f,-0.20f,0.20f,0f,
-0.20f,0.20f,0f, 0.20f,0.20f,0f,
0.20f,0.20f,0f, 0.20f,-0.20f,0f,
0.20f,-0.20f,0f, -0.20f,-0.20f,0f,
-0.20f,0.20f,0f, 0.20f,-0.20f,0f,
0.20f,0.20f,0f,-0.20f,-0.20f,0};
geom.setCoordinates(0, coordinates);
return geom;
}
public BranchGroup createSceneGraph() {
// Create the root of the branch graph
BranchGroup objRoot = new BranchGroup();
Transform3D t3d = new Transform3D();
t3d.setScale(2);
TransformGroup translate = new TransformGroup(t3d);
// create GeometryArray[] (array of GeometryArray objects)
GeometryArray[] geomArray = new GeometryArray[2];
geomArray[0] = createGeomArray0();
geomArray[1] = createGeomArray1();
// create morph object
Morph morphObj = new Morph(geomArray);
morphObj.setCapability(Morph.ALLOW_WEIGHTS_WRITE);
// create alpha object
Alpha alpha = new Alpha(-1, 1, 0, 0, 2000, 100, 0, 0, 0, 0);
// create morph driving behavior
MorphBehavior morphBehav = new MorphBehavior(morphObj, alpha);
morphBehav.setSchedulingBounds(new BoundingSphere());
//assemble scene graph
objRoot.addChild(translate);
translate.addChild(morphObj);
objRoot.addChild(morphBehav);
Background background = new Background();
background.setColor(1f, 1f, 1f);
background.setApplicationBounds(new BoundingSphere());
objRoot.addChild(background);
// Let Java 3D perform optimizations on this scene graph.
objRoot.compile();
return objRoot;
}
// Create a simple scene and attach it to the virtual universe
public SimpleMorph() {
setLayout(new BorderLayout());
Canvas3D canvas3D = new Canvas3D(null);
add("Center", canvas3D);
BranchGroup scene = createSceneGraph();
// SimpleUniverse is a Convenience Utility class
SimpleUniverse simpleU = new SimpleUniverse(canvas3D);
// This will move the ViewPlatform back a bit so the
// objects in the scene can be viewed.
simpleU.getViewingPlatform().setNominalViewingTransform();
simpleU.addBranchGraph(scene);
}
public static void main(String[] args) {
Frame frame = new MainFrame(new SimpleMorph(), 256, 256);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -