PREVIOUS UP   Technologies Internet et Education, © TECFA
  4. Techniques EAI de base

4. Techniques EAI de base

4.1 Référence vers un noeud VRML et changer la valeur d'un champs

Exemple 4-1: Changer la couleur d'une boule

A. Obtenir la référence

Node material = null;
material = browser.getNode("MAT");

B. Obtenir une référence vers un EventIn

Material { 
  exposedField SFFloat ambientIntensity  0.2         # [0,1]
  exposedField SFColor diffuseColor      0.8 0.8 0.8 # [0,1]
  [ .... ]
  exposedField SFFloat transparency      0           # [0,1]
}
EventInSFColor diffuseColor = null;
diffuseColor = (EventInSFColor) material.getEventIn("set_diffuseColor");

C. Envoyer un évent (écrire dans un champs d'un noeud)

float[] color = new float[3];
......
diffuseColor = (EventInSFColor) material.getEventIn("set_diffuseColor");
......
color[0] = 0.2f; color[1] = 0.2f; color[2] = 0.8f;
diffuseColor.setValue(color);
add(redButton = new Button("Red"));
redButton.addActionListener(this);
......
public void actionPerformed (ActionEvent event) {
   Object clickedButton = event.getSource();
   if (clickedButton == redButton)  { 
       color[0] = 0.8f; color[1] = 0.2f; color[2] = 0.2f;
       diffuseColor.setValue(color);
      }

4.2 Lire la valeur d'un champs VRML

Exemple 4-2: Lire la position d'un viewpoint

Node entryVP = null;
EventOutSFVec3f positionVP = null;
.....
entryVP = browser.getNode("Entry");
positionVP =(EventOutSFVec3f) entryVP.getEventOut("position_changed");
float[] currentPosition;
currentPosition = positionVP.getValue();
// et on fait quelque chose (ici écrire dans un textarea)
output.append("Entry ViewPoint is at" +
              " x=" + currentPosition [0] +
              " y=" + currentPosition [1] +
              " z=" + currentPosition [2] + "\n");

4.3 Create VRML from String et remplacer ou modifier le monde

Exemple 4-3: Créer un "scene graph" et remplacer une scène VRML

 Node[] scene = null;
 scene = browser.createVrmlFromString(
           "DEF Camera Viewpoint {\n" +
                "  position 0 0 5}\n" +
           "DEF MySphere Transform {\n" +
                "   children [ \n" +
            ......
                "     ] \n" +
                " } \n"
                );
browser.replaceWorld(scene);

A. Modifier le monde

Exemple 4-4: Ajouter / enlever un objet dans un grouping node

Group { 
  eventIn      MFNode  addChildren
  eventIn      MFNode  removeChildren
  exposedField MFNode  children      []
  field        SFVec3f bboxCenter    0 0 0     # (-,)
  field        SFVec3f bboxSize      -1 -1 -1  # (0,) or -1,-1,-1
}
Node root = browser.getNode("ROOT");
// EventIns of the root node
EventInMFNode addChildren;
EventInMFNode removeChildren;
// Instantiate (get handle to) the EventIn objects
addChildren = (EventInMFNode) root.getEventIn("addChildren");
removeChildren = (EventInMFNode) root.getEventIn("removeChildren");
if (b == addButton) {
    addChildren.setValue(shape);
   }
else if (b == removeButton) {
   removeChildren.setValue(shape);
  }
// Shape group hierarchy
Node[] shape;
shape = browser.createVrmlFromString("Shape {\n" +
                "  appearance Appearance {\n" +
                "    material Material {\n" +
                "      diffuseColor 0.2 0.2 0.8\n" +
                "    }\n" +
                "  }\n" +
                "  geometry Sphere {}\n" +
                "}\n");

4.4 Recevoir des événements de la scène VRML

Exemple 4-5: Un boule qui change de couleur quand on clicke

A. Handle pour un eventOut et enregistrement

Node sensor = browser.getNode("TOUCH");
// Get its isActive EventOut
isActive = (EventOutSFBool) sensor.getEventOut("isActive");
// Set up the callback
isActive.advise(this, isActive);

B. Le callback

public void callback(EventOut who, double when, Object which) {
  // retrieve the state of isActive (see above the advise function)
  EventOutSFBool state = (EventOutSFBool) which;
  // only deal with the event if isActive was true, else the ball would flip back
 if (state.getValue() == true)  {
     System.out.println("callback(): EventOut=" + who
                        + " state =" + state.getValue());
    // Change the color and remember it
    if (colorState == 1) {
       diffuseColor.setValue(redColor);
       colorState = 0;
       }
    else {
       diffuseColor.setValue(greenColor);
       colorState = 1;
  }   }  }

4.5 EAI avec SQL


PREVIOUS UP -- TIE