The Behavior class is an abstract class that provides the mechanism to include code to change the scene graph. The Behavior class, and its descendants, are links to user code providing changes to the graphics and sounds of the virtual universe.

The recipe for writing custom behavior classes is listed here:

  1. write (at least one) constructor
    store a reference to the object of change
  2. override public void initialization()
    specify initial wakeup criteria (trigger)
  3. override public void processStimulus()
    decode the trigger condition
    act according to the trigger condition
    reset trigger as appropriate

To illustrate the recipe a program is written here.the program will implement a simple behavior of making something rotate in response to a keyboard key press.

 public class SimpleBehavior extends Behavior {
  private TransformGroup targetTG;
  private Transform3D rotation = new Transform3D();

  private double angle = 0.0;

  public SimpleBehavior(TransformGroup targetTG) {
    this.targetTG = targetTG;

   * initialize the Behavior
   * set initial wakeup condition called when behavior 
   * beacomes live
  public void initialize() {
    // set initial wakeup condition
    this.wakeupOn(new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED));

   * behave
   * called by Java 3D when appropriate stimulus occures
  public void processStimulus(Enumeration criteria) {
    // decode event
    // do what is necessary
    angle += 0.1;
    this.wakeupOn(new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED));