Skip to Main Content
Killer Game Programming in Java
book

Killer Game Programming in Java

by Andrew Davison
May 2005
Intermediate to advanced content levelIntermediate to advanced
998 pages
26h
English
O'Reilly Media, Inc.
Content preview from Killer Game Programming in Java

Integrating Java 3D and Swing

Checkers3D is a JFrame where GUI controls, such as Swing text fields and buttons, would be placed if necessary. In this example, it creates an instance of WrapCheckers3D (a JPanel) and places it in the center of a BorderLayout:

    c.setLayout( new BorderLayout() );
    WrapCheckers3D w3d = new WrapCheckers3D();   // panel for 3D canvas
    c.add(w3d, BorderLayout.CENTER);

The Canvas3D view onto the scene is created inside WrapCheckers3D:

    public WrapCheckers3D()
    {
      setLayout( new BorderLayout() );
      // other initialization code

      GraphicsConfiguration config =
                 SimpleUniverse.getPreferredConfiguration();
      Canvas3D canvas3D = new Canvas3D(config);
      add("Center", canvas3D);

      // other initialization code}

Some care must be taken when using Canvas3D since it's a heavyweight GUI element (a thin layer over an OS-generated window). Heavyweight components aren't easily combined with Swing controls, which are lightweight; the controls are mostly generated by Java. Problems are avoided if the Canvas3D object is embedded in a JPanel; then the panel can be safely integrated with the rest of the Swing-built application.

Tip

There's a detailed discussion of the issues related to combining Canvas3D and Swing at j3d.org (http://www.j3d.org/tutorials/quick_fix/swing.html).

Compared to applications in earlier chapters, there's no update/draw animation loop. This is unnecessary because Java 3D contains its own mechanism for monitoring changes in the scene and initiating rendering. Here is the algorithm ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Java Game Development with LibGDX: From Beginner to Professional

Java Game Development with LibGDX: From Beginner to Professional

Lee Stemkoski

Publisher Resources

ISBN: 0596007302Supplemental ContentErrata Page