001: /*
002: * $RCSfile: BoundsViewer.java,v $
003: *
004: * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * - Redistribution of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * - Redistribution in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * Neither the name of Sun Microsystems, Inc. or the names of
019: * contributors may be used to endorse or promote products derived
020: * from this software without specific prior written permission.
021: *
022: * This software is provided "AS IS," without a warranty of any
023: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
024: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
025: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
026: * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
027: * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
028: * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
029: * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
030: * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
031: * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
032: * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
033: * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
034: * POSSIBILITY OF SUCH DAMAGES.
035: *
036: * You acknowledge that this software is not designed, licensed or
037: * intended for use in the design, construction, operation or
038: * maintenance of any nuclear facility.
039: *
040: * $Revision: 1.2 $
041: * $Date: 2007/02/09 17:16:59 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.boundsviewer;
046:
047: import java.applet.Applet;
048: import java.awt.BorderLayout;
049: import java.awt.event.*;
050: import java.awt.GraphicsConfiguration;
051: import com.sun.j3d.utils.applet.MainFrame;
052: import com.sun.j3d.utils.geometry.ColorCube;
053: import com.sun.j3d.utils.universe.*;
054: import javax.media.j3d.*;
055: import javax.vecmath.*;
056:
057: import org.jdesktop.j3d.utils.scenegraph.visualtools.ShowBoundsBehavior;
058:
059: public class BoundsViewer extends Applet {
060:
061: private SimpleUniverse u = null;
062:
063: public BranchGroup createSceneGraph() {
064: // Create the root of the branch graph
065: BranchGroup objRoot = new BranchGroup();
066:
067: // Create the TransformGroup node and initialize it to the
068: // identity. Enable the TRANSFORM_WRITE capability so that
069: // our behavior code can modify it at run time. Add it to
070: // the root of the subgraph.
071: TransformGroup objTrans = new TransformGroup();
072: objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
073: objRoot.addChild(objTrans);
074:
075: BranchGroup cubeSet = new BranchGroup();
076:
077: // Create a simple Shape3D node; add it to the scene graph.
078: ColorCube cube = new ColorCube(0.4);
079: cubeSet.addChild(cube);
080:
081: Transform3D t3d = new Transform3D();
082: t3d.set(1.0, new Vector3d(0.65, 0.0, 0.0));
083: TransformGroup cubePos = new TransformGroup(t3d);
084: cubePos.addChild(new ColorCube(0.2));
085: cubeSet.addChild(cubePos);
086:
087: objTrans.addChild(cubeSet);
088:
089: // Create a new Behavior object that will perform the
090: // desired operation on the specified transform and add
091: // it into the scene graph.
092: Transform3D yAxis = new Transform3D();
093: Alpha rotationAlpha = new Alpha(-1, 4000);
094:
095: RotationInterpolator rotator = new RotationInterpolator(
096: rotationAlpha, objTrans, yAxis, 0.0f,
097: (float) Math.PI * 2.0f);
098: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
099: 0.0, 0.0), 100.0);
100: rotator.setSchedulingBounds(bounds);
101: objRoot.addChild(rotator);
102:
103: // Create the Bounds Viewer and register the object
104: // whos bounds we want to view
105: BranchGroup toolGroup = new BranchGroup();
106: objRoot.addChild(toolGroup);
107: ShowBoundsBehavior showBounds = new ShowBoundsBehavior(
108: toolGroup);
109: showBounds.showBounds(cubeSet);
110:
111: // Have Java 3D perform optimizations on this scene graph.
112: objRoot.compile();
113:
114: return objRoot;
115: }
116:
117: public BoundsViewer() {
118: }
119:
120: public void init() {
121: setLayout(new BorderLayout());
122: GraphicsConfiguration config = SimpleUniverse
123: .getPreferredConfiguration();
124:
125: Canvas3D c = new Canvas3D(config);
126: add("Center", c);
127:
128: // Create a simple scene and attach it to the virtual universe
129: BranchGroup scene = createSceneGraph();
130: u = new SimpleUniverse(c);
131:
132: // This will move the ViewPlatform back a bit so the
133: // objects in the scene can be viewed.
134: u.getViewingPlatform().setNominalViewingTransform();
135:
136: u.addBranchGraph(scene);
137: }
138:
139: public void destroy() {
140: u.cleanup();
141: }
142:
143: //
144: // The following allows BoundsViewer to be run as an application
145: // as well as an applet
146: //
147: public static void main(String[] args) {
148: new MainFrame(new BoundsViewer(), 256, 256);
149: }
150: }
|