Java
  Home arrow Java arrow Page 6 - 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  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
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 - Deploying Multiple Applets as One


    (Page 6 of 9 )

    Deploying Multiple Applets as One

    In this section, I describe code you can modify and use as a demonstration framework for your own games. You can distribute it as an applet embedded in a web page, as an executable JAR file, or as a Java Web Start application. The main class, MultiApplet, is structured as a sort of super applet that can contain any number of other applets. As shown in Figure 2-2, MultiApplet allows the players to scroll through a list of your games embedded in this one applet. They can play each one at a time without having to restart the program or reload the page. MultiApplet also provides a panel for displaying news and documentation about your games.


    Figure 2-2.  A MultiApplet example

    MultiAppletStub

    When a container first initializes an Applet instance, it first calls its setStub() method to pass an instance of interface AppletStub. The Applet uses AppletStub to access the AppletContext and other properties.

      package com.croftsoft.core.gui.multi;

     
    import java.applet.*;
      import java.net.*;

      import com.croftsoft.core.lang.NullArgumentException;


      [...]

      public final class  MultiAppletStub
       
    implements AppletStub
      //////////////////////////////////////////////////////
      ///////////////////////////////////////////////////// 
      {

      private final Applet parentApplet;

      private boolean  active;
      ////////////////////////////////////////////////////////
      ///////////////////////////////////////////////////////

      public MultiAppletStub ( Applet parentApplet )
      ////////////////////////////////////////////////////// 
      {
      NullArgumentException.check ( this.parentApplet
            = parentApplet );
      }

    MultiApplet is an applet that contains other applets. It is also a containee of another applet container, usually a web browser, as shown in Figure 2-2. Acting as a container framework and not as a containee, MultiApplet must provide AppletStub instances to its child applets. MultiAppletStub implements the AppletStub interface for this purpose.

      public void appletResize (
        int width,
       
    int height )
      //////////////////////////////////////////////////////// 
      {
        parentApplet.resize ( width, height );
      }

      public AppletContext getAppletContext ( )
      ///////////////////////////////////////////////////////  
      {
        return parentApplet.getAppletContext ( );
      }

        public URL getCodeBase ( )
      /////////////////////////////////////////////////////// 
      {
          return parentApplet.getCodeBase ( );
      }

      public URL getDocumentBase ( )
      //////////////////////////////////////////////////    
      {
        return parentApplet.getDocumentBase ( );
      }

      public String getParameter ( String name )
      /////////////////////////////////////////////////////// 
      {
        return parentApplet.getParameter ( name );
      }

    MultiAppletStub provides container services to a child applet by delegating calls to the parentApplet, usually an instance of MultiApplet. For example, when a child game applet retrieves an AppletContext to call a method such as showDocument(), it uses the same AppletContext instance that the parent applet received from the browser.

      public boolean  isActive ( )
      ///////////////////////////////////////////////////// 
      {
        return active;
      }

      public void setActive ( boolean active )
      /////////////////////////////////////////////         
      {
        this.active = active;
      }

    The AppletStub interface defines the isActive() accessor method so that the running state of the applet can be determined. MultiAppletStub defines the corresponding mutator method. Because a child game applet might be inactive while the parent is active, this property is not shared.

    MultiAppletNews

    You use class MultiAppletNews in package com.croftsoft.core.gui.multi to display online news and documentation for your games (see Figure 2-3).


    Figure 2-3.  MultiAppletNews

      package com.croftsoft.core.gui.multi;

      import java.applet.*;
      import java.awt.*;
      import java.awt.event.*;
      import java.io.*;
      import java.net.*;
      import javax.swing.*;
      import javax.swing.event.*;
      import javax.swing.text.*;
      import javax.swing.text.html.*;

      import com.croftsoft.core.CroftSoftConstants;
      import com.croftsoft.core.lang.NullArgumentException;
      import com.croftsoft.core.jnlp.JnlpLib;

      public final class MultiAppletNews
      extends JPanel
      ///////////////////////////////////////////////////////
      //////////////////////////////////////////////////////
      {

    MultiAppletNews inherits from JPanel instead of JApplet because it is not animated and does not need the lifecycle methods.

    private static final String DEFAULT_NEWS_HTML
      = "<html><body><pre>"
      + CroftSoftConstants.DEFAULT_ATTRIBUTION_NOTICE
      + "</pre></body></html>";
      //

    private final AppletContext appletContext;

    private final JEditorPane jEditorPane;

    If the framework is running as an applet in a browser instead of as a desktop application, MultiAppletNews uses its appletContext to launch new browser frames when a user clicks a hyperlink. The jEditorPane displays the initial web page. If no initial HTML is specified, DEFAULT_NEWS_HTML is used.

      public MultiAppletNews (
        String newsHTML,
        String newsPage,
        Applet applet

      ////////////////////////////////////////////////////////  {
        super ( new BorderLayout ( ) );

        AppletContext appletContext = null;

        try
        {
          appletContext = applet.getAppletContext ( );
        }
        catch ( Exception ex )
        {
        }

        this.appletContext = appletContext;

    Within the constructor, the appletContext is retrieved from the applet. This might throw an exception if the applet is null or if the applet is not running within an applet container. Because instance variable appletContext is declared final, the assignment is made using a temporary method variable.

      if ( newsHTML == null )
      {
        if ( newsPage != null )
       {
        newsHTML
           = "<htm1><body>"
           + "Loading "
           + newsPage
           + "..."
           + "<body><htm1>";
      }
      else
      {
        newsHTML = DEFAULT_NEWS_HTML;
      }
    }

    The newsHTML is displayed initially while the newsPage is being downloaded. If newsPage is null or the download fails, the newsHTML will continue to be displayed. The preceding code provides default values for newsHTML.

      jEditorPane = new JEditorPane ( "text/html", newsHTML );

      jEditorPane.setEditable ( false );

      jEditorPane.setCaretPosition ( 0 );

      jEditorPane.addHyperlinkListener (
        new HyperlinkListener ( )
        {
         public void hyperlinkUpdate ( HyperlinkEvent
                 hyperlinkEvent
         {
         processHyperlinkEvent (hyperlinkEvent );
         }
       } );

    add ( new JScrollPane( jEditorPane ),
    BorderLayout.CENTER );

    An instance of JEditorPane from core package javax.swing is used to display the initial web page in the first panel of the multi-applet framework. The caret position is set to zero so that the top of the page will be displayed instead of scrolling to the bottom when the page is initially loaded. A HyperlinkListener is added so it can intercept mouse clicks on hypertext links and download and display the appropriate web page. For a tutorial on using JEditorPane and HyperlinkListener, I recommend Chapter 4, “JEditorPane and the Swing HTML Package” of Core Swing Advanced Programming by Kim Topley.5

      if ( newsPage != null )
      {
       try
       {
         final URL newsURL = new URL ( newsPage );

         new Thread (
           new Runnable ( )
           {
            public void run ( )
            {
              try
              {
                jEditorPane.setPage ( newsURL );
              }
              catch ( Exception ex )
              {
               ex.printStackTrace ( );
              }
            }
          } ).start ( );
      }
      catch ( MalformedURLException ex )
      {
       ex.printStackTrace ( );
      }
     }
    }

    When initialized, MultiAppletNews attempts to download the web page at newsURL. This is launched in a separate thread because it might stall indefinitely when there is a network problem.

     private void processHyperlinkEvent ( HyperlinkEvent 
     hyperlinkEvent )
     ////////////////////////////////////////////////////////
     {
       try
       {
     if ( hyperlinkEvent.getEventType ( )
      == HyperlinkEvent.EventType.ACTIVATED )
    {
      if ( hyperlinkEvent instanceof HTMLFrameHyperlinkEvent )
      {
       HTMLDocument  htmlDocument
         = ( HTMLDocument ) jEditorPane.getDocument ( );
       htmlDocument.processHTMLFrameHyperlinkEvent (
         ( HTMLFrameHyperlinkEvent ) hyperlinkEvent );
      }
      else
      {
       URL url = hyperlinkEvent.getURL ( );
       if ( appletContext != null )
       {
        appletContext.showDocument ( url, "_blank" );
       }
       else
       {
        
    try
         {
           JnlpLib.showDocument ( url );
         }
         catch ( UnsupportedOperationException ex )
         {
          jEditorPane.setPage ( url );
         }
        }
       }
     }
    }
    catch ( Exception ex )
    {
      ex.printStackTrace ( );
     }
    }

    Clicking a hyperlink generates a HyperlinkEvent. The destination url can then be retrieved from the HyperlinkEvent and displayed. To display the web page, three different mechanisms are attempted. If an AppletContext is available, the code uses it to show the URL in a new browser window. Otherwise, it attempts to launch an external browser using JNLP. If this also fails, the code uses JEditorPane to display the web page. Note that the ability of JEditorPane to display web pages is limited compared to most browsers.

    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