ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button O'Reilly Book Excerpts: Killer Game Programming in Java

Killer Game Programming in Java: A 3D Checkerboard, Part 1

Editor's note: Our book excerpt today is for all you Java gamers, especially the 3D junkies--we know you're out there. In part one of a two-part series taken from Chapter 15 of Killer Game Programming in Java author Andrew Davison describes how to create a scene in a Checkers3D application, using Java 3D. And check back next week, Andrew shows how to create a floating sphere for the Checkers3D app.

This chapter describes Checkers3D with a Java 3D example that creates a scene consisting of a dark green and blue tiled surface with labels along the x- and z-axes, a blue background, and a floating sphere lit from two different directions. The user (viewer) can travel through the scene by moving the mouse.

The lefthand screenshot in Figure 15-1 shows the initial view; the picture on the right shows the scene after the user has moved around a bit.

Figure 15-1
Figure 15-1. Initial view, and later

Checkers3D illustrates many of the common, and sometimes tricky, aspects of programming with Java 3D. For example, the 3D scene is displayed using the Java 3D Canvas3D class, which must be integrated with Java's Swing components. All Java 3D applications require a scene graph, and Checkers3D shows how to add basic shapes, lighting (ambient and directional), and a background. The scene graph diagram acts as a visual form of documentation, and a textual version of its information can be generated easily, with the help of Daniel Selman's Java3dTree package. (I'll supply details at the end of this chapter).

The floor and sphere utilize Java 3D's QuadArray, Text2D, and Sphere geometry classes: the floor is a series of quadrilaterals in a QuadArray, and labels are placed along the main axes of the floor using Text2D objects. The sphere shows how a 3D shape is colored, lit, and positioned in space. The user looks into the 3D world from a viewpoint. You'll see how it can be initially positioned, and how it can be moved during execution by using Java 3D's OrbitBehavior class.

Class Diagrams for Checkers3D

The class diagrams in Figure 15-2 show all the public and private data and methods for the Checkers3D application.

Figure 15-2
Figure 15-2. Class diagrams for Checkers3D

Checkers3D is the top-level JFrame for the application. WrapCheckers3D is a JPanel holding the scene graph, which is viewable via a Canvas3D object. CheckerFloor creates the subgraph for the floor (e.g., tiles, axes, etc.), with all the same colored tiles represented by a single ColoredTiles object.

TIP: The source code for this example is in the Checkers3D/ directory.

Pages: 1, 2, 3, 4

Next Pagearrow