13.3 BEHAVIORS
The Java 3D model for detecting and reacting to system events is based on the Behavior
class, which is a scene graph leaf node. It forms the basis for detecting object inter-actions,
such as collisions, and for performing automatically generated actions, such
as animations. It can also be used for accepting user inputs, such as from the mouse
and keyboard.
13.3.1 The behavior model
In concept, the behavior model is quite simple. When a Behavior object becomes
live in the scene graph, its initialize method is called, and when an event
occurs, the Java 3D scheduler calls the object's processStimulus method. To per-form
an action or interaction, you extend the Behavior class or one of its derivatives,
and override the initialize and processStimulus methods. Sounds easy,
right? Of course the devil and a whole lot more are in the details.
Activation
For a behavior to fire, two conditions must be met: The behavior must be active and
its wakeup conditions must be satisfied. You may remember from the earlier discussion
on node bounds that one type was called the scheduling bounds. This is the kind
of bounds that is associated with Behavior nodes. A behavior is only active if the activation
volume associated with a ViewingPlatform object intersects its scheduling
bounds. The intent of this spatial enabling scheme is to conserve system resources
and thereby improve performance by enabling behaviors only when they are needed,
which is presumably when the view is around to see them. If you want the behavior
to be active all the time, construct a spherical scheduling bounds with an infinite
radius. If you want all behaviors to be active all the time, set the view's activation volume
to infinite radius. Because the default bounds for a behavior is null, you have to
set it for the behavior to work.
Wakeup
An active behavior means only that it is receptive to receiving stimuli from the system.
For the behavior to fire, its wakeup criteria must be met, which are set with its
protected wakeupOn method. Behavior firing is a one-shot process. Setting the
wakeup conditions cocks the trigger. When all the wakeup criteria are met, the system
pulls the trigger by calling the processStimulus method, and the behavior is
expended. A behavior can't fire unless wakeup conditions have first been set, which is
typically done as part of the initialize method; and it won't fire again unless you
re-cock its trigger by setting its wakeup conditions again, as part of the process-Stimulus
method. Although this seems complicated, it is actually quite easy to do
and allows the behavior to change its wakeup criteria between firings.
Conditions
Specifying a behavior's wakeup criteria and checking them in the process-Stimulus
method can be confusing to the beginner because of the somewhat
ambiguously named classes involved and the possibly nested data structures defining
the criteria. Wakeup conditions are set using a WakeupCondition object in
the wakeupOn method. Subclasses of WakeupCondition include the WakeupCriterion
class and several other classes for building boolean combinations of conditions,
such as the WakeupAnd and WakeupOrOfAnds class. This is a classic use of the
composite pattern, with the boolean condition classes serving as group nodes in a
condition tree, and the criteria subclasses serving as the leaves. When the behavior
fires, its processStimulus method is called with an enumeration object listing
the wakeup criteria that caused the triggering. Some criteria subclasses, such as
Criteria
Java 3D provides quite a wide variety of wakeup criteria from which to choose,
including behavior activation and deactivation, AWT events, collision between
objects, a transform change, elapsed time, elapsed number of display frames, a manual
event posting, and more. AWT events, collisions, and event posting will be covered
in detail in the following sections.
The breadth and depth of behaviors and their wakeup criteria offer a lot of potentially
useful functionality that can be employed in 3D UI implementation. Unfortunately,
at the time of this writing, serious bugs in a number of the behavior criteria,
especially the collision and posting criteria, prevent their widespread use in the UI
framework (and the need for a few rather unsightly hacks). Some problems are
straightforward, and, thus fixes are promised in the next release; but others are more
subtle. One in particular is the synchronization of behavior event handling and the
rendering of any resulting scene graph changes, which will be discussed in a later section.
Nevertheless, once the kinks are worked out, behaviors should play a major role
in Java 3D UI development.
13.3.2 User inputs
One way to handle mouse and keyboard input is through the Java 3D event model,
using behaviors and the WakeupOnAWTEvent criterion class. Another way to handle
it is through the AWT event model, by creating AWT event listeners and registering
them with a view's Canvas3D display, which is a subclass of an AWT Canvas. Going
the AWT event route may be a lot more familiar if you've done any Java UI programming,
and it is also simpler to implement. Using the AWT event model to handle
events that will likely affect the scene graph may not seem quite legitimate, but the
latest word from Sun is that it poses no problems (and in practice, it seems to be less
prone to bugs).
The UI framework uses mostly behaviors for its mouse and keyboard input sensors,
which can be found in the package j3dui. control. inputs. sensors. This
was a result of early concern about event synchronization. An example of using the
AWT event model can be found in the j3dui. control. inputs. sensors. AwtKeyboard-ArrowSensor
class, which was forced out of necessity due to a minor bug in how key
repeats are handled by behaviors.
If you do decide to use behaviors, keep in mind that the scheduling bounds size
is ignored and is assumed to be infinite. This is an undocumented but confirmed feature.
Thus, if you have two separate views, each with its own behaviors for display
interaction, all behaviors associated will both displays will wake up when display inter-action
from a particular device type— mouse or keyboard— occurs. This is true
regardless of the spatial separation of the views and no matter how small the scheduling
bounds are, as long as it is non-zero.
13.3.3 Interpolators
Automatically generated actions that occur over time, such as animations, are per-formed
using the Interpolator subclass of Behavior, which takes a single parameter in
its constructor, an Alpha object. Animations are useful in UI feedback to show transitions
and to draw the user's attention. Each interpolator is capable of controlling a
specific kind of target object in a particular way, including color, transparency, position,
rotation, scale, and others. For example, the position, rotation, and scale interpolators
all target a TransformGroup object, but each controls its Transform3D in a
particular manner. The Alpha object specifies how time is translated into a changing
and possibly repeating value between 0.0 and 1.0, which all interpolators use to drive
their target object from one action extreme to another, such as from 90 degrees to
180 degrees of rotation.
You can think of an interpolator as a control actuator whose input is time, and
only time. Interpolators do some nifty things and make it really easy to do constrained
spatial transforms and other actions. It's too bad that Alpha wasn't defined as an inter-face;
then, an interpolator could be used as a general purpose control actuator that
could be controlled by time, programmatic input, or even user interaction. This was
a major reason why the framework developed its own model for control actuators,
which is described in the next part of the book and embodied in the j3dui. control.
actuators package.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|