import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event.*;
import java.awt.GraphicsConfiguration;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.ColorCube;
import javax.media.j3d.*;
import javax.vecmath.*;
// HelloJava3Da renders a single, rotating cube.
public class HelloJava3Da extends Applet {
public HelloJava3Da() {
setLayout(new BorderLayout());
GraphicsConfiguration config =
SimpleUniverse.getPreferredConfiguration();
Canvas3D canvas3D = new Canvas3D(config);
add("Center", canvas3D);
BranchGroup scene = createSceneGraph();
// SimpleUniverse is a Convenience Utility class
SimpleUniverse simpleU = new SimpleUniverse(canvas3D);
// This will move the ViewPlatform back a bit so the
// objects in the scene can be viewed.
simpleU.getViewingPlatform().setNominalViewingTransform();
simpleU.addBranchGraph(scene);
} // end of HelloJava3Da (constructor)
public BranchGroup createSceneGraph() {
// Create the root of the branch graph
BranchGroup objRoot = new BranchGroup();
Transform3D zTrans = new Transform3D( );
zTrans.set( new Vector3f( 0.0f,0.0f,1.0f ) ); //------------------------------- POSITION 1
TransformGroup objTrans = new TransformGroup( zTrans );
objTrans.addChild(new Sphere(0.9f,
Sphere.GENERATE_NORMALS,
createAppearance()));
Transform3D zTrans2 = new Transform3D( );
zTrans.set( new Vector3f( 0.0f,0.0f,-1.0f ) ); //------------------------------- POSITION 2
TransformGroup objTrans2 = new TransformGroup( zTrans2 );
objTrans2.addChild(new Sphere(0.9f,
Sphere.GENERATE_NORMALS,
createAppearance()));
objRoot.addChild(objTrans);
objRoot.addChild(objTrans2);
return objRoot;
} // end of CreateSceneGraph method of HelloJava3Da
// The following allows this to be run as an application
// as well as an applet
public static void main(String[] args) {
Frame frame = new MainFrame(new HelloJava3Da(), 256, 256);
} // end of main (method of HelloJava3Da)
} // end of class HelloJava3Da
Appearance createAppearance() {
Appearance appear = new Appearance();
Material material = new Material();
// material.setDiffuseColor(0.0f, 0.0f, 1.0f);
material.setShininess(50.0f);
// make modifications to default material properties
appear.setMaterial(material);
// ColoringAttributes colorAtt = new ColoringAttributes();
// colorAtt.setShadeModel(ColoringAttributes.SHADE_FLAT);
// appear.setColoringAttributes(colorAtt);
return appear;
}
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event.*;
import java.awt.GraphicsConfiguration;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;
import javax.media.j3d.*;
import javax.vecmath.*;
// HelloJava3Da renders a single, rotating cube.
public class HelloJava3Da extends Applet {
public HelloJava3Da() {
setLayout(new BorderLayout());
GraphicsConfiguration config =
SimpleUniverse.getPreferredConfiguration();
Canvas3D canvas3D = new Canvas3D(config);
add("Center", canvas3D);
BranchGroup scene = createSceneGraph();
// SimpleUniverse is a Convenience Utility class
SimpleUniverse simpleU = new SimpleUniverse(canvas3D);
// This will move the ViewPlatform back a bit so the
// objects in the scene can be viewed.
simpleU.getViewingPlatform().setNominalViewingTransform();
simpleU.addBranchGraph(scene);
} // end of HelloJava3Da (constructor)
public BranchGroup createSceneGraph() {
// Create the root of the branch graph
BranchGroup objRoot = new BranchGroup();
Color3f lColor1 = new Color3f(1.0f, 0.0f, 0.0f);
ColoringAttributes caL1 = new ColoringAttributes();
caL1.setColor(lColor1);
Appearance appL1 = new Appearance();
appL1.setColoringAttributes(caL1);
Transform3D zTrans = new Transform3D( );
zTrans.set( new Vector3f( 0.0f,-1.0f,-4.0f ) ); //------------------------------- POSITION 1
TransformGroup objTrans = new TransformGroup( zTrans );
objTrans.addChild(new Sphere(0.2f, appL1));
Transform3D zTrans2 = new Transform3D( );
zTrans2.set( new Vector3f( 0.0f,1.0f,-2.0f ) ); //------------------------------- POSITION 2
TransformGroup objTrans2 = new TransformGroup( zTrans2 );
objTrans2.addChild(new Sphere(0.2f, appL1));
objRoot.addChild(objTrans);
objRoot.addChild(objTrans2);
return objRoot;
} // end of CreateSceneGraph method of HelloJava3Da
// The following allows this to be run as an application
// as well as an applet
public static void main(String[] args) {
Frame frame = new MainFrame(new HelloJava3Da(), 556, 556);
} // end of main (method of HelloJava3Da)
Appearance createAppearance() {
Appearance appear = new Appearance();
Material material = new Material();
material.setDiffuseColor(0.0f, 0.0f, 1.0f);
material.setShininess(50.0f);
// make modifications to default material properties
appear.setMaterial(material);
// ColoringAttributes colorAtt = new ColoringAttributes();
// colorAtt.setShadeModel(ColoringAttributes.SHADE_FLAT);
// appear.setColoringAttributes(colorAtt);
return appear;
}
} // end of class HelloJava3Da