Gestion d'un Switch

</COMMENT> alt="Your browser understands the &lt;APPLET&gt; tag but isn't running the applet, for some reason." Your browser is completely ignoring the &lt;APPLET&gt; tag!

RETOUR

SimpleSwitch.java

/* 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 com.sun.j3d.utils.behaviors.mouse.*;
import javax.media.j3d.*;
import javax.vecmath.*;

public class SimpleSwitch extends Applet implements ActionListener {

  private Button b = new Button("        Cube         ");
  private int aff = 0 ;
  private String [] mess = new String[4]; 
  private Switch sw ;

  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 objTrans = new TransformGroup();
    objRoot.addChild(objTrans);
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    objTrans.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
    MouseRotate behavior = new MouseRotate(objTrans);
    objTrans.addChild(behavior);
    behavior.setSchedulingBounds(bounds);
    sw = new Switch();
    sw.setCapability(Switch.ALLOW_SWITCH_WRITE);
    sw.setWhichChild(0);
    objTrans.addChild(sw);
    { 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);
      sw.addChild(new Box(0.25F,0.38F,0.58F,a)); }
    { Appearance a = new Appearance();
      Material m = new Material();
      a.setMaterial(m);
      sw.addChild(new Sphere(0.68F,1,100,a)); }
    { 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);
      sw.addChild(new Cylinder(0.5F,0.9F,1,100,100,a)); }
    { 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);
      sw.addChild(new Cone(0.55F,0.95F,1,100,100,a)); }
    objRoot.compile();
    return objRoot;
    }

  public SimpleSwitch() {
    mess[0]="        Cube         ";
    mess[1]="       Sphere        ";
    mess[2]="      Cylindre       ";
    mess[3]="        Cone         ";
    setLayout(new BorderLayout());
    GraphicsConfiguration config;
    config = SimpleUniverse.getPreferredConfiguration();
    Canvas3D c = new Canvas3D(config);
    Container ct = new Container();
    ct.setLayout(new FlowLayout());
    b.addActionListener(this);
    add("Center",c);
    ct.add(b);
    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() == b ) {
        aff = (aff+1)%4;
        switch ( aff ) {
          case 0: 
          case 1:
          case 2:
          case 3: sw.setWhichChild(aff);
                  break;}
        b.setLabel(mess[aff]);}
    }

  public static void main(String[] args) {
    new MainFrame(new SimpleSwitch(),250,250);
    }
  }

RETOUR

Remarques,erreurs
nicolas.janey@univ-fcomte.fr