Newsletter sign-up
View all newsletters

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

Enter the third dimension

Use a Swing component to explore three-dimensional computer graphics

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

While attending university in the mid-1980s, I studied computer graphics from both two-dimensional and three-dimensional perspectives. Although I enjoyed playing with 2D graphics, I became fascinated with 3D graphics. This fascination led me to write this installment of Java Fun and Games, which provides an introduction to 3D graphics from the Java perspective. I'll eventually apply this and future 3D graphics articles to a 3D-based Java game.

This article first introduces you to a Swing component, whose API lets you load/display a 3D model and adjust a 3D model's viewability. Next, the article introduces you to a Swing applet, whose user interface lets you play with this component. This article concludes by presenting you with a component-based 3D graphics tutorial, which introduces you to 3D graphics fundamentals—starting with coordinate systems and transformations.

Note
This article's Swing component does not rely on Java3D or some other high-level 3D API because high-level 3D APIs are extra baggage to consider when deploying applets. Furthermore, having source code access to a low-level 3D API improves your understanding of 3D graphics. I've based this article on Principles of Interactive Computer Graphics (Second Edition) (by William Newman and Robert Sproull, (McGraw-Hill, 1979; ISBN 0070463387)). If you don't have this book, Amazon provides copies for sale.


A component for 3D graphics

A custom Swing component with a simple API is probably the most convenient tool for introducing 3D graphics to Swing-based applets and applications. To this end, I've created a Panel3D class that subclasses javax.swing.JPanel. This 3D panel component provides a four-method API that creates the component, loads a 3D model into the component, displays the model, and adjusts the model's viewability:

  • public Panel3D(int width): creates a 3D panel. Parameter width identifies the number of pixels on each side of the component. The value passed to this parameter establishes the component's preferred and minimum sizes. I set the component's minimum size so that the component is never shown smaller than its preferred size.
  • public void load(String filename): loads a 3D model from the text file identified by parameter filename, parses the model, stores parsed vertices and edges in the component, and repaints the component to present the model. A java.io.IOException object is thrown if an I/O problem occurs. If a parsing problem occurs, a java.text.ParseException object is thrown.
  • public void lookFrom(double x, double y, double z): establishes the viewpoint, or the position from which the 3D model is viewed. The viewpoint's x, y, and z coordinates are specified by the positive/negative values passed to parameters x, y, and z. The viewpoint always looks toward the model's (0, 0, 0) origin. This method does not repaint the component.
  • public void perspective(double ds): establishes the amount of perspective when viewing the 3D model. Parameter ds represents my textbook's D/S zoom ratio. A large ratio zooms into the model (a telephoto view). In contrast, a small ratio zooms out of the model (a wide-angle view). This method must be called after lookFrom(); it repaints the component.


To get comfortable with Panel3D's methods, consider an example that creates a 3D panel for use in an applet context. This component occupies 60 percent of the applet's width, loads the contents of a cube.3d model file (included with this article's code), lets an observer view the model from viewpoint (6.0, 8.0, 7.0), and sets the perspective to halfway between telephoto and wide-angle:

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources