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.
Next: Lifecycle >>
More Java Articles
More By Apress Publishing
|
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.
|
|