/* * This code demonstrates j3d in a Swing environment. * * It creates a menu system that can draw on top of a Canvas3D * * It puts several 3D scenes into a Tabbed * * It creates two 3D windows on a Desktop. These do not work well because Canvas3D objects * always draw on top due to a heavyweight/lightweight issue. See ProblemChild * Note that Swing uses lightweight components and j3d heavy. This means that j3d will get drawn on top * of swing components, so JInternalFrames are not really a good idea for j3d. # This can be fixed for popup menus but not for JInternalFrames. * The Swing team says it may fix this in the next version of Swing. That would be cool. see * http://java.sun.com/products/jfc/tsc/articles/mixing/index.html * * * You can rotate the scenes in each window by left mouse clicking and moving the mouse * * * Copyright: Karl Meissner * meissner-sd@ieee.org * http://meissner.v0.net/java.html * * Karl Meissner grants you ("Licensee") a non-exclusive, royalty free, license to use, * modify and redistribute this software in source and binary code form, * provided that this copyright notice and license appear on all copies of * the software; * * This software is provided "AS IS," without a warranty of any kind. ALL * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. Karl Meissner SHALL NOT BE * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL Karl Meissner * BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF * OR INABILITY TO USE SOFTWARE, EVEN IF Karl Meissner HAS BEEN ADVISED OF THE * POSSIBILITY OF SUCH DAMAGES. * * This software is not designed or intended for use in on-line control of * aircraft, air traffic, aircraft navigation or aircraft communications; or in * the design, construction, operation or maintenance of any nuclear * facility. Licensee represents and warrants that it will not use or * redistribute the Software for such purposes. * this code demonstrates a interaction between swing and and j3d * * This code should be used with JDK 1.3 download from Sun with j3d 1.2.1 also from Sun * */ package java_in_swing; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; import java.beans.*; // import j3d stuff - note they must be correctly installed on your system - typicall in ~\jdk1.3\jre\lib\ext\ import com.sun.j3d.utils.universe.*; import com.sun.j3d.utils.geometry.*; import com.sun.j3d.utils.behaviors.mouse.*; import javax.media.j3d.*; import javax.vecmath.*; /** * Title: Label3D * Description: Places a Text2D in a scene, allows the text to be seen from the back * Copyright: Copyright (c) 2001 * Company: Meissner Software Development, LLC * @author Karl Meissner * @version 1.0 */ class Label3D extends TransformGroup { public Label3D( float x, float y, float z, String msg ) { super(); // place it in the scene graph Transform3D offset = new Transform3D(); offset.setTranslation( new Vector3f( x, y, z )); this.setTransform( offset ); // face it in the scene graph Transform3D rotation = new Transform3D(); TransformGroup rotation_group = new TransformGroup( rotation ); this.addChild( rotation_group ); // make a texture mapped polygon Text2D msg_poly = new Text2D( msg, new Color3f( 1.0f, 1.0f, 1.0f), "Helvetica", 18, Font.PLAIN ); // set it to draw both the front and back of the poly PolygonAttributes msg_attributes = new PolygonAttributes(); msg_attributes.setCullFace( PolygonAttributes.CULL_NONE ); msg_attributes.setBackFaceNormalFlip( true ); msg_poly.getAppearance().setPolygonAttributes( msg_attributes ); // attach it rotation_group.addChild( msg_poly ); } } /** * Title: Wrap3D * Description: this holds a j3d rendered image in a Swing container. * Now you can plug this into anything that takes Swing objects * * Copyright: Copyright (c) 2001 * Company: Meissner Software Development, LLC * @author Karl Meissner * @version 1.0 */ class Wrap3D extends JPanel { // make a scene with a cube and a label BranchGroup createSceneGraph( int scene_type ) { BranchGroup objRoot = new BranchGroup(); TransformGroup root_group = new TransformGroup( ); root_group.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); // allow the mouse behavior to rotate the scene root_group.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); objRoot.addChild( root_group ); // this is the local origin - everyone hangs off this - moving this move every one switch( scene_type ) // decide what to draw { case 0 : root_group.addChild( new ColorCube(0.2) ); // add a color cube root_group.addChild( new Label3D( 0.2f, 0.2f, 0.0f, "ColorCube") ); // add a label to the scene break; case 1 : Transform3D sphere_transform = new Transform3D(); sphere_transform.setScale( 0.2); // shrink the sphere so our view point is not inside it TransformGroup sphere_group = new TransformGroup( sphere_transform ); sphere_group.addChild( new Sphere() ); // add a sphere root_group.addChild( sphere_group ); root_group.addChild( new Label3D( 0.2f, 0.2f, 0.0f, "sphere") ); // add a label to the scene BoundingSphere bounds = new BoundingSphere(); // the render will only light objects inside this volume bounds.setRadius(1000.0); // add a light so we can see the sphere DirectionalLight lightD = new DirectionalLight(); lightD.setInfluencingBounds(bounds); lightD.setDirection(new Vector3f(.7f, -.7f, -.7f)); // point the light right, down, into the screen objRoot.addChild(lightD); break; } // another J3D bug with JInternalFrames - if there are a lot of Text2D obects and several scenes, // there is a strange interaction // between selecting windows and texture memory. After selecting several // JInternalFrames with Canvas3D items, Text2D items start // drawing as opaque white rectangles. // the only work around I can find is to either only have a single scene or // not to use Text2D // I would appriciate any other workarounds for JInternalFrames people // can come up with - meissner-sd@ieee.org // reproduce bug HERE /* int Num_Text_Msg; Num_Text_Msg=20; int h; for ( h = 0; h