//The interpolator for the movement.
float maxRight = 0.5f;
PositionInterpolator cylMoveR = new PositionInterpolator(cylAlphaR,tgmCyl,
escape,0.0f,maxRight);
BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0),Double.MAX_VALUE);
cylMoveR.setSchedulingBounds(bounds);
//The same for the movement from right to left.
Alpha cylAlphaL = new Alpha(1,2000);
cylAlphaL.setStartTime(Long.MAX_VALUE);
PositionInterpolator cylMoveL = new PositionInterpolator(cylAlphaL,tgmCyl,
escape,maxRight,0.0f);
cylMoveL.setSchedulingBounds(bounds);
//Add the movements to the transformation group.
tgmCyl.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
tgmCyl.addChild(cylMoveR);
tgmCyl.addChild(cylMoveL);
//A Switch for the green and the red sphere.
Switch colourSwitch = new Switch();
colourSwitch.setCapability(Switch.ALLOW_SWITCH_WRITE);
//An Appearance for the green sphere.
Color3f ambientColourGSphere = new Color3f(0.0f,0.8f,0.0f);
Color3f emissiveColourGSphere = new Color3f(0.0f,0.0f,0.0f);
Color3f diffuseColourGSphere = new Color3f(0.0f,0.8f,0.0f);
Color3f specularColourGSphere = new Color3f(0.0f,0.8f,0.0f);
float shininessGSphere = 1.0f;
Appearance greenSphereApp = new Appearance();
greenSphereApp.setMaterial(new Material(ambientColourGSphere,
emissiveColourGSphere,
diffuseColourGSphere,
specularColourGSphere,
shininessGSphere));
//Generate the green sphere.
float radius = 0.1f;
Sphere greenSphere = new Sphere(radius,greenSphereApp);
//The same for the red sphere.
Color3f ambientColourRSphere = new Color3f(0.6f,0.0f,0.0f);
Color3f emissiveColourRSphere = new Color3f(0.0f,0.0f,0.0f);
Color3f diffuseColourRSphere = new Color3f(0.6f,0.0f,0.0f);
Color3f specularColourRSphere = new Color3f(0.8f,0.0f,0.0f);
float shininessRSphere = 20.0f;
Appearance redSphereApp = new Appearance();
redSphereApp.setMaterial(new Material(ambientColourRSphere,emissiveColourRSphere,
diffuseColourRSphere,specularColourRSphere,shininessRSphere));
Sphere redSphere = new Sphere(radius,redSphereApp);
//Add the two spheres to the Switch.
colourSwitch.addChild(greenSphere);
colourSwitch.addChild(redSphere);
//The CollisionBounds for the two spheres.
colourSwitch.setCollisionBounds(new BoundingSphere(new Point3d(0.0,0.0,0.0),radius));
//Enable the test for collision.
colourSwitch.setCollidable(true);
//The green sphere should be visible in the beginning.