Intéraction entre Java3D et l'AWT
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Novembre 2001 */
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
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 SimpleAWTJava3D extends Applet implements ActionListener {
private TextField tfr = new TextField("0.5",5);
private TextField tfv = new TextField("0.6",5);
private TextField tfb = new TextField("0.7",5);
private Material m ;
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);
Appearance a = new Appearance();
m = new Material();
m.setCapability(Material.ALLOW_COMPONENT_WRITE);
float r = new Float(tfr.getText()).floatValue();
float v = new Float(tfv.getText()).floatValue();
float b = new Float(tfb.getText()).floatValue();
m.setDiffuseColor(r,v,b);
a.setMaterial(m);
objRoot.addChild(new Sphere(0.5F,1,100,a));
objRoot.compile();
return objRoot;
}
public SimpleAWTJava3D() {
setLayout(new BorderLayout());
GraphicsConfiguration config;
config = SimpleUniverse.getPreferredConfiguration();
Canvas3D c = new Canvas3D(config);
Container ct = new Container();
ct.setLayout(new FlowLayout());
tfr = new TextField("0.5",5);
tfr.addActionListener(this);
tfv = new TextField("0.6",5);
tfv.addActionListener(this);
tfb = new TextField("0.7",5);
tfb.addActionListener(this);
add("Center",c);
ct.add(tfr);
ct.add(tfv);
ct.add(tfb);
add("South",ct);
SimpleUniverse u = new SimpleUniverse(c);
BranchGroup scene = createSceneGraph(u);
u.getViewingPlatform().setNominalViewingTransform();
u.addBranchGraph(scene);
}
public void actionPerformed(ActionEvent e) {
if ( ( e.getSource() == tfr ) ||
( e.getSource() == tfv ) ||
( e.getSource() == tfb ) ) {
float r = new Float(tfr.getText()).floatValue();
float v = new Float(tfv.getText()).floatValue();
float b = new Float(tfb.getText()).floatValue();
m.setDiffuseColor(r,v,b);}
}
public static void main(String[] args) {
new MainFrame(new SimpleAWTJava3D(),300,250);
}
}
Remarques,erreurs
nicolas.janey@univ-fcomte.fr