Home arrow Java arrow Page 7 - Deployment Frameworks
JAVA

Deployment Frameworks


Once you have learned how to program games in Java, you will want to be able to deploy them on different types of deployment frameworks. This article will cover browser applets, executable JARs, and Java Web Start. It is excerpted from the book Advanced Java Game Programming written by David Wallace Croft (Apress, 2004; ISBN 1590591232).

Author Info:
By: Apress Publishing
Rating: 4 stars4 stars4 stars4 stars4 stars / 11
April 12, 2005
TABLE OF CONTENTS:
  1. · Deployment Frameworks
  2. · Reading from a JAR File
  3. · Signing Applets
  4. · Deploying with Java Web Start
  5. · Isolating Optional Packages
  6. · Deploying Multiple Applets as One
  7. · Lifecycle
  8. · MultiApplet
  9. · CroftSoftCollection

print this article
SEARCH DEVARTICLES

Deployment Frameworks - Lifecycle
(Page 7 of 9 )

Lifecycle

InterfaceLifecyclefrompackage com.croftsoft.core.
lang.lifecycle
defines all four of the Applet lifecycle methods: init(), start(), stop(), and destroy(). The purpose of the interface is to allow you to build a framework for manipulating lifecycle objects without requiring that they extend from the superclass Applet. For example, you might have a non-visual component that must be initialized, started, stopped, and destroyed that does not need to extend Applet or its superclass Panel.

You might have a framework that needs to initialize a number of objects upon startup and then destroy them upon shutdown. You could pass the objects to the framework as an array of interface Lifecycle instances so that a generic routine could initialize and destroy them. This is somewhat less than satisfactory, however, as the start() and stop() methods would be superfluous and you would need to implement them as blank method bodies.

Over time, I came to the conclusion that the best approach is to define an interface for each method and then extend those interfaces using multiple interface inheritance. Within package com.croftsoft.core.lang.lifecycle, you also find the interfaces Initializable, Startable, Stoppable, and Destroyable, each one defining a single lifecycle method. Interface Commissionable extends both Initializable and Destroyable. Interface Resumable extends Startable and Stoppable. Interface Lifecycle indirectly extends Initializable, Startable, Stoppable, and Destroyable by directly extending Commissionable and Resumable.

Class LifecycleLib in the same package contains a library of static methods for manipulating objects that implement the lifecycle interfaces. For example, static method destroy() takes an array of Destroyable instances as its argument and calls the destroy() method of each in turn, catching and reporting any exceptions thrown before moving on to the next element.

LifecycleWindowListener

For the demonstration framework to run as a Swing desktop application independent of a container such as a browser, it must be able to translate windowing events into the appropriate lifecycle method calls to your game. For example, when the window is activated for the first time, it should call the init() and start() methods to start animation. It should call stop() to suspend the animation when the window is minimized and destroy() when the window is closed.

package com.croftsoft.core.gui;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import com.croftsoft.core.awt.image.ImageLib;
import com.croftsoft.core.gui.FullScreenToggler;
import com.croftsoft.core.lang.NullArgumentException;
import com.croftsoft.core.lang.lifecycle.AppletLifecycle;
import com.croftsoft.core.lang.lifecycle.Lifecycle;
import com.croftsoft.core.lang.lifecycle.LifecycleLib;

public final class LifecycleWindowListener
  implements WindowListener
////////////////////////////////////////////////////////
///////////////////////////////////////////////////////
{

private final Lifecycle [ ] lifecycles;

private final String          shutdownConfirmationPrompt;

private final String          shutdownConfirmationTitle;

//

private boolean initialized;

[...]


public LifecycleWindowListener (
  Lifecycle [ ] lifecycles,
  String        shutdownConfirmationPrompt,
  String        shutdownConfirmationTitle )
/////////////////////////////////////////////////////////
{
 this.lifecycles = lifecycles;

 this.shutdownConfirmationPrompt=
                        
shutdownConfirmationPrompt;

 this.shutdownConfirmationTitle =
                         shutdownConfirmationTitle;
}

[...]

public void windowActivated ( WindowEvent windowEvent )
/////////////////////////////////////////////////////////
{
  if ( !initialized )
  {
   LifecycleLib.init ( lifecycles );
   initialized = true;
}
LifecycleLib.start ( lifecycles );
}

[...]

public void windowClosing ( WindowEvent windowEvent )
/////////////////////////////////////////////////////////
{
  Window window = windowEvent.getWindow ( );

  if ( shutdownConfirmationPrompt != null )
  {
    int confirm = JOptionPane.showOptionDialog ( window, 
      shutdownConfirmationPrompt,
      shutdownConfirmationTitle != null
        ? shutdownConfirmationTitle : 
      shutdownConfirmationPrompt,  
      JOptionPane.YES_NO_OPTION,
      
JOptionPane.QUESTION_MESSAGE,
      null, null, null );
if ( confirm != JOptionPane.YES_OPTION )
{
  return;
 }
}

  window.hide ( );

  if ( shutdownConfirmationPrompt == null )
  {
   LifecycleLib.stop ( lifecycles );
  }

   LifecycleLib.destroy ( lifecycles );

   window.dispose ( );

   System.exit ( 0 );
  }

  public void windowDeactivated (WindowEvent windowEvent)
  ////////////////////////////////////////////////////////
  {
   LifecycleLib.stop ( lifecycles );
  }

  [...]

Class LifecycleWindowListener in package com.croftsoft.core.gui is an implementation of abstract class WindowListener in package java.awt.event. You use it by passing it an array of objects that implement the Lifecycle interface. When the window is activated for the first time it calls the init() method on the Lifecycle objects. It also calls the start() method every time. When the window is deactivated, it calls the stop() method. This means that your game animation resumes when the user clicks the window or maximizes it and is suspended when the user clicks outside the window or minimizes it.

When the user clicks the close window icon, it displays a confirmation display window on top. This deactivates the parent window which results in a call to the game object stop() method, suspending game animation. If the user then decides to proceed with the shutdown, it will call the destroy() method before exit. If the user decides to cancel instead, the confirmation display is dismissed. This reactivates the parent window and results in a call to the game object start() method, resuming game animation.

 public static void  launchFrameAsDesktopApp (
   JFrame                jFrame,
   final Lifecycle [ ]   lifecycles,
   Dimension              frameSize,
   String                 shutdownConfirmationPrompt )
 /////////////////////////////////////////////////////////
 {
   NullArgumentException.check ( jFrame );

   jFrame.setDefaultCloseOperation  (
     WindowConstants.DO_NOTHING_ON_CLOSE );

   jFrame.addWindowListener ( new LifecycleWindowListener(
   lifecycles, shutdownConfirmationPrompt ) );

 if ( frameSize != null )
 {
  WindowLib.centerOnScreen ( jFrame, frameSize );
 }
 else
 {
  WindowLib.centerOnScreen ( jFrame, 0.8 );
 }

 jFrame.show ( );
}

Static method launchFrameAsDesktopApp() is available within the same class. It adds a new LifecycleWindowListener instance to the frame and then calls its show() method. Showing the window activates it, which calls the lifecycle init() and start() methods of your game object. The Lifecycle array constructor argument passes in the game object. If no frameSize is specified, it defaults to 80 percent of the screen size.

 public static void main ( String [ ] args )
 ////////////////////////////////////////////////////////
 {
  launchFrameAsDesktopApp (
   new JFrame ( "Test" ),
   new Lifecycle [ ] {
     new Lifecycle ( ) 
     {
       
public void init ( ) { System.out.println 
        ( "init" ); }
       public void start ( ) { System.out.println
        ( "start" ); }

       public void stop ( ) { System.out.println   
        ( "stop" ); }                          

    public void destroy ( )   
                 { System.out.println   ( "destroy" ); }
   } },
 null, // frameSize
 "Exit Test?" );
}

Class LifecycleWindowListener contains a static main() method that you can use to test and demonstrate its functionality by launching it from the command-line prompt. As you manipulate the window containing the test program, the lifecycle methods print debugging messages to the standard output.

This article is excerpted from Advanced Java Game Programming by David Wallace Croft (Apress, 2004; ISBN 1590591232). Check it out at your favorite bookstore today. Buy this book now.


blog comments powered by Disqus
JAVA ARTICLES

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials