Chapter 20. An Articulated, Moveable Figure


A Full-Screen Version of the Application

The AnimTourFS application is essentially AnimTour3D, but with the original AnimTour3D and WrapAnimTour3D classes replaced by a single new class, AnimTourFS. AnimTourFS contains the new FSEM-related code. The rest of the classes are unchanged, as Figure 19-7 shows.

Application invocation must include the option -Dsun.java2d.noddraw=true:

     java -cp %CLASSPATH%;ncsa\portfolio.jar  -Dsun.java2d.noddraw=true AnimTourFS

The nodraw property switches off the use of Window's DirectDraw for drawing AWT elements and off-screen surfaces. This avoids a problem which first appeared when using the OpenGL version of Java 3D 1.3 with J2SE 1.4 and DirectX 7.0. Version numbers have moved on since then, so you may want to see what happens without the nodraw option.

Creating the Full-Screen Scene

Figure 19-8 shows all the methods in the AnimTourFS class and should be compared with Figure 19-3, which lists the methods in the old WrapAnimTour3D class.

main( ) is new; all the other changes are inside the AnimTourFS( ) constructor (with the addition of some new private global variables).

Figure 19-7. Class diagrams for AnimTourFS


Figure 19-8. Methods in AnimTourFS


FSEM works poorly with Swing components, so AnimTourFS( ) uses a Frame object as the top-level window and embeds the Canvas3D GUI inside it:

     private Frame win;   // global, required at quit time     GraphicsConfiguration config =             SimpleUniverse.getPreferredConfiguration( );     win = new Frame("AnimTourFS", config);   // use SU's preference     win.setUndecorated(true) ;                // no menu bar, borders, etc. or Swing components     win.setResizable(false);    // fixed size display     Canvas3D canvas3D = new Canvas3D(config);    // set up canvas3D     win.add(canvas3D);

The graphics configuration of the Frame is set to the one preferred by Java 3D. FSEM likes a fixed-size window with no decoration.

FSEM is handled through a GraphicsDevice object representing the screen, which is accessed via a GraphicsEnvironment reference. There may be several GraphicsDevice objects for a machine if, for example, it uses dual monitors. However, for the normal single-monitor system, the getdefaultScreenDevice( ) method is sufficient:

     private GraphicsDevice gd;   // global     GraphicsEnvironment ge =       GraphicsEnvironment.getLocalGraphicsEnvironment( );     gd = ge.getDefaultScreenDevice( );

Once the GraphicsDevice object (gd) has been obtained, it's a good idea to check if FSEM is supported by the user's OS before attempting to use it:

     if (!gd.isFullScreenSupported( )) {       System.out.println("FSEM not supported.") ;       System.out.println("Device = " + gd) ;       System.exit(0) ;     }     gd.setFullScreenWindow(win); // set FSEM     if (gd.getFullScreenWindow( ) == null)       System.out.println("Did not get FSEM");     else       System.out.println("Got FSEM") ;

If GraphicsDevice.setFullScreenWindow( ) cannot switch to FSEM mode, then it will position the window at (0,0) and resize to fit the screen, so the application can continue without being overly affected.

The final task is to switch off FSEM when the program terminates, which is done as part of the response to a quit key being pressed:

     canvas3D.addKeyListener( new KeyAdapter( ) {       public void keyPressed(KeyEvent e)       { int keyCode = e.getKeyCode( );         if ((keyCode == KeyEvent.VK_ESCAPE) || (keyCode == KeyEvent.VK_Q) ||             (keyCode == KeyEvent.VK_END) ||             ((keyCode == KeyEvent.VK_C) && e.isControlDown( )) ) {           gd.setFullScreenWindow(null);  // exit FSEM           win.dispose( );           System.exit(0);         }       }     });

Figure 19-9 shows the FSEM in operation. Note the absence of a window frame or any other decoration.

Figure 19-9. AnimTourFS in action


Changing the Display Mode

Once an application is in FSEM, further performance gains may be available by adjusting the screen's display mode (i.e., its bit depth, height, width, and refresh rate). Bit depth is the number of bits per pixel, and refresh rate is how frequently the monitor updates itself. Reducing the bit depth will increase rendering speed, but there may be an impact on the quality of the textures and other images in the scene.

A crucial thing to do when changing the display mode is to store the original version so it can be restored at the end of execution, as shown in the following code. In AnimTourFS( ), you should change the display mode after FSEM has been initiated:

     private DisplayMode origDM = null;  // global     if (gd.isDisplayChangeSupported( )) {       origDM = gd.getDisplayMode( );       gd.setDisplayMode(            new DisplayMode( origDM.getWidth( ), origDM.getHeight( ),                     origDM.getBitDepth( )/2,                     origDM.getRefreshRate( )  ));     }

The code checks if display changing is supported via a call to GraphicsDevice.isDisplayChangeSupported( ), stores the original display mode, and updates the mode using the original screen dimensions and refresh rate but halving the bit depth (with the aim of increasing the rendering speed).

This reduces the bit depth of my machine from 32 to 16 bits.


The original display mode is restored at the end of the program by two extra lines in the quit key listener:

     if ((keyCode == KeyEvent.VK_ESCAPE) || (keyCode == KeyEvent.VK_Q) ||         (keyCode == KeyEvent.VK_END) ||         ((keyCode == KeyEvent.VK_C) && e.isControlDown( )) ) {       if (origDM != null)  // original was saved              gd.setDisplayMode(origDM);       gd.setFullScreenWindow(null);       win.dispose( );       System.exit(0);     }

Figure 19-10 shows AnimTourFS with its reduced bit depth.

Figure 19-10. Reduced bit depth AnimTourFS


The 24-bit JPEG used for the background has become rather pixilated as you would expect with only 16 bits per pixel. I didn't notice any acceleration in rendering, but that may be because the scene is already so easy to render.

The Java FSEM tutorial (http://java.sun.com/docs/books/tutorial/extra/fullscreen/) includes a section on display modes and an example showing how to use them.




Killer Game Programming in Java
Killer Game Programming in Java
ISBN: 0596007302
EAN: 2147483647
Year: 2006
Pages: 340

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net