Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 5 of 5

3D JavaWorld logo, transformed via translation, rotation, and scaling
You may have looked at all of the set-up code in our first two examples above and wondered, Why do we have to make so many redundant method calls each time we use Java 3D? The answer is, we don't if we're willing to use Sun utility classes (or write our own).
Sun has provided a large number of utility classes to take some of the burden off of the developer. Assuming you can put up with the portability restrictions mentioned previously, these utilities can take care of simple view branch construction for you. They also provide a library of primitive shapes that you can use directly, rather than constructing objects from the smallest fragments each time.
Example 3 illustrates the use of SimpleUniverse and ColorCube utilities. Please refer to the complete source code listing for the import statements and additional information on these utilities.
001 //Now that we have our Frame and Canvas3D, we are ready
002 //to start building our scene graph. We need to construct
003 //both a view branch and a content branch. Using Sun's
004 //SimpleUniverse utility, we can combine the View and
005 //view branch construction into one simple step.
006 SimpleUniverse myUniverse = new SimpleUniverse(myCanvas3D);
007 BranchGroup contentBranchGroup = constructContentBranch();
008 myUniverse.addBranchGraph(contentBranchGroup);
009 }
010
011 /**
012 * constructContentBranch() is where we specify the 3D graphics
013 * content to be rendered. We return the content branch group
014 * for use with our SimpleUniverse. We also demonstrate the
015 * use of com.sun.j3d.utils.geometry.ColorCube to build more
016 * complicated 3D shapes.
017 *
018 **/
019 private BranchGroup constructContentBranch() {
020 Font myFont = new Font("TimesRoman",Font.PLAIN,10);
021 Font3D myFont3D = new Font3D(myFont,new FontExtrusion());
022 Text3D myText3D = new Text3D(myFont3D, "JavaWorld");
023 Shape3D myShape3D = new Shape3D(myText3D, new Appearance());
024 Shape3D myCube = new ColorCube();
025
026 BranchGroup contentBranchGroup = new BranchGroup();
027 Transform3D myTransform3D = new Transform3D();
028 myTransform3D.setTranslation(new Vector3f(-1.0f,0.0f,-4.0f));
029 myTransform3D.setScale(0.1);
030 Transform3D tempTransform3D = new Transform3D();
031 tempTransform3D.rotY(Math.PI/4.0d);
032 myTransform3D.mul(tempTransform3D);
033 TransformGroup contentTransformGroup = new TransformGroup(myTransform3D);
034
035 contentTransformGroup.addChild(myShape3D);
036 contentBranchGroup.addChild(contentTransformGroup);
037
038 myTransform3D.setIdentity();
039 myTransform3D.setTranslation(new Vector3f(-0.5f,-0.5f,-2.3f));
040 myTransform3D.setScale(0.1);
041 TransformGroup cubeTransformGroup = new TransformGroup(myTransform3D);
042
043 cubeTransformGroup.addChild(myCube);
044 contentBranchGroup.addChild(cubeTransformGroup);
045
046 return(contentBranchGroup);
047 }

JavaWorld logo plus a ColorCube from Sun's utility packages
Note that the string "JavaWorld" should appear white: apparently, there is a bug in Java 3D involving the ordering of this render. I am following up with Sun personnel and will update the example source as need be to address this.
A vast amount of capability, and seeming complexity, lurks beneath the calm surface of the Java 3D sea. This column has shown you how to dip your toes in and go for a short swim.
Next month, we'll continue our exploration of Java 3D. I'll discuss loading VRML and other 3D content into Java 3D. I'll also discuss some of the general performance issues at work under the covers in Sun's Java 3D implementation. In the column after that, I'll compare and contrast Java OpenGL bindings with Java 3D.
I wanted to again thank everyone who stopped by to say hello at my SIGS Conference for Java Development tutorial. (I presented Programming with the Java Media APIs at JavaDevCon in late October 1998.) I greatly appreciate all the feedback I received on the tutorial and this column. As always, if you have questions or ideas for upcoming installments, please let me know.
Read more about Core Java in JavaWorld's Core Java section.