java left logo
java middle logo
java right logo
 

Home arrow Other API Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 3941
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
How to create a custom behavior class E-mail
User Rating: / 3
PoorBest 

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;
    rotation.rotY(angle);
    targetTG.setTransform(rotation);
    this.wakeupOn(new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED));
  }
}

 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.