La sélection d'objets 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.picking.PickTool;
import com.sun.j3d.utils.picking.behaviors.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
public class SimplePicking extends Applet {
public BranchGroup createSceneGraph(Canvas3D canvas) {
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 objTrans = new TransformGroup();
objRoot.addChild(objTrans);
Transform3D t1 = new Transform3D();
t1.setTranslation(new Vector3d(-0.7,0.0,0.0)) ;
objTrans.setTransform(t1);
TransformGroup objTrans2 = new TransformGroup();
objTrans.addChild(objTrans2);
objTrans2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objTrans2.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
objTrans2.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
Appearance a = new Appearance();
Material m = new Material();
a.setMaterial(m);
Sphere s = new Sphere(0.18F,Sphere.GENERATE_NORMALS|Sphere.ENABLE_GEOMETRY_PICKING,70,a);
s.setCapability(Geometry.ALLOW_INTERSECT);
objTrans2.addChild(s); }
{ TransformGroup objTrans = new TransformGroup();
objRoot.addChild(objTrans);
Transform3D t1 = new Transform3D();
t1.setTranslation(new Vector3d(-0.25,0.0,0.0)) ;
objTrans.setTransform(t1);
TransformGroup objTrans2 = new TransformGroup();
objTrans.addChild(objTrans2);
objTrans2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objTrans2.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
objTrans2.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
Appearance a = new Appearance();
Material m = new Material();
m.setDiffuseColor(0.5f,1.0f,0.2f);
m.setAmbientColor(0.5f,0.6f,0.8f);
a.setMaterial(m);
Cone c = new Cone(0.14F,0.28F,Cone.GENERATE_NORMALS|Cone.ENABLE_GEOMETRY_PICKING,50,50,a);
c.setCapability(Geometry.ALLOW_INTERSECT);
objTrans2.addChild(c); }
{ TransformGroup objTrans = new TransformGroup();
objRoot.addChild(objTrans);
Transform3D t1 = new Transform3D();
t1.setTranslation(new Vector3d(0.25,0.0,0.0)) ;
objTrans.setTransform(t1);
TransformGroup objTrans2 = new TransformGroup();
objTrans.addChild(objTrans2);
objTrans2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objTrans2.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
objTrans2.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
Appearance a = new Appearance();
Material m = new Material();
m.setDiffuseColor(0.1f,0.1f,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);
Cylinder c = new Cylinder(0.14F,0.28F,Cylinder.GENERATE_NORMALS|Cylinder.ENABLE_GEOMETRY_PICKING,50,50,a);
c.setCapability(Geometry.ALLOW_INTERSECT);
objTrans2.addChild(c); }
{ TransformGroup objTrans = new TransformGroup();
objRoot.addChild(objTrans);
Transform3D t1 = new Transform3D();
t1.setTranslation(new Vector3d(0.7,0.0,0.0)) ;
objTrans.setTransform(t1);
TransformGroup objTrans2 = new TransformGroup();
objTrans.addChild(objTrans2);
objTrans2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objTrans2.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
objTrans2.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
Appearance a = new Appearance();
Material m = new Material();
m.setDiffuseColor(0.9f,0.8f,0.4f);
m.setAmbientColor(0.6f,0.0f,0.6f);
a.setMaterial(m);
Box b = new Box(0.05F,0.13F,0.22F,Box.GENERATE_NORMALS|Box.ENABLE_GEOMETRY_PICKING,a);
b.setCapability(Geometry.ALLOW_INTERSECT);
objTrans2.addChild(b); }
PickRotateBehavior behavior1 =
new PickRotateBehavior(objRoot, canvas, bounds);
behavior1.setMode(PickTool.GEOMETRY);
objRoot.addChild(behavior1);
PickZoomBehavior behavior2 =
new PickZoomBehavior(objRoot, canvas, bounds);
behavior2.setMode(PickTool.GEOMETRY);
objRoot.addChild(behavior2);
PickTranslateBehavior behavior3 =
new PickTranslateBehavior(objRoot, canvas, bounds);
behavior3.setMode(PickTool.GEOMETRY);
objRoot.addChild(behavior3);
objRoot.compile();
return objRoot;
}
public SimplePicking() {
setLayout(new BorderLayout());
GraphicsConfiguration config;
config = SimpleUniverse.getPreferredConfiguration();
Canvas3D c = new Canvas3D(config);
add("Center",c);
SimpleUniverse u = new SimpleUniverse(c);
BranchGroup scene = createSceneGraph(c);
u.getViewingPlatform().setNominalViewingTransform();
u.addBranchGraph(scene);
}
public static void main(String[] args) {
new MainFrame(new SimplePicking(),600,150);
}
}
Remarques,erreurs
nicolas.janey@univ-fcomte.fr