Deployment Frameworks - CroftSoftCollection
(Page 9 of 9 )
CroftSoftCollection
CroftSoftCollection is 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 a MultiApplet.
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
/////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
{
[...]
CroftSoftCollection extends MultiApplet 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 the APPLET_PAIRS array to be passed as an argument to MultiApplet. 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></htm1>";
private static final String NEWS_PAGE
= "http://www.croftsoft.com/portfolio/collection/news/";
When you create your own subclass of MultiApplet to deploy your own games, you will want to change these MultiAppletNews constants. You will also probably want to load the text of NEWS_HTML from 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 copying CroftSoftCollection.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 the main() method does not create an instance of CroftSoft-Collection. It simply passes the static constants defined in that class to the static launch() method of MultiApplet.
public CroftSoftCollection ( )
///////////////////////////////////////////////////////
{
super (
APPLET_INFO,
APPLET_PAIRS,
NEWS_NAME,
NEWS_HTML,
NEWS_PAGE );
}
When the CroftSoftCollection is used as an applet, however, you must provide a no-argument constructor that passes these constants to the constructor method of the superclass MultiApplet.
<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 Ant build.xml file. Note that if the classes were statically linked, you would only need the first include tag.
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.
- http://java.sun.com/j2se/1.4.1/docs/guide/
misc/threadPrimitiveDeprecation.html
- http://java.sun.com/j2se/1.4.2/docs/guide/plugin/
- http://java.sun.com/j2se/1.4.2/docs/guide/plugin/
- http://java.sun.com/products/javawebstart/download-spec.html,
http://java.sun.com/j2se/1.4.2/docs/guide/
jws/developersguide/contents.html
- Kim Topley, Core Swing Advanced Programming (Upper Saddle River, NJ: Prentice Hall PTR, 2000).
Further Reading Schmidt, Rene W. Java Network Launching Protocol & API Specification. http://java.sun.com/products/javawebstart/download-spec.html. Sun Microsystems. Java Plug-in 1.4.2 Developer Guide. http://java.sun.com/j2se/1.4.2/docs/guide/plugin/. Sun Microsystems. Java Web Start 1.4.2 Developer Guide.
http://java.sun.com/j2se/1.4.2/docs/guide/jws/ developersguide/contents.html. Sun Microsystems. “Why Are Thread.stop, Thread.suspend, Thread.resume and Runtime.runFinalizersOnExit Deprecated?”
http://java.sun.com/j2se/1.4.1/docs/guide/
misc/threadPrimitiveDeprecation. html.
Topley, Kim. “JEditorPane and the Swing HTML Package.” Chapter 4 in Core Swing Advanced Programming. Upper Saddle River, NJ: Prentice-Hall PTR, 2000.
This article is excerpted from Advanced Java Game Programming by David Wallace Croft (Apress, 2004; ISBN 1590591232). Check it out at your favorite bookstore today. Buy this book now. |
| 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. |