BS Contact J : Java-EAI : Manipulating fields

Here's a little example to demonstrate Field manipulation: We have a simple sphere whose material node we want to manipulate. The diffuseColor of the sphere is to change every 1/3 second.

First, get an instance of the BS Contact J applet. Then connect to the material node of the sphere (which has to be DEFed already), and to its diffuseColor field. Then place a call in the main method of the applet which changes the value of the sphere's diffuseColor field every 1/3 second.

Sorry, your browser doesn't support Java.




view complete source


The start method could look like this:

public void start(){

// connect to blaxxun3d
while (browser==null) {
System.out.println("Got browser.");

// wait till everything is loaded
while (browser.getWorld()==null) {
System.out.println("World loaded.");

// connect to nodes
try {
catch (X3DException notfound) {
System.out.println("Can't find nodes. Aborting.");

System.out.println("Got nodes.");

// main loop
float[] col=new float[3];
while (true) {
// get current color values

// calculate new values
for (int i=0;i<3;i++) col[i]=(float)Math.random();

// sync renderer

// send them to the sphere

// continue rendering

// wait some time

The most important line of code here is the one that sets the diffuseColor field:


The first two arguments contain the start and stop indices of the array to be filled; 0 means that the array should be filled starting at the first index, and -1 means that all of the elements in col should be put in ascending order into the diffuseCol value array. Afterwards, col finally contains all new values to be set. In this particular example,
would lead to the same result since the loop will be repeated three times.

All other fields can be manipulated in the same way; only the method differs: use

nodeArrayField.setValue(startIndex, endIndex, sourceArrayOfNodes)

to manipulate a nodeArray field, for example.