Behavior Class

All behaviors are potentially time-varying and/or reactive values such as colors, numbers and images. They can be used to construct animated objects. All behavior subclasses inherit the methods of this class.

Because all behaviors are potentially time-varying and/or reactive, their values can change as a function of time or as the result of user input. Time-varying behaviors are created by combining time-based default behaviors (such as localTime) with other behaviors. Reactive behaviors are created using the until, untilEx, and untilNotify methods and specifying input events, such as mouse clicks. Complex behaviors can be built by combining existing behaviors. The value of a complex behavior always depends on the values of the behaviors that comprise it.

Behaviors are started either by the createModel method (which means they start at time = 0) or with the untilNotify.notify method, when they are started at the event time. When a behavior runs, its time-varying and reactive qualities are active. This means the user can experience any changes that occur in the behavior as time passes or events occur.

In order to type-check behaviors, all behavior types must be known at construction time. This is why there is no Behavior.newUninitBvr method included in the Behavior class.

For relevant methods from the Statics class, see Statics Methods Relevant to Behavior Objects.

Behavior Methods

bvrHook Allows a running behavior to be sampled.
debug() Gives the information about a running behavior.
debug(name) Gives information about a running behavior.
debug(name, onStart) Gives information about a running behavior.
debug(name, onStart, std) Gives information about a running behavior.
duration Creates an animation fragment by associating a local "stop time" with a behavior.
extract Extracts the value of the behavior, returning a Java object.
init Initializes a behavior created by the newUninit method.
repeat Creates a behavior that repeats itself the number of times specified by repetitions.
repeatForever Creates a behavior that repeats infinitely.
runOnce Used when applications must reference a running behavior once it starts running, but don't require the application to explicitly start that behavior.
substituteTime Creates a new behavior from an existing Behavior and a NumberBvr.

bvrHook

Behavior Class

Allows a running behavior to be sampled.

Syntax

public Behavior bvrHook( BvrCallback callback );

Parameters

callback
BvrCallback interface containing the notify method.

Return Value

Returns the Behavior object that is the value of the running behavior at the time it was sampled.

See Also

BvrCallback

debug()

Behavior Class

Gives the information about a running behavior.

Syntax

public Behavior debug( );

Remarks

This method gives the following information about a running behavior.

The method assumes the following defaults:

This method is unavailable to viewers that don't display the standard output.

Example

The following example shows that, when it was started, the second instance of a behavior was started at a global time of 39.66 seconds, and had a value of 0.5:

2 started at 39.66 result = 0.5

debug(name)

Behavior Class

Gives information about a running behavior.

Syntax

public Behavior debug( String name );

Parameters

name
Name of the behavior.

Return Value

Returns the Behavior object.

Remarks

This method gives the following information about a running behavior.

The method assumes the following defaults:

This method is unavailable to viewers that don't display the standard output.

Example

The following example shows that, when it was started, the second instance of a behavior called "b" was started at a global time of 39.66 seconds, and had a value of 0.5:

"b" 2 started at 39.66 result = 0.5

The next example displays a solid-colored image whose red and green components are 1, and whose blue component is time-varying. The NumberBvr x initially has a value of "b" (a value that varies over time). When the left button of the mouse is pressed, "b" (and therefore, "x" starts all over again, with a value of 0.5.

The debugging information (shown in the previous example) is displayed each time the left mouse button is pressed.

import com.ms.dxmedia.*;

public class DebugTest extends DXMApplet{
  public DebugTest() {
    // Set the model
    setModel(new DebugTestModel());
  } 
}

class DebugTestModel extends Model {
  public void createModel(BvrsToRun extraBvrsToRun) {
      NumberBvr b = (NumberBvr) add(localTime, toBvr(0.5))
      .Debug("b");

      NumberBvr x = NumberBvr.newUninitBvr();
      x.init(until(b, leftButtonDown, x));
      
      ImageBvr model = solidColorImage(colorRgb(toBvr(1),
      toBvr(1), x));

      setImage(model);
  }
}       

debug(name, onStart)

Behavior Class

Gives information about a running behavior.

Syntax

public Behavior debug( String name, boolean onStart );

Parameters

name
Name of the behavior.
onStart
If true, the debug method is called when the behavior is started. If false, the method is called whenever the behavior is sampled. The default is true.

Return Value

Returns the Behavior object.

Remarks

This method gives the following information about a running behavior.

This method assumes the information is displayed on the standard output.

This method is unavailable to viewers that don't display the standard output.

Example

The following example shows that, when it was started, the second instance of a behavior called "b" was started at a global time of 39.66 seconds, and had a value of 0.5:

"b" 2 started at 39.66 result = 0.5

debug(name, onStart, std)

Behavior Class

Gives information about a running behavior.

Syntax

public Behavior debug(String name, boolean onStart, PrintStream std);

Parameters

name
Name of the behavior.
onStart
If true, the debug method is called when the behavior is started. If false, the method is called whenever the behavior is sampled. The default is true.
std
Output device. The default is the standard output.

Return Value

Returns the Behavior object.

Remarks

This method gives the following information about a running behavior.

This method is unavailable to viewers that don't display the standard output.

Example

The following example shows that, when it was started, the second instance of a behavior called "b" was started at a global time of 39.66 seconds, and had a value of 0.5:

"b" 2 started at 39.66 result = 0.5

duration

Behavior Class

Creates an animation fragment by associating a local "stop time" with a behavior. The result is a new behavior that is the same as the original behavior for the length of the duration. Once the duration is over, the behavior is a snapshot of the behavior's state when the duration ended.

Syntax

public Behavior duration( NumberBvr length )

Parameters

length
NumberBvr object representing the length of time, in seconds, that the duration lasts. This parameter can also be of type double.

Return Value

Returns the Behavior object.

extract

Behavior Class

Extracts the value of the behavior, returning a Java object. This behavior must have a constant value.

Syntax

public Object extract( );

Return Value

Returns a Java object. For more information about java.lang.Object objects, consult a Java reference.

init

Behavior Class

Initializes a behavior created by the newUninit method.

Syntax

public void init( Behavior a )

Parameters

a
Behavior object to be initialized.

repeat

Behavior Class

Creates a behavior that repeats itself the number of times specified by repetitions.

Syntax

public Behavior repeat( int repetitions );

Parameters

repetitions
Number of times the behavior will repeat.

Return Value

Returns the Behavior object.

repeatForever

Behavior Class

Creates a behavior that repeats infinitely.

Syntax

public Behavior repeatForever( );

Return Value

Returns the Behavior object.

runOnce

Behavior Class

Used when applications must reference a running behavior once it starts running, but don't require the application to explicitly start that behavior.

Syntax

public Behavior runOnce( );

Example

The following code fragment plays 2 movies. The first movie plays from the beginning for 10 seconds. It then fades, over a 2 second interval, into a second movie. (Assume the existence of a fade method.)

ImageBvr movie1Once = (ImageBvr)movie1.runOnce();
ImageBvr movie2Once = (ImageBvr)movie2.runOnce();
  until(movie1Once,
   timer(toBvr(10)),
    until(fade(div(localTime, toBvr(2)), movie1Once, movie2Once),
     timer(toBvr(2)),
      movie2Once);

substituteTime

Behavior Class

Creates a new behavior from an existing Behavior and a NumberBvr. In the new behavior, the number behavior replaces all occurrences of localTime in the original behavior. (This includes behaviors where localTime is implicit, such as imported movies. The method can be used even if localTime isn't explicitly in the code.)

Syntax

public Behavior substituteTime( NumberBvr a );

Parameters

a
NumberBvr object that replaces localTime.

Return Value

Returns the Behavior object.

Example

This method allows behaviors to be, for example, time-scaled to run faster or slower, time-shifted to start at a different time, or frozen at a particular point in time. Here are some examples:

//A point moving 1 unit in x/second, starting at 0	
    b0 = point2(localTime, toBvr(0));

    //Create new behavior moving 0.5 units/second, 
    //by replacing localTime with localTime/2
    b1 = origBvr.substituteTime(div(localTime, toBvr(2)));

    //Create new behavior moving 1 unit/second, 
    //starting at 33, by replacing localTime with localTime + 33
    b2 = origBvr.substituteTime(add(localTime, toBvr(33));

    //Create new behavior moving 2 units/second, starting at 33, 
    //by replacing localTime with (localTime *2) + 33
    b3 = origBvr.substituteTime
    (add(mul(localTime, toBvr(2)), toBvr(33));

    //Freeze the original behavior at time 77
    b4 = origBvr.substituteTime(toBvr(77));

    //Tie the new behavior to the x-component of the mouse
    b5 = origBvr.substituteTime(mousePosition.getX());

    //Show that replacements are cumulative.
    // The following sequence:
    c0 = point2(localTime, toBvr(0));
    c1 = c0.substituteTime(add(localTime, toBvr(33)));
    c2 = c1.substituteTime(mul(localTime, toBvr(2)));

    //is equivalent to:
    c0.substituteTime(add(mul(localTime, toBvr(2))), toBvr(33));

Top of Page Top of Page
© 2000 Microsoft and/or its suppliers. All rights reserved. Terms of Use.