Deploying Multiple Java Applets as One - CroftSoftCollection
(Page 5 of 5 )
CroftSoftCollectionis the demonstration program that is compiled and launched when you run the default target in the Ant build file. By modifying the constants, you can use it as template code to distribute your own collection of games within aMultiApplet.
package com.croftsoft.apps.collection;
import java.awt.Dimension;
import java.net.URL;
import com.croftsoft.core.CroftSoftConstants;
import com.croftsoft.core.gui.multi.MultiApplet;
import com.croftsoft.core.lang.Pair;
public final class CroftSoftCollection
extends MultiApplet
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
{
[...]
CroftSoftCollectionextendsMultiApplet so that it can run within an applet container.
private static final Pair [ ] APPLET_PAIRS = {
new Pair (
"BackpropXOR",
"com.croftsoft.apps.backpropxor.BackpropXor" ),
new Pair (
"Clock",
"com.croftsoft.apps.clock.DigitalClock" ),
[...]
new Pair (
"Zombie",
"com.croftsoft.apps.zombie.Zombie" ) };
It defines theAPPLET_PAIRSarray to be passed as an argument toMultiApplet. There is no pre-defined limit to the length of the array. Whenever I create a new game, I usually insert it into the array in alphabetical order. The preceding code shows only three of the array elements.
private static final String NEWS_NAME
= MultiApplet.DEFAULT_NEWS_NAME;
private static final String NEWS_HTML
= "<html><body><pre>"+ APPLET_INFO +
"</pre></body></html>";
private static final String NEWS_PAGE
= "http://www.croftsoft.com/portfolio/collection/news/";
When you create your own subclass ofMultiAppletto deploy your own games, you will want to change theseMultiAppletNewsconstants. You will also probably want to load the text ofNEWS_HTMLfrom a resource file within the JAR instead of defining it as a compiled constant as I have done here.
private static final String FRAME_TITLE = TITLE;
private static final String FRAME_ICON_FILENAME
= CroftSoftConstants.FRAME_ICON_FILENAME;
private static final Dimension FRAME_SIZE = null;
private static final String SHUTDOWN_CONFIRMATION_PROMPT
= "Close " + TITLE + "?";
You can also customize the frame variables in your own subclass. You will probably want to start by copyingCroftSoftCollection.java, renaming the package and class name, and replacing one of the games with one of your own.
public static void main ( String [ ] args )
throws Exception
//////////////////////////////////////////////////////////////////////
{
System.out.println ( APPLET_INFO );
MultiApplet.launch(
APPLET_INFO,
APPLET_PAIRS,
NEWS_NAME,
NEWS_HTML,
NEWS_PAGE,
FRAME_TITLE,
FRAME_ICON_FILENAME,
CroftSoftCollection.class.getClassLoader ( ),
FRAME_SIZE,
SHUTDOWN_CONFIRMATION_PROMPT );
}
Note that themain()method does not create an instance ofCroftSoftCollection. It simply passes the static constants defined in that class to the staticlaunch()method ofMultiApplet.
public CroftSoftCollection ( )
//////////////////////////////////////////////////////////////////////
{
super (
APPLET_INFO,
APPLET_PAIRS,
NEWS_NAME,
NEWS_HTML,
NEWS_PAGE );
}
When theCroftSoftCollectionis used as an applet, however, you must provide a no-argument constructor that passes these constants to the constructor method of the superclassMultiApplet.
<javac srcdir="${srcdir}" destdir="jar">
<include name="com/croftsoft/apps/collection/ CroftSoftCollection.java"/>
<include name="com/croftsoft/apps/backpropxor/ BackpropXor.java"/>
<include name="com/croftsoft/apps/clock/DigitalClock.java"/>
[...]
<include name="com/croftsoft/apps/zombie/Zombie.java"/>
</javac>
<jar
jarfile="collection.jar"
basedir="jar"
manifest="bld/apps/collection/ manifest.txt"/>
Because these game applets are dynamically linked, you have to take care to make sure that they are explicitly included in your build. The preceding code is an excerpt from my Antbuild.xmlfile. Note that if the classes were statically linked, you would only need the firstincludetag.
Summary
In this chapter, I introduced three standard Java game deployment frameworks: browser-based applets, executable JARs, and Java Web Start. I discussed the advantages and disadvantages of each with regard to different features including security. Throughout the chapter, I covered programming techniques for
creating your own games including animation thread management, isolating optional packages using custom interfaces and dynamic linking, external browser access through JNLP, using a frame as a stand-alone container, and displaying web pages using Swing. The chapter ended with an examination of the source code of MultiApplet, a reusable framework that you can use to deploy your own games.
1. http://java.sun.com/j2se/1.4.1/docs/ guide/misc/threadPrimitiveDeprecation.html
2. http://java.sun.com/j2se/1.4.2/docs/ guide/plugin/
3. http://java.sun.com/j2se/1.4.2/docs/ guide/plugin/
4. http://java.sun.com/products/ javawebstart/download-spec.html,
http://java.sun.com/j2se/1.4.2/ docs/guide/jws/developersguide/contents.html
5. Kim Topley, Core Swing Advanced Programming (Upper Saddle River, NJ: Prentice Hall PTR, 2000).
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
|
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.
|
|