BS Contact J : Java-EAI : Realizing a CylinderSensor using the AWT Observer

To receive the user's mouse events, one has to register an AWT observer next. This is done after the connection to the render applet has been established by adding the line
browser.addAWTObserver(this);
after the BS Contact J connection has been established. This means that the applet is registered as an AWT observer to BS Contact J. Whenever an AWT event occurs, it will now be sent to the extension applet and it only has to be distinguished what kind of AWT events are of interest. In this case mouse drag events will be observed.
This is done in the onEvent(java.awt.Event) method, which must be implemented when the EventObserver-interface is used.

Sorry, your browser doesn't support Java.

view complete source


public boolean onEvent(java.awt.Event event) {
switch (event.id) {
case java.awt.Event.MOUSE_DOWN: {
x=event.x;
y=event.y;
break;
}
case java.awt.Event.MOUSE_DRAG: {
update3d(event.x-x,event.y-y);
x=event.x;
y=event.y;
break;
}
}
}

Whenever the mouse is dragged, the update3d method is called. Update3d first retrieves the current orientation of the node by using the method
getValueFloatArray().
Then the new orientation that depends on the user's mouse input is calculated and the newly calculated values are set.

private void update3d(int delta) {
float [] rot=new float[4];

// get current orientation
rot=textrot.getValueFloatArray(0,-1);
rot[3]+=delta/20f;

// sync with renderer
browser.beginUpdate();

// set new orientation
textrot.setValueFloatArray(0,-1,rot);

// continue rendering
browser.endUpdate();

}