Java
  Home arrow Java arrow Page 7 - Deployment Frameworks
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Deployment Frameworks
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 5
    2005-04-12

    Table of Contents:
  • Deployment Frameworks
  • Reading from a JAR File
  • Signing Applets
  • Deploying with Java Web Start
  • Isolating Optional Packages
  • Deploying Multiple Applets as One
  • Lifecycle
  • MultiApplet
  • CroftSoftCollection

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More Java Articles
    More By Apress Publishing


     

    JAVA ARTICLES

    - 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 ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT