Home arrow Java arrow Page 2 - Deploying Multiple Java Applets as One
JAVA

Deploying Multiple Java Applets as One


In this conclusion to a three-part series covering the three different kinds of deployment frameworks you can use with Java games, you'll learn how to deploy multiple Java applets as if they were all one applet. This article is excerpted from chapter two of Advanced Java Game Programming, written by David Wallace Croft (Apress; ISBN: 1590591232).

Author Info:
By: Apress Publishing
Rating: 3 stars3 stars3 stars3 stars3 stars / 9
April 24, 2008
TABLE OF CONTENTS:
  1. · Deploying Multiple Java Applets as One
  2. · MultiAppletNews
  3. · Lifecycle
  4. · MultiApplet
  5. · CroftSoftCollection

print this article
SEARCH DEVARTICLES

Deploying Multiple Java Applets as One - MultiAppletNews
(Page 2 of 5 )

You use classMultiAppletNewsin packagecom.croftsoft.core.gui.multito 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
  //////////////////////////////////////////////////////////////////////
  //////////////////////////////////////////////////////////////////////
  {

MultiAppletNewsinherits fromJPanelinstead ofJAppletbecause 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 itsappletContextto launch new browser frames when a user clicks a hyperlink. ThejEditorPane displays the initial web page. If no initial HTML is specified,DEFAULT_NEWS_HTMLis 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, theappletContextis 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 variableappletContextis declared final, the assignment is made using a temporary method variable.

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

ThenewsHTMLis displayed initially while thenewsPageis being downloaded. IfnewsPageis null or the download fails, thenewsHTML will continue to be displayed. The preceding code provides default values fornewsHTML.

  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 ofJEditorPanefrom core packagejavax.swingis 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. AHyperlinkListeneris added so it can intercept mouse clicks on hypertext links and download and display the appropriate web page. For a tutorial on usingJEditorPaneandHyperlinkListener, 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,MultiAppletNewsattempts to download the web page atnewsURL. 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 aHyperlinkEvent. The destinationurlcan then be retrieved from theHyperlinkEventand displayed. To display the web page, three different mechanisms are attempted. If anAppletContextis 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 usesJEditorPaneto display the web page. Note that the ability ofJEditorPaneto display web pages is limited compared to most browsers.


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