001: /*
002: * $RCSfile: GearTest.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.3 $
041: * $Date: 2007/02/09 17:21:38 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.gears;
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 GearTest extends javax.swing.JFrame {
057:
058: private int toothCount = 24;
059: private SimpleUniverse univ = null;
060: private BranchGroup scene = null;
061:
062: public BranchGroup createSceneGraph() {
063: // Create the root of the branch graph
064: BranchGroup objRoot = new BranchGroup();
065:
066: // Create a Transformgroup to scale all objects so they
067: // appear in the scene.
068: TransformGroup objScale = new TransformGroup();
069: Transform3D t3d = new Transform3D();
070: t3d.setScale(0.4);
071: objScale.setTransform(t3d);
072: objRoot.addChild(objScale);
073:
074: // Create a bounds for the background and lights
075: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
076: 0.0, 0.0), 100.0);
077:
078: // Set up the background
079: Color3f bgColor = new Color3f(0.05f, 0.05f, 0.2f);
080: Background bgNode = new Background(bgColor);
081: bgNode.setApplicationBounds(bounds);
082: objScale.addChild(bgNode);
083:
084: // Set up the global lights
085: Color3f light1Color = new Color3f(1.0f, 1.0f, 0.9f);
086: Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);
087: Color3f light2Color = new Color3f(0.3f, 0.3f, 0.4f);
088: Vector3f light2Direction = new Vector3f(-6.0f, -2.0f, -1.0f);
089: Color3f ambientColor = new Color3f(0.1f, 0.1f, 0.1f);
090:
091: AmbientLight ambientLightNode = new AmbientLight(ambientColor);
092: ambientLightNode.setInfluencingBounds(bounds);
093: objScale.addChild(ambientLightNode);
094:
095: DirectionalLight light1 = new DirectionalLight(light1Color,
096: light1Direction);
097: light1.setInfluencingBounds(bounds);
098: objScale.addChild(light1);
099:
100: DirectionalLight light2 = new DirectionalLight(light2Color,
101: light2Direction);
102: light2.setInfluencingBounds(bounds);
103: objScale.addChild(light2);
104:
105: // Create the transform group node and initialize it to the
106: // identity. Enable the TRANSFORM_WRITE capability so that
107: // our behavior code can modify it at runtime. Add it to the
108: // root of the subgraph.
109: TransformGroup objTrans = new TransformGroup();
110: objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
111: objScale.addChild(objTrans);
112:
113: // Create an Appearance.
114: Appearance look = new Appearance();
115: Color3f objColor = new Color3f(0.5f, 0.5f, 0.6f);
116: Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
117: Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
118: look.setMaterial(new Material(objColor, black, objColor, white,
119: 100.0f));
120:
121: // Create a gear, add it to the scene graph.
122: // SpurGear gear = new SpurGear(toothCount, 1.0f, 0.2f,
123: SpurGear gear = new SpurGearThinBody(toothCount, 1.0f, 0.2f,
124: 0.05f, 0.05f, 0.3f, 0.28f, look);
125: objTrans.addChild(gear);
126:
127: // Create a new Behavior object that will rotate the object and
128: // add it into the scene graph.
129: Transform3D yAxis = new Transform3D();
130: Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0,
131: 0, 8000, 0, 0, 0, 0, 0);
132:
133: RotationInterpolator rotator = new RotationInterpolator(
134: rotationAlpha, objTrans, yAxis, 0.0f,
135: (float) Math.PI * 2.0f);
136: rotator.setSchedulingBounds(bounds);
137: objTrans.addChild(rotator);
138:
139: // Have Java 3D perform optimizations on this scene graph.
140: objRoot.compile();
141:
142: return objRoot;
143: }
144:
145: private Canvas3D createUniverse() {
146: // Get the preferred graphics configuration for the default screen
147: GraphicsConfiguration config = SimpleUniverse
148: .getPreferredConfiguration();
149:
150: // Create a Canvas3D using the preferred configuration
151: Canvas3D c = new Canvas3D(config);
152:
153: // Create simple universe with view branch
154: univ = new SimpleUniverse(c);
155:
156: // This will move the ViewPlatform back a bit so the
157: // objects in the scene can be viewed.
158: univ.getViewingPlatform().setNominalViewingTransform();
159:
160: // Ensure at least 5 msec per frame (i.e., < 200Hz)
161: univ.getViewer().getView().setMinimumFrameCycleTime(5);
162:
163: return c;
164: }
165:
166: /**
167: * Creates new form GearTest
168: */
169: public GearTest(String args[]) {
170: int value;
171:
172: if (args.length > 1) {
173: System.out.println("Usage: java GearTest [#teeth]");
174: System.exit(0);
175: } else if (args.length == 1) {
176: try {
177: value = Integer.parseInt(args[0]);
178: } catch (NumberFormatException e) {
179: System.out.println("Illegal integer specified");
180: System.out.println("Usage: java GearTest [#teeth]");
181: value = 0;
182: System.exit(0);
183: }
184: if (value <= 0) {
185: System.out.println("Integer must be positive (> 0)");
186: System.out.println("Usage: java GearBox [#teeth]");
187: System.exit(0);
188: }
189: toothCount = value;
190:
191: }
192:
193: // Initialize the GUI components
194: initComponents();
195:
196: // Create Canvas3D and SimpleUniverse; add canvas to drawing panel
197: Canvas3D c = createUniverse();
198: drawingPanel.add(c, java.awt.BorderLayout.CENTER);
199:
200: // Create the content branch and add it to the universe
201: scene = createSceneGraph();
202: univ.addBranchGraph(scene);
203: }
204:
205: // ----------------------------------------------------------------
206:
207: /** This method is called from within the constructor to
208: * initialize the form.
209: * WARNING: Do NOT modify this code. The content of this method is
210: * always regenerated by the Form Editor.
211: */
212: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
213: private void initComponents() {
214: drawingPanel = new javax.swing.JPanel();
215:
216: setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
217: setTitle("GearTest");
218: drawingPanel.setLayout(new java.awt.BorderLayout());
219:
220: drawingPanel.setPreferredSize(new java.awt.Dimension(700, 700));
221: getContentPane()
222: .add(drawingPanel, java.awt.BorderLayout.CENTER);
223:
224: pack();
225: }// </editor-fold>//GEN-END:initComponents
226:
227: /**
228: * @param args the command line arguments
229: */
230: public static void main(final String args[]) {
231: java.awt.EventQueue.invokeLater(new Runnable() {
232: public void run() {
233: GearTest gt = new GearTest(args);
234: gt.setVisible(true);
235: }
236: });
237: }
238:
239: // Variables declaration - do not modify//GEN-BEGIN:variables
240: private javax.swing.JPanel drawingPanel;
241: // End of variables declaration//GEN-END:variables
242:
243: }
|