Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

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

3D graphics programming in Java, Part 1: Java 3D

Get a head start with this introduction to the Java 3D API

  • Print
  • Feedback

Page 5 of 5

3D JavaWorld logo, transformed via translation, rotation, and scaling

Using Sun utility classes to make your code simpler

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.

Conclusions

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.

About the author

Bill Day is a software engineer at Silicon Graphics Computer Systems and an ACM Distinguished Lecturer. In addition to writing for JavaWorld, Bill is authoring a book entitled Java Media Players for O'Reilly & Associates. When Bill is not writing or programming, he loves to mountain bike, travel with his wife, and speak French. Java, c'est magnifique!

Read more about Core Java in JavaWorld's Core Java section.

  • Print
  • Feedback

Resources
  • A good place to start is the Introduction to Programming with Java 3D course tutorial put together by Sun and the San Diego Supercomputer Center. http://www.sdsc.edu/~nadeau/Courses/SDSCjava3d/
  • Sun's Java 3D API page documents the API and its use. It contains javadocs for the API, the mailing list archive, Sun's Java 3D FAQ, and more. http://java.sun.com/products/java-media/3D/
  • The API is specified in Sun's Java 3D API Specification document. http://java.sun.com/products/java-media/3D/forDevelopers/j3dguide/j3dTOC.doc.html
  • The specification is also available in book format from Addison-Wesley's Java Series. Please note that the version of the API this printed specification corresponds to may be somewhat out of date The online specification is always kept up to date for the latest API release. http://www.amazon.com/exec/obidos/ASIN/0201325764/billday/
  • Sun makes its Win32 and Solaris implementations (as opposed to the API specification itself) available from a separate, product Web site. This site also contains the example code for Sun Java 3D training courses, demos from the Java 3D programming contest Sun hosted earlier in 1998, case studies with Sun's Java 3D customers, and lists of Java 3D related papers and Web pages. http://www.sun.com/desktop/java3d/index.html
  • Sun's Java 3D product site links to the course notes for its "Java 3D Roadshow." This is the crash course Sun representatives have been teaching to interested developers around the world in 1998. http://www.sun.com/desktop/java3d/collateral/j3d_clas.pdf
  • The OpenGL Consortium's Web site is the complete resource for OpenGL. http://www.opengl.org/
  • The Java 3D FAQ is maintained by Steve Pietrowicz, a project manager of the Java 3D group at NCSA. It is an excellent third-party resource for Java 3D information. http://tintoy.ncsa.uiuc.edu/~srp/java3d/faq.html
  • Read more about Java 3D's heavyweight components and Sun's thoughts on integrating them with Swing lightweight components. http://java.sun.com/products/java-media/mail-archive/3D/1520.html
  • Follow-up on Sun's plans for Java 3D vis-a-vis lightweight components and Swing. http://java.sun.com/products/java-media/mail-archive/3D/1557.html
  • "Progress on the media front at Siggraph '98" sizes up the state of Java Media in general and Java 3D in particular at this year's premier computer graphics conference. http://www.javaworld.com/javaworld/jw-10-1998/jw-10-media.html
  • Download source code and classes for this column. http://www.javaworld.com/jw-12-1998/media/jw-12-media.jar
  • Bill has archived Media Programming resources on his Web site. This archive contains the up-to-date media.jar file with code fixes for all of the examples in the column. http://reality.sgi.com/bday/Work/index.html
  • Read Bill's previous Media Programming columns. http://www.javaworld.com/topicalindex/jw-ti-media.html