import java.awt.event.*; import java.awt.*; import java.net.*; import javax.swing.event.*; import javax.media.j3d.*; import javax.vecmath.*; import com.sun.j3d.utils.universe.*; import com.sun.j3d.utils.image.*; import com.sun.j3d.utils.geometry.*; import com.sun.j3d.utils.behaviors.mouse.*; public class Java3DWorld extends Canvas3D { private Appearance appearance; private Light ambientLight; private Box shape; private Color3f lightColor; private Light directionalLight; private Material material; private SimpleUniverse simpleUniverse; private TransformGroup transformGroup; private String imageName; private static final float x = 0.1f; private static final float y = 0.2f; private static final float z = 0.3f; public Java3DWorld ( String imageFileName ) { super( SimpleUniverse.getPreferredConfiguration() ); imageName=imageFileName; simpleUniverse=new SimpleUniverse( this); ViewingPlatform viewPlatform = simpleUniverse.getViewingPlatform(); viewPlatform.setNominalViewingTransform(); BranchGroup branchGroup = createScene(); simpleUniverse.addBranchGraph( branchGroup ); } public BranchGroup createScene() { BranchGroup scene = new BranchGroup (); transformGroup = new TransformGroup (); transformGroup.setCapability( TransformGroup.ALLOW_TRANSFORM_WRITE ); transformGroup.setCapability( TransformGroup.ALLOW_TRANSFORM_READ ); scene.addChild( transformGroup ); BoundingSphere bounds = new BoundingSphere( new Point3d( 0.0f, 0.0f, 0.0f ), 50.0 ); appearance=new Appearance(); material=new Material(); appearance.setMaterial( material ); String rgb = new String( "RGB" ); Box shape = new Box( x, y, z, Box.GENERATE_NORMALS | Box.GENERATE_TEXTURE_COORDS, appearance ); transformGroup.addChild( shape ); ambientLight = new AmbientLight(); ambientLight.setInfluencingBounds ( bounds ); directionalLight = new DirectionalLight (); lightColor = new Color3f(); directionalLight.setColor( lightColor ); directionalLight.setCapability( DirectionalLight.ALLOW_DIRECTION_WRITE ); directionalLight.setCapability( DirectionalLight.ALLOW_DIRECTION_READ ); directionalLight.setCapability( DirectionalLight.ALLOW_COLOR_WRITE ); directionalLight.setCapability( DirectionalLight.ALLOW_COLOR_READ ); directionalLight.setInfluencingBounds( bounds ); scene.addChild( ambientLight ); scene.addChild( directionalLight ); MouseRotate rotateBehavior = new MouseRotate(); rotateBehavior.setTransformGroup( transformGroup ); rotateBehavior.setSchedulingBounds( bounds ); MouseTranslate translateBehavior = new MouseTranslate(); translateBehavior.setTransformGroup( transformGroup ); translateBehavior.setSchedulingBounds( new BoundingBox( new Point3d( -1.0f, -1.0f, -1.0f ), new Point3d( 1.0f, 1.0f, 1.0f ) ) ); MouseZoom scaleBehavior = new MouseZoom(); scaleBehavior.setTransformGroup( transformGroup ); scaleBehavior.setSchedulingBounds( bounds ); scene.addChild( scaleBehavior ); scene.addChild( rotateBehavior ); scene.addChild( translateBehavior ); scene.compile(); return scene; } public void changeColor( Color color ) { lightColor.set( color ); directionalLight.setColor( lightColor ); } public Dimension getPreferredSize() { return new Dimension( 500, 500 ); } public Dimension getMinimumSize() { return getPreferredSize(); } }