Java
  Home arrow Java arrow Page 2 - Deploying Multiple Java Applets as One
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

Deploying Multiple Java Applets as One
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 3
    2008-04-24

    Table of Contents:
  • Deploying Multiple Java Applets as One
  • MultiAppletNews
  • 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


    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.

    More Java Articles
    More By Apress Publishing


       · This article is an excerpt from the book "Advanced Java Game Programming," published...
     

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

    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