Exemple de classe
utilisant Java3D
/* 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.geometry.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.behaviors.mouse.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import javax.media.j3d.GeometryStripArray.*;
import java.awt.event.*;
public class GrandeOurse3DJava3D extends Applet implements MouseListener,MouseMotionListener {
private Canvas3D c;
private float dz = -8.0F;
private Vector3d eta = new Vector3d( 2.6782868 , 0.95249915 ,-10.211735+8);
private Vector3d miz = new Vector3d( 1.3972227 , 0.11056229 , -7.9981213+8);
private Vector3d dze = new Vector3d( 1.3814067 , 0.11733321 , -8.1529656+8);
private Vector3d eps = new Vector3d( 0.70122137, 0.080953359, -7.6173640+8);
private Vector3d del = new Vector3d( 0.0 , 0.0 , -8.04+8);
private Vector3d gam = new Vector3d(-0.8883190 , 0.82615963 ,-15.251831+8);
private Vector3d alp = new Vector3d(-1.4533803 ,-1.0239428 , -9.6983930+8);
private Vector3d bet = new Vector3d(-1.2871224 ,-0.10782208 , -7.1645021+8);
private Point3d [] pts = new Point3d[14];
private Color3f [] couls = new Color3f[14];
private LineArray la;
public BranchGroup createSceneGraph(SimpleUniverse u) {
BranchGroup objRoot = new BranchGroup();
BoundingSphere largeBounds;
largeBounds = new BoundingSphere(new Point3d(0.0,0.0,0.0),100.0);
BoundingSphere smallBounds;
smallBounds = new BoundingSphere(new Point3d(0.0,0.0,0.0),0.4);
{ 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(largeBounds);
objRoot.addChild(dl); }
{ Color3f lcoulal = new Color3f(0.2F,0.2F,0.1F);
AmbientLight al = new AmbientLight(lcoulal);
al.setInfluencingBounds(largeBounds);
objRoot.addChild(al); }
TransformGroup objTrans = new TransformGroup();
objRoot.addChild(objTrans);
Transform3D t = new Transform3D();
t.setTranslation(new Vector3d(0.0F,0.0F,-8.0F)) ;
objTrans.setTransform(t);
TransformGroup objTr2 = new TransformGroup();
objTrans.addChild(objTr2);
Transform3D tt = new Transform3D();
objTr2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objTr2.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
objTr2.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
objTr2.setTransform(tt);
Appearance a = new Appearance();
Material m = new Material();
m.setDiffuseColor(1.0f,1.0f,1.0f);
m.setSpecularColor(1.0f,1.0f,1.0f);
m.setAmbientColor(1.0f,1.0f,1.0f);
m.setShininess(25.0f);
a.setMaterial(m);
{ TransformGroup objTran = new TransformGroup();
objTr2.addChild(objTran);
Transform3D t1 = new Transform3D();
t1.setTranslation(eta) ;
objTran.setTransform(t1);
objTran.addChild(new Sphere(0.12F,a)); }
{ TransformGroup objTran = new TransformGroup();
objTr2.addChild(objTran);
Transform3D t1 = new Transform3D();
t1.setTranslation(miz) ;
objTran.setTransform(t1);
objTran.addChild(new Sphere(0.12F,a)); }
{ TransformGroup objTran = new TransformGroup();
objTr2.addChild(objTran);
Transform3D t1 = new Transform3D();
t1.setTranslation(dze) ;
objTran.setTransform(t1);
objTran.addChild(new Sphere(0.12F,a)); }
{ TransformGroup objTran = new TransformGroup();
objTr2.addChild(objTran);
Transform3D t1 = new Transform3D();
t1.setTranslation(eps) ;
objTran.setTransform(t1);
objTran.addChild(new Sphere(0.12F,a)); }
{ TransformGroup objTran = new TransformGroup();
objTr2.addChild(objTran);
Transform3D t1 = new Transform3D();
t1.setTranslation(del) ;
objTran.setTransform(t1);
objTran.addChild(new Sphere(0.12F,a)); }
{ TransformGroup objTran = new TransformGroup();
objTr2.addChild(objTran);
Transform3D t1 = new Transform3D();
t1.setTranslation(gam) ;
objTran.setTransform(t1);
objTran.addChild(new Sphere(0.12F,a)); }
{ TransformGroup objTran = new TransformGroup();
objTr2.addChild(objTran);
Transform3D t1 = new Transform3D();
t1.setTranslation(alp) ;
objTran.setTransform(t1);
objTran.addChild(new Sphere(0.12F,a)); }
{ TransformGroup objTran = new TransformGroup();
objTr2.addChild(objTran);
Transform3D t1 = new Transform3D();
t1.setTranslation(bet) ;
objTran.setTransform(t1);
objTran.addChild(new Sphere(0.12F,a)); }
{ pts[0] = new Point3d( 2.6782868 , 0.95249915 ,-10.211735+8);
pts[1] = new Point3d( 1.3814067 , 0.11733321 , -8.1529656+8);
pts[2] = new Point3d( 1.3814067 , 0.11733321 , -8.1529656+8);
pts[3] = new Point3d( 0.70122137, 0.080953359, -7.6173640+8);
pts[4] = new Point3d( 0.70122137, 0.080953359, -7.6173640+8);
pts[5] = new Point3d( 0.0 , 0.0 , -8.04+8);
pts[6] = new Point3d( 0.0 , 0.0 , -8.04+8);
pts[7] = new Point3d(-0.8883190 , 0.82615963 ,-15.251831+8);
pts[8] = new Point3d( 0.0 , 0.0 , -8.04+8);
pts[9] = new Point3d(-1.4533803 ,-1.0239428 , -9.6983930+8);
pts[10] = new Point3d(-1.4533803 ,-1.0239428 , -9.6983930+8);
pts[11] = new Point3d(-1.2871224 ,-0.10782208 , -7.1645021+8);
pts[12] = new Point3d(-1.2871224 ,-0.10782208 , -7.1645021+8);
pts[13] = new Point3d(-0.8883190 , 0.82615963 ,-15.251831+8);
couls[0] = new Color3f(1.0f,1.0f,1.0f);
couls[1] = new Color3f(1.0f,1.0f,1.0f);
couls[2] = new Color3f(1.0f,1.0f,1.0f);
couls[3] = new Color3f(1.0f,1.0f,1.0f);
couls[4] = new Color3f(1.0f,1.0f,1.0f);
couls[5] = new Color3f(1.0f,1.0f,1.0f);
couls[6] = new Color3f(1.0f,1.0f,1.0f);
couls[7] = new Color3f(1.0f,1.0f,1.0f);
couls[8] = new Color3f(1.0f,1.0f,1.0f);
couls[9] = new Color3f(1.0f,1.0f,1.0f);
couls[10] = new Color3f(1.0f,1.0f,1.0f);
couls[11] = new Color3f(1.0f,1.0f,1.0f);
couls[12] = new Color3f(1.0f,1.0f,1.0f);
couls[13] = new Color3f(1.0f,1.0f,1.0f);
la = new LineArray(14,LineArray.COORDINATES|LineArray.COLOR_3);
la.setCoordinates(0,pts);
la.setColors(0,couls);
Shape3D shape = new Shape3D(la,a);
shape.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);
objTr2.addChild(shape); }
BoundingSphere bounds;
bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0),100.0);
MouseRotate behavior = new MouseRotate(objTr2);
objTr2.addChild(behavior);
behavior.setSchedulingBounds(bounds);
MouseZoom behavior2 = new MouseZoom(objTr2);
objTr2.addChild(behavior2);
behavior2.setSchedulingBounds(bounds);
objRoot.compile();
return objRoot;
}
public GrandeOurse3DJava3D() {
setLayout(new BorderLayout());
GraphicsConfiguration config =
SimpleUniverse.getPreferredConfiguration();
c = new Canvas3D(config);
add("Center",c);
SimpleUniverse u = new SimpleUniverse(c);
BranchGroup scene = createSceneGraph(u);
u.getViewingPlatform().setNominalViewingTransform();
u.addBranchGraph(scene);
}
public void mouseDragged(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
int x = e.getX() ;
int y = e.getY() ;
System.out.println(x+" "+y);
PopupMenu pm = new PopupMenu("Hello");
c.add(pm);
pm.show(c,x,y);
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
int x = e.getX() ;
int y = e.getY() ;
System.out.println(x+" "+y);
PopupMenu pm = new PopupMenu("Hello");
c.add(pm);
pm.show(c,x,y);
}
public void mouseReleased(MouseEvent e) {
}
public static void main(String[] args) {
new MainFrame(new GrandeOurse3DJava3D(),256,256);
}
}
Remarques,erreurs
nicolas.janey@univ-fcomte.fr