001: /*
002: * $RCSfile: Applet3D.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.5 $
041: * $Date: 2007/08/01 21:37:58 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.applet3d;
046:
047: import com.sun.j3d.utils.geometry.ColorCube;
048: import com.sun.j3d.utils.universe.SimpleUniverse;
049: import java.awt.BorderLayout;
050: import java.awt.Container;
051: import java.awt.GraphicsConfiguration;
052: import java.awt.GraphicsDevice;
053: import java.awt.GraphicsEnvironment;
054: import javax.media.j3d.Alpha;
055: import javax.media.j3d.BoundingSphere;
056: import javax.media.j3d.BranchGroup;
057: import javax.media.j3d.Canvas3D;
058: import javax.media.j3d.GraphicsConfigTemplate3D;
059: import javax.media.j3d.RotationInterpolator;
060: import javax.media.j3d.Transform3D;
061: import javax.media.j3d.TransformGroup;
062: import javax.swing.JApplet;
063: import javax.swing.JFrame;
064: import javax.swing.JPopupMenu;
065: import javax.swing.WindowConstants;
066: import javax.vecmath.Point3d;
067:
068: /**
069: * Simple Java 3D program that can be run as an application or as an applet.
070: */
071: public class Applet3D extends javax.swing.JPanel {
072:
073: private SimpleUniverse univ = null;
074: private BranchGroup scene = null;
075:
076: private Alpha rotationAlpha1;
077: private Alpha rotationAlpha2;
078:
079: public BranchGroup createSceneGraph() {
080: // Create the root of the branch graph
081: BranchGroup objRoot = new BranchGroup();
082:
083: // Create two TransformGroup nodes in series
084: TransformGroup objTrans1 = new TransformGroup();
085: objTrans1.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
086: objRoot.addChild(objTrans1);
087:
088: // Create two TransformGroup nodes in series
089: TransformGroup objTrans2 = new TransformGroup();
090: objTrans2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
091: objTrans1.addChild(objTrans2);
092:
093: // Create a simple Shape3D node; add it to the scene graph.
094: objTrans2.addChild(new ColorCube(0.4));
095:
096: // Create 2 new Behavior objects that will perform the
097: // desired operations on the specified transforms and add
098: // them into the scene graph.
099: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
100: 0.0, 0.0), 100.0);
101:
102: Transform3D yAxis1 = new Transform3D();
103: rotationAlpha1 = new Alpha(-1, 4000);
104:
105: RotationInterpolator rotator1 = new RotationInterpolator(
106: rotationAlpha1, objTrans1, yAxis1, 0.0f,
107: (float) Math.PI * 2.0f);
108: rotator1.setSchedulingBounds(bounds);
109: objRoot.addChild(rotator1);
110:
111: Transform3D yAxis2 = new Transform3D();
112: yAxis2.rotX(Math.PI / 4.0);
113: rotationAlpha2 = new Alpha(-1, 13000);
114:
115: RotationInterpolator rotator2 = new RotationInterpolator(
116: rotationAlpha2, objTrans2, yAxis2, 0.0f,
117: (float) Math.PI * 2.0f);
118: rotator2.setSchedulingBounds(bounds);
119: objRoot.addChild(rotator2);
120:
121: return objRoot;
122: }
123:
124: private Canvas3D createUniverse(Container container) {
125: GraphicsDevice graphicsDevice;
126: if (container.getGraphicsConfiguration() != null) {
127: graphicsDevice = container.getGraphicsConfiguration()
128: .getDevice();
129: } else {
130: graphicsDevice = GraphicsEnvironment
131: .getLocalGraphicsEnvironment()
132: .getDefaultScreenDevice();
133: }
134: GraphicsConfigTemplate3D template = new GraphicsConfigTemplate3D();
135: GraphicsConfiguration config = graphicsDevice
136: .getBestConfiguration(template);
137:
138: Canvas3D c = new Canvas3D(config);
139:
140: univ = new SimpleUniverse(c);
141:
142: // This will move the ViewPlatform back a bit so the
143: // objects in the scene can be viewed.
144: univ.getViewingPlatform().setNominalViewingTransform();
145:
146: // Ensure at least 5 msec per frame (i.e., < 200Hz)
147: univ.getViewer().getView().setMinimumFrameCycleTime(5);
148:
149: return c;
150: }
151:
152: private void destroy() {
153: univ.cleanup();
154: }
155:
156: /**
157: * Creates new form Applet3D
158: */
159: public Applet3D(Container container) {
160: // Initialize the GUI components
161: JPopupMenu.setDefaultLightWeightPopupEnabled(false);
162: initComponents();
163:
164: // Create Canvas3D and SimpleUniverse; add canvas to drawing panel
165: Canvas3D c = createUniverse(container);
166: drawingPanel.add(c, BorderLayout.CENTER);
167:
168: // Create the content branch and add it to the universe
169: scene = createSceneGraph();
170: univ.addBranchGraph(scene);
171: }
172:
173: // ----------------------------------------------------------------
174:
175: // Applet framework
176:
177: public static class MyApplet extends JApplet {
178: Applet3D mainPanel;
179:
180: public void init() {
181: setLayout(new BorderLayout());
182: mainPanel = new Applet3D(this );
183: add(mainPanel, BorderLayout.CENTER);
184: }
185:
186: public void destroy() {
187: mainPanel.destroy();
188: }
189: }
190:
191: // Application framework
192:
193: private static class MyFrame extends JFrame {
194: MyFrame() {
195: setLayout(new BorderLayout());
196: setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
197: setTitle("Simple 3D Applet");
198: getContentPane().add(new Applet3D(this ),
199: BorderLayout.CENTER);
200: pack();
201: }
202: }
203:
204: /**
205: * @param args the command line arguments
206: */
207: public static void main(String args[]) {
208: java.awt.EventQueue.invokeLater(new Runnable() {
209: public void run() {
210: new MyFrame().setVisible(true);
211: }
212: });
213: }
214:
215: /** This method is called from within the constructor to
216: * initialize the form.
217: * WARNING: Do NOT modify this code. The content of this method is
218: * always regenerated by the Form Editor.
219: */
220: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
221: private void initComponents() {
222: java.awt.GridBagConstraints gridBagConstraints;
223:
224: guiPanel = new javax.swing.JPanel();
225: pauseButton = new javax.swing.JButton();
226: resumeButton = new javax.swing.JButton();
227: drawingPanel = new javax.swing.JPanel();
228:
229: setLayout(new java.awt.BorderLayout());
230:
231: guiPanel.setLayout(new java.awt.GridBagLayout());
232:
233: pauseButton.setText("Pause");
234: pauseButton
235: .addActionListener(new java.awt.event.ActionListener() {
236: public void actionPerformed(
237: java.awt.event.ActionEvent evt) {
238: pauseButtonActionPerformed(evt);
239: }
240: });
241: gridBagConstraints = new java.awt.GridBagConstraints();
242: gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
243: guiPanel.add(pauseButton, gridBagConstraints);
244:
245: resumeButton.setText("Resume");
246: resumeButton
247: .addActionListener(new java.awt.event.ActionListener() {
248: public void actionPerformed(
249: java.awt.event.ActionEvent evt) {
250: resumeButtonActionPerformed(evt);
251: }
252: });
253: gridBagConstraints = new java.awt.GridBagConstraints();
254: gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
255: guiPanel.add(resumeButton, gridBagConstraints);
256:
257: add(guiPanel, java.awt.BorderLayout.NORTH);
258:
259: drawingPanel.setPreferredSize(new java.awt.Dimension(500, 500));
260: drawingPanel.setLayout(new java.awt.BorderLayout());
261: add(drawingPanel, java.awt.BorderLayout.CENTER);
262: }// </editor-fold>//GEN-END:initComponents
263:
264: private void resumeButtonActionPerformed(
265: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resumeButtonActionPerformed
266: rotationAlpha1.resume();
267: rotationAlpha2.resume();
268: }//GEN-LAST:event_resumeButtonActionPerformed
269:
270: private void pauseButtonActionPerformed(
271: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pauseButtonActionPerformed
272: rotationAlpha1.pause();
273: rotationAlpha2.pause();
274: }//GEN-LAST:event_pauseButtonActionPerformed
275:
276: // Variables declaration - do not modify//GEN-BEGIN:variables
277: private javax.swing.JPanel drawingPanel;
278: private javax.swing.JPanel guiPanel;
279: private javax.swing.JButton pauseButton;
280: private javax.swing.JButton resumeButton;
281: // End of variables declaration//GEN-END:variables
282:
283: }
|