![]() | ![]() | ![]() |
|
A Window to the World© Justin Couch 1999As 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 APIsJava3D 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.
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.
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. ResourcesBecause 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 windowThe 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.
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 WorldWith 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
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.
|
|