From 59ef25109b7243ebff8a130810bea0058e08c4ac Mon Sep 17 00:00:00 2001 From: pepe Date: Thu, 9 Nov 2006 06:47:37 +0000 Subject: Issue 131: updated demo for JCanvas3D. Removed tabs, table and added options. --- .../j3d/examples/jcanvas3d/JCanvas3DExample.form | 195 ++++++++------------- .../j3d/examples/jcanvas3d/JCanvas3DExample.java | 146 ++++++--------- .../j3d/examples/jcanvas3d/JInternalWorld.java | 114 ++++++------ 3 files changed, 184 insertions(+), 271 deletions(-) (limited to 'src') diff --git a/src/classes/org/jdesktop/j3d/examples/jcanvas3d/JCanvas3DExample.form b/src/classes/org/jdesktop/j3d/examples/jcanvas3d/JCanvas3DExample.form index 80874ae..661f402 100644 --- a/src/classes/org/jdesktop/j3d/examples/jcanvas3d/JCanvas3DExample.form +++ b/src/classes/org/jdesktop/j3d/examples/jcanvas3d/JCanvas3DExample.form @@ -1,18 +1,18 @@ -
+ - - + + @@ -20,15 +20,12 @@ - + - - - @@ -38,7 +35,7 @@ - + @@ -47,124 +44,77 @@ - - - - - + - + - + + + + + + + + - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
-
-
-
-
-
-
-
-
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
@@ -174,6 +124,9 @@ + + + diff --git a/src/classes/org/jdesktop/j3d/examples/jcanvas3d/JCanvas3DExample.java b/src/classes/org/jdesktop/j3d/examples/jcanvas3d/JCanvas3DExample.java index 198e605..a68bbe6 100644 --- a/src/classes/org/jdesktop/j3d/examples/jcanvas3d/JCanvas3DExample.java +++ b/src/classes/org/jdesktop/j3d/examples/jcanvas3d/JCanvas3DExample.java @@ -44,16 +44,16 @@ package org.jdesktop.j3d.examples.jcanvas3d; -import java.awt.Toolkit; -import javax.swing.table.DefaultTableModel; /** - * - * @author pepe + * Simple Java 3D example program that displays universes within lightweight swing components, layed in JInternalFrame objects. */ +import java.awt.Toolkit; +import javax.swing.table.DefaultTableModel; + public class JCanvas3DExample extends javax.swing.JFrame implements java.awt.event.ActionListener { - + /** * Creates new form JCanvas3DExample */ @@ -63,7 +63,7 @@ public class JCanvas3DExample extends javax.swing.JFrame implements java.awt.eve Toolkit.getDefaultToolkit().setDynamicLayout( true ); setDefaultCloseOperation( EXIT_ON_CLOSE ); } - + /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is @@ -73,101 +73,71 @@ public class JCanvas3DExample extends javax.swing.JFrame implements java.awt.eve private void initComponents() { java.awt.GridBagConstraints gridBagConstraints; - jSplitPane1 = new javax.swing.JSplitPane(); - jScrollPane1 = new javax.swing.JScrollPane(); - jPanel1 = new javax.swing.JPanel(); - jTabbedPane1 = new javax.swing.JTabbedPane(); - jPanel2 = new javax.swing.JPanel(); + splitPane = new javax.swing.JSplitPane(); + scrollPane = new javax.swing.JScrollPane(); + panel = new javax.swing.JPanel(); addButton = new javax.swing.JButton(); delayCheckBox = new javax.swing.JCheckBox(); interactiveCheckBox = new javax.swing.JCheckBox(); - jPanel3 = new javax.swing.JPanel(); - jScrollPane2 = new javax.swing.JScrollPane(); - dataTable = new javax.swing.JTable(); + randomCheckBox = new javax.swing.JCheckBox(); desktopPane = new javax.swing.JDesktopPane(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); - setTitle("JCanvas3D Example"); - jSplitPane1.setDividerLocation(300); - jSplitPane1.setDividerSize(8); - jSplitPane1.setContinuousLayout(true); - jSplitPane1.setOneTouchExpandable(true); - jSplitPane1.setPreferredSize(new java.awt.Dimension(1000, 685)); - jPanel1.setLayout(new java.awt.BorderLayout()); - - jPanel2.setLayout(new java.awt.GridBagLayout()); + splitPane.setDividerLocation(300); + splitPane.setDividerSize(8); + splitPane.setContinuousLayout(true); + splitPane.setOneTouchExpandable(true); + panel.setLayout(new java.awt.GridBagLayout()); addButton.setText("Create New Frame"); addButton.setToolTipText("Adds a new frame containing an universe into the desktop pane"); addButton.addActionListener(this); - jPanel2.add(addButton, new java.awt.GridBagConstraints()); + panel.add(addButton, new java.awt.GridBagConstraints()); delayCheckBox.setText("Resize Delayed"); delayCheckBox.setToolTipText("Shows the effect of using a delayed resizing to the internal frames."); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 1; - jPanel2.add(delayCheckBox, gridBagConstraints); + gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; + panel.add(delayCheckBox, gridBagConstraints); + interactiveCheckBox.setSelected(true); interactiveCheckBox.setText("Interactive Cube"); interactiveCheckBox.setToolTipText("Tests the use of AWT behaviors on the displayed component."); interactiveCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); interactiveCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0)); + interactiveCheckBox.addActionListener(this); + gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 2; - jPanel2.add(interactiveCheckBox, gridBagConstraints); - - jTabbedPane1.addTab("Content Creation", jPanel2); - - jPanel3.setLayout(new java.awt.BorderLayout()); - - jScrollPane2.setPreferredSize(new java.awt.Dimension(100, 100)); - dataTable.setModel(new javax.swing.table.DefaultTableModel( - new Object [][] { - - }, - new String [] { - "canvas", "Title 2" - } - ) { - Class[] types = new Class [] { - java.lang.Integer.class, java.lang.Long.class - }; - boolean[] canEdit = new boolean [] { - false, false - }; - - public Class getColumnClass(int columnIndex) { - return types [columnIndex]; - } - - public boolean isCellEditable(int rowIndex, int columnIndex) { - return canEdit [columnIndex]; - } - }); - jScrollPane2.setViewportView(dataTable); - - jPanel3.add(jScrollPane2, java.awt.BorderLayout.CENTER); - - jTabbedPane1.addTab("Data Report", jPanel3); + gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; + panel.add(interactiveCheckBox, gridBagConstraints); - jPanel1.add(jTabbedPane1, java.awt.BorderLayout.CENTER); + randomCheckBox.setText("Random start angle"); + randomCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); + randomCheckBox.setEnabled(false); + randomCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0)); + gridBagConstraints = new java.awt.GridBagConstraints(); + gridBagConstraints.gridx = 0; + gridBagConstraints.gridy = 3; + gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; + panel.add(randomCheckBox, gridBagConstraints); - jScrollPane1.setViewportView(jPanel1); - jPanel1.getAccessibleContext().setAccessibleName("parameter panel"); - jPanel1.getAccessibleContext().setAccessibleDescription("panel used to set parameters and create internal frames"); + scrollPane.setViewportView(panel); - jSplitPane1.setLeftComponent(jScrollPane1); + splitPane.setLeftComponent(scrollPane); desktopPane.setBackground(new java.awt.Color(153, 153, 201)); - jSplitPane1.setRightComponent(desktopPane); + desktopPane.setPreferredSize(new java.awt.Dimension(300, 300)); + splitPane.setRightComponent(desktopPane); - getContentPane().add(jSplitPane1, java.awt.BorderLayout.CENTER); + getContentPane().add(splitPane, java.awt.BorderLayout.CENTER); java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize(); - setBounds((screenSize.width-1010)/2, (screenSize.height-715)/2, 1010, 715); + setBounds((screenSize.width-1020)/2, (screenSize.height-402)/2, 1020, 402); } // Code for dispatching events from components to event handlers. @@ -176,26 +146,28 @@ public class JCanvas3DExample extends javax.swing.JFrame implements java.awt.eve if (evt.getSource() == addButton) { JCanvas3DExample.this.addButtonActionPerformed(evt); } + else if (evt.getSource() == interactiveCheckBox) { + JCanvas3DExample.this.interactiveCheckBoxActionPerformed(evt); + } }// //GEN-END:initComponents - private void addButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_addButtonActionPerformed - {//GEN-HEADEREND:event_addButtonActionPerformed + private void interactiveCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_interactiveCheckBoxActionPerformed + randomCheckBox.setEnabled( interactiveCheckBox.isSelected() ? false:true ); + }//GEN-LAST:event_interactiveCheckBoxActionPerformed + + private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed JInternalWorld iWorld; -// if ( true == filteredCheckBox.isSelected() ) -// { - iWorld = new JInternalWorld( delayCheckBox.isSelected(), interactiveCheckBox.isSelected(), (DefaultTableModel)dataTable.getModel() ); -// } -// else -// { -// iWorld = new JInternalWorldFiltered( delayCheckBox.isSelected(), interactiveCheckBox.isSelected() ); -// } + // we create an internal world to be added within the JDesktop. + iWorld = new JInternalWorld( interactiveCheckBox.isSelected(), + this.randomCheckBox.isSelected() && ( false == interactiveCheckBox.isSelected() ) + ); iWorld.setSize( 256, 256 ); iWorld.setLocation( 50, 50 ); iWorld.setResizable( true ); desktopPane.add( iWorld ); iWorld.setVisible(true); }//GEN-LAST:event_addButtonActionPerformed - + /** * @param args the command line arguments */ @@ -209,20 +181,16 @@ public class JCanvas3DExample extends javax.swing.JFrame implements java.awt.eve } }); } - + // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton addButton; - private javax.swing.JTable dataTable; private javax.swing.JCheckBox delayCheckBox; private javax.swing.JDesktopPane desktopPane; private javax.swing.JCheckBox interactiveCheckBox; - private javax.swing.JPanel jPanel1; - private javax.swing.JPanel jPanel2; - private javax.swing.JPanel jPanel3; - private javax.swing.JScrollPane jScrollPane1; - private javax.swing.JScrollPane jScrollPane2; - private javax.swing.JSplitPane jSplitPane1; - private javax.swing.JTabbedPane jTabbedPane1; + private javax.swing.JPanel panel; + private javax.swing.JCheckBox randomCheckBox; + private javax.swing.JScrollPane scrollPane; + private javax.swing.JSplitPane splitPane; // End of variables declaration//GEN-END:variables - + } diff --git a/src/classes/org/jdesktop/j3d/examples/jcanvas3d/JInternalWorld.java b/src/classes/org/jdesktop/j3d/examples/jcanvas3d/JInternalWorld.java index 7193dc9..6bfa32d 100644 --- a/src/classes/org/jdesktop/j3d/examples/jcanvas3d/JInternalWorld.java +++ b/src/classes/org/jdesktop/j3d/examples/jcanvas3d/JInternalWorld.java @@ -41,97 +41,93 @@ * $Date$ * $State$ */ - package org.jdesktop.j3d.examples.jcanvas3d; import com.sun.j3d.exp.swing.JCanvas3D; -//import com.sun.j3d.exp.swing.Canvas3DWrapper; import com.sun.j3d.utils.behaviors.mouse.MouseRotate; import com.sun.j3d.utils.geometry.ColorCube; import com.sun.j3d.utils.universe.SimpleUniverse; + import java.awt.BorderLayout; import java.awt.Component; import java.awt.Dimension; +import java.awt.Rectangle; +import java.awt.geom.Rectangle2D; + import javax.media.j3d.Alpha; import javax.media.j3d.BoundingSphere; import javax.media.j3d.BranchGroup; +import javax.media.j3d.GraphicsConfigTemplate3D; import javax.media.j3d.RotationInterpolator; import javax.media.j3d.Transform3D; import javax.media.j3d.TransformGroup; + import javax.swing.JInternalFrame; -import javax.swing.table.DefaultTableModel; + import javax.vecmath.Point3d; import javax.vecmath.Vector3d; /** - * DOCUMENT ME! + * This is a JInternalFrame holding an universe, which can be configured to + * be interactive -that is, where user can interact with object- or automatic + * -where the object spins only-. When in automatic mode, spinning speed is + * changed so that they look less the same. Changing the spinning start angle + * helps unsynchronizing the rotations too. * * @author pepe */ public class JInternalWorld extends JInternalFrame { - /** DOCUMENT ME! */ - JCanvas3D c; - - /** Creates a new instance of JInternalWorld */ - private SimpleUniverse u = null; - /** DOCUMENT ME! */ private Component comp; - private DefaultTableModel model; - /** * Creates a new JInternalWorld object. * - * @param isDelayed DOCUMENT ME! - * @param isInteractive DOCUMENT ME! + * @param isInteractive tells the world to be constructed as interactive + * @param isDelayed tells the rotator to start at a random alpha. */ - public JInternalWorld(boolean isDelayed, boolean isInteractive, DefaultTableModel model) { + public JInternalWorld(boolean isInteractive, boolean isDelayed) { + super(); setSize(256, 256); setClosable(true); - this.model = model; - model.addRow( new Object[]{new Integer( hashCode()), Integer.valueOf(0) }); - c = new JCanvas3D(); + JCanvas3D canvas = new JCanvas3D(new GraphicsConfigTemplate3D()); if (true == isDelayed) { - c.setResizeMode(c.RESIZE_DELAYED); + canvas.setResizeMode(canvas.RESIZE_DELAYED); } - setComp(c); + comp = canvas; + Dimension dim = new Dimension(256, 256); - getComp().setPreferredSize( dim ); - getComp().setSize( dim ); + comp.setPreferredSize(dim); + comp.setSize(dim); getContentPane().setLayout(new BorderLayout()); - getContentPane().add(getComp(), BorderLayout.CENTER); + getContentPane().add(comp, BorderLayout.CENTER); pack(); // Create a simple scene and attach it to the virtual universe - BranchGroup scene = createSceneGraph(isInteractive); - u = new SimpleUniverse(c.getOffscreenCanvas3D());//TODO: this is awful and must not be done like that in final version + BranchGroup scene = createSceneGraph(isInteractive, isDelayed); + SimpleUniverse universe = new SimpleUniverse(canvas.getOffscreenCanvas3D()); //TODO: this is awful and must not be done like that in final version // This will move the ViewPlatform back a bit so the // objects in the scene can be viewed. - u.getViewingPlatform().setNominalViewingTransform(); -// Canvas3DWrapper wrapper = new Canvas3DWrapper( c ); -// wrapper.setView( u.getViewer().getView() ); -// KeyNavigatorBehavior navig = new KeyNavigatorBehavior(u.getViewingPlatform().getViewPlatformTransform() ); -// BranchGroup navigBG = new BranchGroup(); -// navigBG.addChild( navig ); -// scene.addChild( navigBG ); - u.addBranchGraph(scene); -// c.getOffscreenCanvas3D().getView().setMinimumFrameCycleTime(200);//TODO: this is awful and must not be done like that in final version + universe.getViewingPlatform().setNominalViewingTransform(); + universe.getViewer().getView().setMinimumFrameCycleTime(30); + universe.addBranchGraph(scene); } /** - * DOCUMENT ME! + * Creates the world. Only exists to cleanup the source a bit * - * @param isInteractive DOCUMENT ME! + * @param isInteractive tells the world to be constructed as interactive + * @param isDelayed tells the rotator to start at a random alpha. * - * @return DOCUMENT ME! + * @return a global branchgroup containing the world, as desired. */ - public BranchGroup createSceneGraph(boolean isInteractive) { + private BranchGroup createSceneGraph(boolean isInteractive, + boolean isDelayed) { // Create the root of the branch graph BranchGroup objRoot = new BranchGroup(); @@ -141,12 +137,13 @@ public class JInternalWorld extends JInternalFrame { // the root of the subgraph. TransformGroup objTrans = new TransformGroup(); Transform3D t3dTrans = new Transform3D(); - t3dTrans.setTranslation( new Vector3d( 0, 0, -1 ) ); - objTrans.setTransform( t3dTrans ); + t3dTrans.setTranslation(new Vector3d(0, 0, -1)); + objTrans.setTransform(t3dTrans); + TransformGroup objRot = new TransformGroup(); objRot.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); objRoot.addChild(objTrans); - objTrans.addChild( objRot ); + objTrans.addChild(objRot); // Create a simple Shape3D node; add it to the scene graph. ColorCube cube = new ColorCube(0.75); @@ -156,7 +153,7 @@ public class JInternalWorld extends JInternalFrame { 100.0); if (true == isInteractive) { - MouseRotate mr = new MouseRotate(getComp(), objRot); + MouseRotate mr = new MouseRotate(comp, objRot); mr.setSchedulingBounds(bounds); mr.setSchedulingInterval(1); objRoot.addChild(mr); @@ -165,11 +162,23 @@ public class JInternalWorld extends JInternalFrame { // desired operation on the specified transform and add // it into the scene graph. Transform3D yAxis = new Transform3D(); - Alpha rotationAlpha = new Alpha(-1, - Math.max(2000, (int) (Math.random() * 8000.))); + + // rotation speed is randomized a bit so that it does not go at the same speed on every canvases, + // which will make it more natural and express the differences between every present universes + int duration = Math.max(2000, (int) (Math.random() * 8000.)); + Alpha rotationAlpha = null; + + if (true == isDelayed) { + rotationAlpha = new Alpha(-1, + (int) ((double) duration * Math.random()), 0, duration, + 0, 0); + } else { + rotationAlpha = new Alpha(-1, duration); + } RotationInterpolator rotator = new RotationInterpolator(rotationAlpha, objRot, yAxis, 0.0f, (float) Math.PI * 2.0f); + rotator.setSchedulingBounds(bounds); objRoot.addChild(rotator); } @@ -177,21 +186,4 @@ public class JInternalWorld extends JInternalFrame { return objRoot; } - /** - * DOCUMENT ME! - * - * @return DOCUMENT ME! - */ - protected Component getComp() { - return comp; - } - - /** - * DOCUMENT ME! - * - * @param comp DOCUMENT ME! - */ - protected void setComp(Component comp) { - this.comp = comp; - } } -- cgit v1.2.3