001: /*
002: * $RCSfile: HelloUniverse.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:21:42 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.hello_universe;
046:
047: import com.sun.j3d.utils.universe.*;
048: import com.sun.j3d.utils.geometry.ColorCube;
049: import javax.media.j3d.*;
050: import javax.vecmath.*;
051: import java.awt.GraphicsConfiguration;
052:
053: /**
054: * Simple Java 3D example program to display a spinning cube.
055: */
056: public class HelloUniverse extends javax.swing.JFrame {
057:
058: private SimpleUniverse univ = null;
059: private BranchGroup scene = null;
060:
061: public BranchGroup createSceneGraph() {
062: // Create the root of the branch graph
063: BranchGroup objRoot = new BranchGroup();
064:
065: // Create the TransformGroup node and initialize it to the
066: // identity. Enable the TRANSFORM_WRITE capability so that
067: // our behavior code can modify it at run time. Add it to
068: // the root of the subgraph.
069: TransformGroup objTrans = new TransformGroup();
070: objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
071: objRoot.addChild(objTrans);
072:
073: // Create a simple Shape3D node; add it to the scene graph.
074: objTrans.addChild(new ColorCube(0.4));
075:
076: // Create a new Behavior object that will perform the
077: // desired operation on the specified transform and add
078: // it into the scene graph.
079: Transform3D yAxis = new Transform3D();
080: Alpha rotationAlpha = new Alpha(-1, 4000);
081:
082: RotationInterpolator rotator = new RotationInterpolator(
083: rotationAlpha, objTrans, yAxis, 0.0f,
084: (float) Math.PI * 2.0f);
085: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
086: 0.0, 0.0), 100.0);
087: rotator.setSchedulingBounds(bounds);
088: objRoot.addChild(rotator);
089:
090: // Have Java 3D perform optimizations on this scene graph.
091: objRoot.compile();
092:
093: return objRoot;
094: }
095:
096: private Canvas3D createUniverse() {
097: // Get the preferred graphics configuration for the default screen
098: GraphicsConfiguration config = SimpleUniverse
099: .getPreferredConfiguration();
100:
101: // Create a Canvas3D using the preferred configuration
102: Canvas3D c = new Canvas3D(config);
103:
104: // Create simple universe with view branch
105: univ = new SimpleUniverse(c);
106:
107: // This will move the ViewPlatform back a bit so the
108: // objects in the scene can be viewed.
109: univ.getViewingPlatform().setNominalViewingTransform();
110:
111: // Ensure at least 5 msec per frame (i.e., < 200Hz)
112: univ.getViewer().getView().setMinimumFrameCycleTime(5);
113:
114: return c;
115: }
116:
117: /**
118: * Creates new form HelloUniverse
119: */
120: public HelloUniverse() {
121: // Initialize the GUI components
122: initComponents();
123:
124: // Create Canvas3D and SimpleUniverse; add canvas to drawing panel
125: Canvas3D c = createUniverse();
126: drawingPanel.add(c, java.awt.BorderLayout.CENTER);
127:
128: // Create the content branch and add it to the universe
129: scene = createSceneGraph();
130: univ.addBranchGraph(scene);
131: }
132:
133: // ----------------------------------------------------------------
134:
135: /** This method is called from within the constructor to
136: * initialize the form.
137: * WARNING: Do NOT modify this code. The content of this method is
138: * always regenerated by the Form Editor.
139: */
140: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
141: private void initComponents() {
142: drawingPanel = new javax.swing.JPanel();
143:
144: setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
145: setTitle("HelloUniverse");
146: drawingPanel.setLayout(new java.awt.BorderLayout());
147:
148: drawingPanel.setPreferredSize(new java.awt.Dimension(250, 250));
149: getContentPane()
150: .add(drawingPanel, java.awt.BorderLayout.CENTER);
151:
152: pack();
153: }// </editor-fold>//GEN-END:initComponents
154:
155: /**
156: * @param args the command line arguments
157: */
158: public static void main(String args[]) {
159: java.awt.EventQueue.invokeLater(new Runnable() {
160: public void run() {
161: new HelloUniverse().setVisible(true);
162: }
163: });
164: }
165:
166: // Variables declaration - do not modify//GEN-BEGIN:variables
167: private javax.swing.JPanel drawingPanel;
168: // End of variables declaration//GEN-END:variables
169:
170: }
|