Une animation en Java 3D
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Novembre 2001 */
import java.applet.Applet;
import java.awt.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;
import javax.media.j3d.*;
import javax.vecmath.*;
public class SimpleAnimation extends Applet {
public BranchGroup createSceneGraph(SimpleUniverse u) {
BranchGroup objRoot = new BranchGroup();
BoundingSphere bounds;
bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0),100.0);
Vector3f ldir = new Vector3f(1.0F,1.0F,-1.0F);
Color3f lcouldl = new Color3f(1.0F,1.0F,1.0F);
DirectionalLight dl = new DirectionalLight(lcouldl,ldir);
dl.setInfluencingBounds(bounds);
objRoot.addChild(dl);
Color3f lcoulal = new Color3f(0.3F,0.3F,0.1F);
AmbientLight al = new AmbientLight(lcoulal);
al.setInfluencingBounds(bounds);
objRoot.addChild(al);
{ TransformGroup objTrans1 = new TransformGroup();
objRoot.addChild(objTrans1);
objTrans1.setCapability(
TransformGroup.ALLOW_TRANSFORM_WRITE);
Transform3D yAxis = new Transform3D();
Alpha rotationAlpha;
rotationAlpha = new Alpha(-1,
Alpha.INCREASING_ENABLE,
0,0,
4000,0,0,
0,0,0);
RotationInterpolator rotator;
rotator = new RotationInterpolator(
rotationAlpha,
objTrans1,yAxis,
0.0f,
(float) Math.PI*2.0f);
rotator.setSchedulingBounds(bounds);
objTrans1.addChild(rotator);
Appearance a = new Appearance();
Material m = new Material();
m.setDiffuseColor(0.1f,0.9f,0.9f);
m.setSpecularColor(1.0f,0.0f,0.0f);
m.setAmbientColor(0.9f,0.9f,0.9f);
m.setShininess(25.0f);
a.setMaterial(m);
TransformGroup objTrans2 = new TransformGroup();
objTrans1.addChild(objTrans2);
Transform3D t3D = new Transform3D();
t3D.setRotation(new AxisAngle4d(1.0,1.0,0.0,Math.PI/5.0)) ;
objTrans2.setTransform(t3D);
objTrans2.addChild(new Cylinder(0.5F,1.0F,1,70,70,a)); }
objRoot.compile();
return objRoot;
}
public SimpleAnimation() {
setLayout(new BorderLayout());
GraphicsConfiguration config;
config = SimpleUniverse.getPreferredConfiguration();
Canvas3D c = new Canvas3D(config);
add("Center",c);
SimpleUniverse u = new SimpleUniverse(c);
BranchGroup scene = createSceneGraph(u);
u.getViewingPlatform().setNominalViewingTransform();
u.addBranchGraph(scene);
}
public static void main(String[] args) {
new MainFrame(new SimpleAnimation(),300,250);
}
}
Remarques,erreurs
nicolas.janey@univ-fcomte.fr