001: /*
002: * $RCSfile: SwingInteraction.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:53 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.swing_interaction;
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: import java.awt.event.ActionEvent;
053: import java.awt.event.MouseEvent;
054: import java.util.Enumeration;
055: import javax.swing.JPopupMenu;
056:
057: /**
058: * Simple Java 3D test program created in NetBeans to illustrate interacting
059: * with a Java 3D scene graph from an Swing-based program.
060: */
061: public class SwingInteraction extends javax.swing.JFrame {
062:
063: private SimpleUniverse univ = null;
064: private BranchGroup scene = null;
065:
066: private TransformGroup objTrans;
067: private RotateBehavior awtBehavior;
068:
069: public BranchGroup createSceneGraph() {
070: // Create the root of the branch graph
071: BranchGroup objRoot = new BranchGroup();
072:
073: // Create the transform group node and initialize it to the
074: // identity. Enable the TRANSFORM_WRITE capability so that
075: // our behavior code can modify it at runtime. Add it to the
076: // root of the subgraph.
077: objTrans = new TransformGroup();
078: objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
079: objRoot.addChild(objTrans);
080:
081: // Create a simple shape leaf node, add it to the scene graph.
082: objTrans.addChild(new ColorCube(0.4));
083:
084: // create the RotateBehavior
085: awtBehavior = new RotateBehavior(objTrans);
086: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
087: 0.0, 0.0), 100.0);
088: awtBehavior.setSchedulingBounds(bounds);
089: objRoot.addChild(awtBehavior);
090:
091: return objRoot;
092: }
093:
094: private Canvas3D createUniverse() {
095: GraphicsConfiguration config = SimpleUniverse
096: .getPreferredConfiguration();
097:
098: Canvas3D c = new Canvas3D(config);
099:
100: univ = new SimpleUniverse(c);
101:
102: // This will move the ViewPlatform back a bit so the
103: // objects in the scene can be viewed.
104: univ.getViewingPlatform().setNominalViewingTransform();
105:
106: // Ensure at least 5 msec per frame (i.e., < 200Hz)
107: univ.getViewer().getView().setMinimumFrameCycleTime(5);
108:
109: return c;
110: }
111:
112: /**
113: * Creates new form SwingInteraction
114: */
115: public SwingInteraction() {
116: // Initialize the GUI components
117: JPopupMenu.setDefaultLightWeightPopupEnabled(false);
118: initComponents();
119:
120: // Create Canvas3D and SimpleUniverse; add canvas to drawing panel
121: Canvas3D c = createUniverse();
122: drawingPanel.add(c, java.awt.BorderLayout.CENTER);
123:
124: // Create the content branch and add it to the universe
125: scene = createSceneGraph();
126: univ.addBranchGraph(scene);
127: }
128:
129: /**
130: * Behavior class that waits for a behavior post from the AWT event handler
131: */
132: class RotateBehavior extends Behavior {
133:
134: private TransformGroup transformGroup;
135: private Transform3D trans = new Transform3D();
136: private WakeupCriterion criterion;
137: private float angle = 0.0f;
138:
139: private final int ROTATE = 1;
140:
141: // create a new RotateBehavior
142: RotateBehavior(TransformGroup tg) {
143: transformGroup = tg;
144: }
145:
146: // initialize behavior to wakeup on a behavior post with id = ROTATE
147: public void initialize() {
148: criterion = new WakeupOnBehaviorPost(this , ROTATE);
149: wakeupOn(criterion);
150: }
151:
152: // processStimulus to rotate the cube
153: public void processStimulus(Enumeration criteria) {
154: angle += Math.toRadians(10.0);
155: trans.rotY(angle);
156: transformGroup.setTransform(trans);
157: wakeupOn(criterion);
158: }
159:
160: // when the mouse is clicked, postId for the behavior
161: void rotate() {
162: postId(ROTATE);
163: }
164: }
165:
166: // ----------------------------------------------------------------
167:
168: /** This method is called from within the constructor to
169: * initialize the form.
170: * WARNING: Do NOT modify this code. The content of this method is
171: * always regenerated by the Form Editor.
172: */
173: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
174: private void initComponents() {
175: java.awt.GridBagConstraints gridBagConstraints;
176:
177: guiPanel = new javax.swing.JPanel();
178: rotateButton = new javax.swing.JButton();
179: drawingPanel = new javax.swing.JPanel();
180: jMenuBar1 = new javax.swing.JMenuBar();
181: fileMenu = new javax.swing.JMenu();
182: exitMenuItem = new javax.swing.JMenuItem();
183:
184: setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
185: setTitle("Swing Interaction Test");
186: guiPanel.setLayout(new java.awt.GridBagLayout());
187:
188: rotateButton.setText("Rotate");
189: rotateButton
190: .addActionListener(new java.awt.event.ActionListener() {
191: public void actionPerformed(
192: java.awt.event.ActionEvent evt) {
193: rotateButtonActionPerformed(evt);
194: }
195: });
196:
197: gridBagConstraints = new java.awt.GridBagConstraints();
198: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
199: gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
200: guiPanel.add(rotateButton, gridBagConstraints);
201:
202: getContentPane().add(guiPanel, java.awt.BorderLayout.NORTH);
203:
204: drawingPanel.setLayout(new java.awt.BorderLayout());
205:
206: drawingPanel.setPreferredSize(new java.awt.Dimension(500, 500));
207: getContentPane()
208: .add(drawingPanel, java.awt.BorderLayout.CENTER);
209:
210: fileMenu.setText("File");
211: exitMenuItem.setText("Exit");
212: exitMenuItem
213: .addActionListener(new java.awt.event.ActionListener() {
214: public void actionPerformed(
215: java.awt.event.ActionEvent evt) {
216: exitMenuItemActionPerformed(evt);
217: }
218: });
219:
220: fileMenu.add(exitMenuItem);
221:
222: jMenuBar1.add(fileMenu);
223:
224: setJMenuBar(jMenuBar1);
225:
226: pack();
227: }// </editor-fold>//GEN-END:initComponents
228:
229: private void rotateButtonActionPerformed(
230: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rotateButtonActionPerformed
231: awtBehavior.rotate();
232: }//GEN-LAST:event_rotateButtonActionPerformed
233:
234: private void exitMenuItemActionPerformed(
235: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed
236: System.exit(0);
237: }//GEN-LAST:event_exitMenuItemActionPerformed
238:
239: /**
240: * @param args the command line arguments
241: */
242: public static void main(String args[]) {
243: java.awt.EventQueue.invokeLater(new Runnable() {
244: public void run() {
245: new SwingInteraction().setVisible(true);
246: }
247: });
248: }
249:
250: // Variables declaration - do not modify//GEN-BEGIN:variables
251: private javax.swing.JPanel drawingPanel;
252: private javax.swing.JMenuItem exitMenuItem;
253: private javax.swing.JMenu fileMenu;
254: private javax.swing.JPanel guiPanel;
255: private javax.swing.JMenuBar jMenuBar1;
256: private javax.swing.JButton rotateButton;
257: // End of variables declaration//GEN-END:variables
258:
259: }
|