>MIME-Version: 1.0
>Date: Mon, 29 Jan 2001 19:03:58 -0600
>From: Altaher Man-QA5778 <Ma'[EMAIL PROTECTED]>
>Subject: [JAVA3D] WakeUpOnTransformChange
>To: [EMAIL PROTECTED]
>
>Anyone has sample code showing how to use WakeUpOnTransformChange ?
>
>Thx,
>
>Ma'n.
>
Try this...
- Kelvin
---------------
Java 3D Team
Sun Microsystems Inc.
/*
* "@(#)TransformChangeBehavior.java 1.1 99/10/28"
*
* Copyright (c) 1996-1999 Sun Microsystems, Inc. All Rights Reserved.
*
* Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
* modify and redistribute this software in source and binary code form,
* provided that i) this copyright notice and license appear on all copies of
* the software; and ii) Licensee does not utilize the software in a manner
* which is disparaging to Sun.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
* IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
* NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
* LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
* OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
* LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
* INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
* CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
* OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGES.
*
* This software is not designed or intended for use in on-line control of
* aircraft, air traffic, aircraft navigation or aircraft communications; or in
* the design, construction, operation or maintenance of any nuclear
* facility. Licensee represents and warrants that it will not use or
* redistribute the Software for such purposes.
*/
import java.awt.event.*;
import java.util.Enumeration;
import javax.media.j3d.*;
/**
* This class is a simple behavior that invokes the perform Click Node
*/
public class TransformChangeBehavior extends Behavior {
private WakeupOnTransformChange w;
private TransformGroup target;
private Transform3D trans = new Transform3D();
public TransformChangeBehavior(TransformGroup triggeredTransform,
TransformGroup targetTransform) {
w = new WakeupOnTransformChange(triggeredTransform);
target = targetTransform;
}
/**
* Override Behavior's initialize method to setup wakeup criteria.
*/
public void initialize() {
wakeupOn(w);
}
/**
* Override Behavior's stimulus method to handle the event.
*/
public void processStimulus(Enumeration criteria) {
w.getTransformGroup().getTransform(trans);
target.setTransform(trans);
wakeupOn(w);
}
}
/*
* "@(#)WakeupOnTransformTest.java 1.1 99/10/28"
*
* Copyright (c) 1996-1999 Sun Microsystems, Inc. All Rights Reserved.
*
* Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
* modify and redistribute this software in source and binary code form,
* provided that i) this copyright notice and license appear on all copies of
* the software; and ii) Licensee does not utilize the software in a manner
* which is disparaging to Sun.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
* IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
* NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
* LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
* OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
* LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
* INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
* CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
* OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGES.
*
* This software is not designed or intended for use in on-line control of
* aircraft, air traffic, aircraft navigation or aircraft communications; or in
* the design, construction, operation or maintenance of any nuclear
* facility. Licensee represents and warrants that it will not use or
* redistribute the Software for such purposes.
*/
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
public class WakeupOnTransformTest extends JApplet {
TransformGroup coneTransformGroup;
public BranchGroup createSceneGraph(boolean isCone) {
// Create the root of the branch graph
BranchGroup objRoot = new BranchGroup();
BoundingSphere bounds =
new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
// Create the transform group node and initialize it to the
// identity. Enable the TRANSFORM_WRITE capability so that
// our behavior code can modify it at runtime. Add it to the
// root of the subgraph.
TransformGroup objTrans = new TransformGroup();
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
objRoot.addChild(objTrans);
// Create a simple shape leaf node, add it to the scene graph.
Appearance ap = new Appearance();
Color3f aColor = new Color3f(0.1f, 0.1f, 0.1f);
Color3f eColor = new Color3f(0.0f, 0.0f, 0.0f);
Color3f dColor;
if (isCone) {
dColor = new Color3f(0.2f, 0.7f, 0.8f);
} else {
dColor = new Color3f(0.7f, 0.1f, 0.4f);
}
Color3f sColor = new Color3f(1.0f, 1.0f, 0.0f);
Material m = new Material(aColor, eColor, dColor, sColor, 80.0f);
m.setLightingEnable(true);
ap.setMaterial(m);
if (isCone) {
Cone coneObj = new Cone(0.4f, 0.8f, Cone.GENERATE_NORMALS |
Cone.GENERATE_TEXTURE_COORDS, ap);
Transform3D xAxis = new Transform3D();
xAxis.rotX(0.5);
TransformGroup trans = new TransformGroup(xAxis);
objTrans.addChild(trans);
trans.addChild(coneObj);
// Create a new Behavior object that will perform the desired
// operation on the specified transform object and add it into
// the scene graph.
Transform3D yAxis = new Transform3D();
Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE,
0, 0,
4000, 0, 0,
0, 0, 0);
RotationInterpolator rotator =
new RotationInterpolator(rotationAlpha, objTrans, yAxis,
0.0f, (float) Math.PI*2.0f);
rotator.setSchedulingBounds(bounds);
objTrans.addChild(rotator);
coneTransformGroup = objTrans;
} else {
Sphere sphere = new Sphere(0.6f, ap);
Transform3D scale = new Transform3D();
scale.setScale(new Vector3d(0.2, 1.0, 0.8));
TransformGroup trans = new TransformGroup(scale);
objTrans.addChild(trans);
Behavior behav = new TransformChangeBehavior(
coneTransformGroup, objTrans);
objTrans.addChild(behav);
behav.setSchedulingBounds(bounds);
trans.addChild(sphere);
}
//Shine it with two lights.
Color3f lColor1 = new Color3f(0.7f, 0.7f, 0.7f);
Color3f lColor2 = new Color3f(0.2f, 0.2f, 0.1f);
Vector3f lDir1 = new Vector3f(-1.0f, -1.0f, -1.0f);
Vector3f lDir2 = new Vector3f(1.0f, 1.0f, 1.0f);
DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1);
DirectionalLight lgt2 = new DirectionalLight(lColor2, lDir2);
lgt1.setInfluencingBounds(bounds);
lgt2.setInfluencingBounds(bounds);
objRoot.addChild(lgt1);
objRoot.addChild(lgt2);
// Have Java 3D perform optimizations on this scene graph.
// objRoot.compile();
return objRoot;
}
public WakeupOnTransformTest() {
Container contentPane = getContentPane();
JPanel canvasPane = new JPanel();
canvasPane.setLayout(new GridLayout(0, 2));
GraphicsConfiguration config =
SimpleUniverse.getPreferredConfiguration();
Canvas3D c1 = new Canvas3D(config);
// Create a simple scene and attach it to the virtual universe
BranchGroup scene = createSceneGraph(true);
SimpleUniverse u = new SimpleUniverse(c1);
scene.compile();
// This will move the ViewPlatform back a bit so the
// objects in the scene can be viewed.
u.getViewingPlatform().setNominalViewingTransform();
u.addBranchGraph(scene);
// Create second Scene graph
scene = createSceneGraph(false);
Canvas3D c2 = new Canvas3D(config);
u = new SimpleUniverse(c2);
u.getViewingPlatform().setNominalViewingTransform();
scene.compile();
u.addBranchGraph(scene);
canvasPane.add(c1);
canvasPane.add(c2);
contentPane.add("Center", canvasPane);
}
//
// The following allows WakeupOnTransformTest to be run as an application
// as well as an applet
//
public static void main(String[] args) {
new MainFrame(new WakeupOnTransformTest(), 768, 384);
}
}