Areas
  • J3D FAQ
  • Tutorials
  • Utilities
  • Books
  • News
  • Other Sites
  •  Chapters
  • TOC
  • Geometry
  • Lighting
  • Textures
  • Behaviours
  • User Feedback
  • Navigation
  • Audio
  • Input Devices
  • No Monitors
  •  Sections
  • Chapter TOC
  • Scene Graph Basics
  • Geometry
  • Textures
  • Event Model
  • Cameras and Viewing
  •   

    A Window to the World

    © Justin Couch 1999

    As we've mentioned, Java3D is a mid level 3D graphics API. It provides a structured scene graph approach to representing objects in 3D space. On top of this, it provides a system for building custom behaviours that act according to a variety of stimulus within the API. Low Level APIs usually don't provide this and must be taken care of by the overall application. J3D also provides a lot of glue capabilities, such as generic input device support and 3D sound. We'll go over this in more detail a little later.

    In the real world application, Java3D only provides the 3D part of the user interface. There is a lot of other support code that is needed in order to make it run, such as a window, mouse handling and other items. We'll now look at how Java3D fits with the rest of the Java APIs

    Fitting in with the Other Java APIs

    Java3D exists as part of the Java Media APIs. The various APIs in this framework provide the capabilities for integrating with other multimedia and Internet technologies. For example, you will find APIs for loading and displaying streaming video and audio (JMF), as well as collaborative networking (JSDT). Also, it works with parts of the standard Java 2 platform to provide integration with the windowing system and basic mouse input. Java3D provides only 3D rendering of graphics and sound. If you want to use sound, you also need to use some of the other Java Media APIs (JavaSound) to source the audio input. Similarly, it does not provide raw device input capabilities. It provides a virtual device and then you need to make use of other APIs like the Java Communications API or JINI to provide the low level device specific handling.

       Note
      If you want to know more about networking and Java 3D, I have written a book on this called Java 2 Networking. This provides you with all the background information you need to be able to build a networked 3D application using JSDT or the other standard Java networking APIs.
    In order to provide a decent level of performance, Java3D uses a lot of native code provided by the operating system libraries. On a Solaris based machine, this means making use of OpenGL rendering. MS Windows users may use either OpenGL or Direct3D variants.

    Native code use is restricted to only the final rendering steps while most of the core features are written in Java. This means a lot of usage of the AWT toolkit and Java2D APIs for use in providing basic windowing functionality.

       Note
      At the current time, the following platforms have Java 3D available for them:
      • Solaris 2.5+
      • Linux (Blackdown)
      • HPUX 10.20+ (check!)
      • IRIX 6.3+
      • Win32
      Macintosh support is not available, and we've heard no news of the potential any time soon. For more detailed information, you might wish to visit the Java 3D FAQ

    Generally, what all this means is that to build a complete Java3D application, you are going to need to use a lot more than just the one API set. For example, as you will see later, much of our mouse handling is performed using standard AWT mouse handling. Also, we assume that you are familiar with the Java2D requirements for dealing with issues such as graphics configuration and window handling.

    JDK 1.3 adds the capability to work with multiple monitors for your display. For the moment we will assume just the standard monitor capabilities and leave the multi-monitor code to Chapter 10: No Monitors.

    Resources

    Because of the approach that we are taking in this tutorial, you are going to require a lot of external help from various resources. Apart from finding yourself a good Java3D tutorial book, the following sites will be useful:

    The Basic window

    The first part in any Java3D application is to establish an area that can be used to render to. This is the Canvas3D class. The canvas provides a single view into your world and forms the basic interface with the windowing toolkit. Just like any other component, it may be added in any fashion, shown, hidden, be given mouse input and display output.

    Before we even start, we need to mention that everything, although using Java 2, will be developed using standard AWT for the windowing and mouse handling. Swing is not generally used for the examples because of problems that exist between Java 3D and the lightweight component architecture of Swing. However, for completeness, both versions of the initial windowing code are presented.

       Download
      Click on the link below to download the code for the code for this chapter.The files contain the completed code for this chapter.

    For simplicity, our basic application consists of a Frame with a simple menubar. To start with, the menu only allows us to close the application. The 3D window takes up all of the available space. We'll call the application class J3dTestFrame. This code is pretty straight forward, and you've probably done this a thousand times already. For this reason, we are only going to present the basic outlines of the important bits for the later text based on the AWT code.

    The display class consists of a simple frame, menu bar and the canvas. Within the constructor, the contents of the window is created.

    public class J3dAWTFrame extends Frame
      implements ActionListener, WindowListener
    {
      private MenuItem close_menu;
      private Canvas3D canvas;
    
      private UniverseManager universe;
    
      public J3dAWTFrame()
      {
        super("Java3D Tester");
    
        MenuBar menubar = new MenuBar();
    
        ...
    
        constructWorld();
    
        setSize(600, 600);
      }
    
      private void constructWorld()
      {
      }
    
      // code for listeners etc
      ...
    
      // The ever popular main method to start the app.
      public static void main(String[] args)
      {
        Frame frame = new J3dAWTFrame();
        frame.setVisible(true);
      }
    }
    
    The constructWorld() method is the center piece of where all the Java 3D scene graph is kept. In this code, we haven't placed the Java 3D rendering area yet. That is the next topic...

    Canvas3D: Your Window to the World

    With a basic window established, we now want to put in our 3D canvas. Creating a new Canvas3D takes a little more than just a single line of code. In the real world, what that canvas paints on to can be a myriad of different devices. We could have a set of 3D shutter glasses, a CAVE environment or just your lowly monitor.

    Java keeps all of this device specific information in a number of different classes. Java3D requires the use of a graphics configuration that must be extracted from the system and passed to the canvas with whatever options we need to work with. 3D also provides a number of extra features over the standard 2D display, such a stereo displays. To cater for this, it provides an extended version of the standard java.awt.GraphicsConfigTemplate called GraphicsConfigTemplate3D. Apart from the basic information, this template adds additional information dealing with depth buffering support and stereo rendering.

    To set up your basic canvas with no options you would use the following code:

    GraphicsConfigTemplate3D tmpl = new GraphicsConfigTemplate3D();
    GraphicsEnvironment env =
      GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice device = env.getDefaultScreenDevice();
    GraphicsConfiguration config = device.getBestConfiguration(tmpl);
    canvas = new Canvas3D(config);
    
    Then to finish, you create the canvas and it to the parent frame.

    canvas = new Canvas3D(config);
    
    add(canvas, BorderLayout.CENTER);
    
    Of course, a simpler way of doing this is to create a canvas with a null passed in, this achieves the same effect. This establishes your basic, flatscreen 3D canvas. There are other options that you may add to this in you code. For example you might want to add code to read a system property to decide if you should do stereo rendering, or to change the rendering quality.

      

    [ TOC ] [ Home ] [ FAQ ] [ Books ] [ Tutorials ] [ Utilities ] [ Contact Us ]