📄 simpleinterpolator.java
字号:
//SimpleInterpolator.java
//演示如何使用Interpolator来制作动画
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 com.sun.j3d.utils.geometry.ColorCube;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.image.TextureLoader;import com.sun.j3d.utils.geometry.Box;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.behaviors.interpolators.*;
public class SimpleInterpolator extends Applet {
public BranchGroup createSceneGraph() {
// Create the root of the branch graph
BranchGroup objRoot = new BranchGroup();
Transform3D t3d = new Transform3D();
BoundingSphere bounds = new BoundingSphere();
// create target TransformGroup with Capabilities
TransformGroup objMove1 = new TransformGroup();
objMove1.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
Alpha alpha1 = new Alpha (-1,
Alpha.INCREASING_ENABLE + Alpha.DECREASING_ENABLE,
0, 0, 2000, 0, 1000, 2000, 0, 1000);
// create position interpolator
PositionInterpolator posInt1 = new PositionInterpolator (alpha1, objMove1);
posInt1.setSchedulingBounds(bounds);
posInt1.setStartPosition(-1.0f);
t3d.setTranslation(new Vector3f(0.0f, 0.5f, 0.0f));
TransformGroup objMovePos1 = new TransformGroup(t3d);
objRoot.addChild(objMovePos1);
objMovePos1.addChild(objMove1);
objMove1.addChild(new ColorCube(0.1f));
objRoot.addChild(posInt1);
TransformGroup objMove2 = new TransformGroup();
objMove2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
Alpha alpha2 = new Alpha (-1,
Alpha.INCREASING_ENABLE + Alpha.DECREASING_ENABLE,
0, 0, 2000, 1800, 1000, 2000, 1800, 1000);
// create position interpolator
PositionInterpolator posInt2 = new PositionInterpolator (alpha2, objMove2);
posInt2.setSchedulingBounds(bounds);
posInt2.setStartPosition(-1.0f);
t3d.setTranslation(new Vector3f(0.0f, 0.0f, 0.0f));
TransformGroup objMovePos2 = new TransformGroup(t3d);
objRoot.addChild(objMovePos2);
objMovePos2.addChild(objMove2);
objMove2.addChild(new ColorCube(0.1f));
objRoot.addChild(posInt2);
TransformGroup objMove3 = new TransformGroup();
objMove3.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
Alpha alpha3 = new Alpha (-1,
Alpha.INCREASING_ENABLE + Alpha.DECREASING_ENABLE,
0, 0, 2000, 600, 1000, 2000, 600, 1000);
// create position interpolator
PositionInterpolator posInt3 = new PositionInterpolator (alpha3, objMove3);
posInt3.setSchedulingBounds(bounds);
posInt3.setStartPosition(-1.0f);
t3d.setTranslation(new Vector3f(0.0f,-0.5f, 0.0f));
TransformGroup objMovePos3 = new TransformGroup(t3d);
objRoot.addChild(objMovePos3);
objMovePos3.addChild(objMove3);
objMove3.addChild(new ColorCube(0.1f));
objRoot.addChild(posInt3);
// create target TransformGroup with Capabilities
/* TransformGroup objRotate = new TransformGroup();
objRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
// create rotation interpolator
Alpha alpha = new Alpha (-1,
Alpha.INCREASING_ENABLE + Alpha.DECREASING_ENABLE,
0, 0, 2000, 0, 1000, 2000, 0, 1000);
RotationInterpolator rotInt = new RotationInterpolator (alpha, objRotate);
rotInt.setSchedulingBounds(bounds);
objRoot.addChild(rotInt);
t3d.setTranslation(new Vector3f(0.0f, 0.0f, 0.0f));
TransformGroup objRotPos = new TransformGroup(t3d);
objRoot.addChild(objRotPos);
objRotPos.addChild(objRotate);
objRotate.addChild(new ColorCube(0.1));
t3d.setTranslation(new Vector3f(0.5f, 0.0f, 0.0f));
TransformGroup objRotPosOut = new TransformGroup(t3d);
objRotPosOut.addChild(new ColorCube(0.1));
objRotate.addChild(objRotPosOut);
*/
/* Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
Color3f yellow = new Color3f(1.0f, 1.0f, 0.0f);
Color3f cyan = new Color3f(0.0f, 1.0f, 1.0f);
Color3f megenta= new Color3f(1.0f,0.0f,1.0f);
Color3f red = new Color3f(1.0f,0.0f,0.0f); String filename="flower2.jpg"; TextureLoader myLoader=new TextureLoader (filename,this); ImageComponent2D myImage=myLoader.getImage (); Texture2D myTex=(Texture2D)myLoader.getTexture();
// Create appearance object for textured cube Appearance app = new Appearance();
// app.setCapability(Appearance.ALLOW_MATERIAL_WRITE ); app.setTexture(myTex);
Material mat = new Material();
mat.setCapability(Material.ALLOW_COMPONENT_WRITE);
// mat.setDiffuseColor(cyan);
mat.setSpecularColor(white);
mat.setShininess(128); app.setMaterial (mat);
TextureAttributes texAttr = new TextureAttributes();
// texAttr.setCapability(TextureAttributes.ALLOW_TRANSFORM_WRITE |TextureAttributes.ALLOW_BLEND_COLOR_WRITE); texAttr.setTextureMode(TextureAttributes.BLEND ); texAttr.setTextureBlendColor (new Color4f(0.9f,0.9f,0.9f,0.1f)); app.setTextureAttributes(texAttr);
Alpha alpha = new Alpha (-1,
Alpha.INCREASING_ENABLE + Alpha.DECREASING_ENABLE,
0, 0, 2000, 0, 1000, 2000, 0, 1000);
ColorInterpolator colInt = new ColorInterpolator (alpha, mat);
colInt.setStartColor(cyan);
colInt.setEndColor(megenta);
colInt.setSchedulingBounds(bounds);
objRoot.addChild(colInt);
Box myBox = new Box(0.4f, 0.4f, 0.4f, Box.GENERATE_TEXTURE_COORDS |Box.GENERATE_NORMALS , app);
// myBox.setCapability(Box.ENABLE_APPEARANCE_MODIFY);
objRoot.addChild(myBox);
DirectionalLight lightD1 = new DirectionalLight();
lightD1.setDirection(new Vector3f(0.0f,-1.0f,-1.0f));
lightD1.setColor(white);
lightD1.setInfluencingBounds(bounds);
objRoot.addChild(lightD1);
*/
// create target TransformGroup with Capabilities
/* TransformGroup objScale = new TransformGroup();
objScale.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
Alpha alpha = new Alpha (-1,
Alpha.INCREASING_ENABLE + Alpha.DECREASING_ENABLE,
0, 0, 2000, 0, 1000, 2000, 0, 1000);
// create scale interpolator
ScaleInterpolator scaInt = new ScaleInterpolator (alpha, objScale);
scaInt.setSchedulingBounds(bounds);
t3d.setTranslation(new Vector3f(-0.4f, -0.4f, 0.0f));
TransformGroup objScalePos = new TransformGroup(t3d);
objRoot.addChild(objScalePos);
objScalePos.addChild(objScale);
objScale.addChild(new ColorCube(0.4));
objRoot.addChild(scaInt);
*/
// create target Switch with Capabilities
/* Switch objSwitch = new Switch();
objSwitch.setCapability(Switch.ALLOW_SWITCH_WRITE);
objSwitch.addChild(new Sphere(0.4f));
objSwitch.addChild(new ColorCube(0.4f));
Alpha alpha = new Alpha (-1,
Alpha.INCREASING_ENABLE + Alpha.DECREASING_ENABLE,
0, 0, 2000, 0, 1000, 2000, 0, 1000);
// create switch value interpolator
SwitchValueInterpolator swiInt = new SwitchValueInterpolator (alpha, objSwitch);
swiInt.setSchedulingBounds(bounds);
swiInt.setLastChildIndex(1);// since switch made after interpolator
t3d.setTranslation(new Vector3f(0.0f, 0.0f, 0.0f));
TransformGroup objSwitchPos = new TransformGroup(t3d);
objRoot.addChild(objSwitchPos);
objSwitchPos.addChild(objSwitch);
objRoot.addChild(swiInt);
*/
/* Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
Color3f cyan = new Color3f(0.0f, 1.0f, 1.0f); Appearance transpAppear = new Appearance(); Material mat = new Material();
mat.setDiffuseColor(cyan);
mat.setSpecularColor(white);
mat.setShininess(128); transpAppear.setMaterial (mat);
// create target Transparency with Capabilities
TransparencyAttributes objTransp = new TransparencyAttributes();
objTransp.setCapability(TransparencyAttributes.ALLOW_VALUE_WRITE);
objTransp.setTransparencyMode(TransparencyAttributes.BLENDED);
transpAppear.setTransparencyAttributes(objTransp);
Alpha alpha = new Alpha (-1,
Alpha.INCREASING_ENABLE + Alpha.DECREASING_ENABLE,
0, 0, 2000, 0, 1000, 2000, 0, 1000);
TransparencyInterpolator traInt = new TransparencyInterpolator (alpha, objTransp);
traInt.setSchedulingBounds(bounds);
t3d.setTranslation(new Vector3f(0.0f, 0.0f, 0.0f));
TransformGroup objTranspPos = new TransformGroup(t3d);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -