In a single scene graph, if I have more than one Behavior
that wakes up on AWT events, its seems that only one Behavior
will wakeup - the one that is encountered first in a traversal.
The other Behaviors are effectively blocked from hearing
about the event they are also interested in.

Is this a bug? Am I doing something wrong? I've appended
a short example below. Only the Behavior labeled "A"
will wakeup (and print out a message).

Its not obvious from the documentation that things should
work this way. I don't like this property. While, I
appreciate that performance would be more efficient if there
was only one Behavior that would wake up for a particular
event, it limits how well scene graph objects can be encapsulated.
I am trying to develop Java3D components that I can mix and
match in any scene - I am trying to give them independent
responsibility for what they do about AWT events.
I'd appreciate any feedback.

Regards,
Mark

p.s.
// testMultAwt.java - demonstrate how one behavior blocks another

import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.SimpleUniverse;
import java.applet.Applet;
import javax.vecmath.Point3d;
import javax.media.j3d.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class testMultAwt extends Applet
{
   public static void main(String[] args)
      { new MainFrame(new testMultAwt(), 200, 200); }

   public testMultAwt()
   {
      // two behaviors, each listening for AWT MousePress
      // - if one wakes up it will print a message that
      //   includes the label passed here to the constructor
      // "The Problem" - only "A" will print out messages
      BranchGroup scene = new BranchGroup();
      scene.addChild(new MouseResponse("A"));
      scene.addChild(new MouseResponse("B"));

      // bare necessities for a live scene graph
      // that includes the above
      Canvas3D canv = new Canvas3D(null);
      SimpleUniverse univ = new SimpleUniverse(canv);
      univ.getViewingPlatform().setNominalViewingTransform();
      univ.addBranchGraph(scene);
      setLayout(new BorderLayout());
      add("Center", canv);
   }

   Bounds bounds = new BoundingSphere(new Point3d(0f,0f,0f), 100f);
   WakeupCondition mousePress = new       WakeupOnAWTEvent(MouseEvent.MOUSE_PRESSED);

   class MouseResponse extends Behavior
   {
      String label;
      long count = 0;

      MouseResponse(String label)
      {
         super();
         this.label = label;
         setSchedulingBounds(bounds);
      }

      public void initialize() { wakeupOn(mousePress); }

      public void processStimulus(Enumeration criteria)
      {
         while (criteria.hasMoreElements()) {
            WakeupCriterion criterion =                
(WakeupCriterion)criteria.nextElement();
            if (criterion instanceof WakeupOnAWTEvent) {
               AWTEvent[] event =                   
((WakeupOnAWTEvent)criterion).getAWTEvent();
               for (int i=0 ; i < event.length ; i++, count++)
                  System.out.println(
                     "AWTEvent(" + label + ")[" + count + "]"
                     + ": " + "event[" + event[i].getID() + "]"
                     + " @ " + ((MouseEvent)event[i]).getX()
                     + "," + ((MouseEvent)event[i]).getY()
                  );
            }
         }
         wakeupOn(mousePress);
      }
   }
}

// end of file
=====================================================================
To subscribe/unsubscribe, send mail to [EMAIL PROTECTED]
Java 3D Home Page: http://java.sun.com/products/java-media/3D/

Reply via email to