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).
public final class MultiAppletNews extends JPanel ////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////// {
MultiAppletNewsinherits fromJPanelinstead ofJAppletbecause it is not animated and does not need the lifecycle methods.
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 ( ) );
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.
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 );
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.
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.