Deploying Multiple Java Applets as One (Page 1 of 5 )
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).
Deploying Multiple Applets as One
In this section, I describe code you can modify and use as a demonstration framework for your own games. You can distribute it as an applet embedded in a web page, as an executable JAR file, or as a Java Web Start application. The main class, MultiApplet, is structured as a sort of super applet that can contain any number of other applets. As shown in Figure 2-2,MultiAppletallows the players to scroll through a list of your games embedded in this one applet. They can play each one at a time without having to restart the program or reload the page.MultiAppletalso provides a panel for displaying news and documentation about your games.

Figure 2-2. A MultiApplet example
MultiAppletStub
When a container first initializes anAppletinstance, it first calls itssetStub()method to pass an instance of interfaceAppletStub. TheAppletusesAppletStubto access theAppletContextand other properties.
package com.croftsoft.core.gui.multi;
import java.applet.*;
import java.net.*;
import com.croftsoft.core.lang.NullArgumentException;
[...]
public final class MultiAppletStub
implements AppletStub
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
{
private final Applet parentApplet;
private boolean active;
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
public MultiAppletStub ( Applet parentApplet )
//////////////////////////////////////////////////////////////////////
{
NullArgumentException.check ( this.parentApplet = parentApplet );
}
MultiAppletis an applet that contains other applets. It is also a containee of another applet container, usually a web browser, as shown in Figure 2-2. Acting as a container framework and not as a containee,MultiAppletmust provideAppletStubinstances to its child applets.MultiAppletStubimplements theAppletStubinterface for this purpose.
public void appletResize (
int width,
int height )
//////////////////////////////////////////////////////////////////////
{
parentApplet.resize ( width, height );
}
public AppletContext getAppletContext ( )
//////////////////////////////////////////////////////////////////////
{
return parentApplet.getAppletContext ( );
}
public URL getCodeBase ( )
//////////////////////////////////////////////////////////////////////
{
return parentApplet.getCodeBase ( );
}
public URL getDocumentBase ( )
//////////////////////////////////////////////////////////////////////
{
return parentApplet.getDocumentBase ( );
}
public String getParameter ( String name )
//////////////////////////////////////////////////////////////////////
{
return parentApplet.getParameter ( name );
}
MultiAppletStubprovides container services to a child applet by delegating calls to theparentApplet, usually an instance ofMultiApplet. For example, when a child game applet retrieves anAppletContextto call a method such asshowDocument(), it uses the sameAppletContextinstance that the parent applet received from the browser.
public boolean isActive ( )
//////////////////////////////////////////////////////////////////////
{
return active;
}
public void setActive ( boolean active )
//////////////////////////////////////////////////////////////////////
{
this.active = active;
}
TheAppletStubinterface defines theisActive()accessor method so that the running state of the applet can be determined.MultiAppletStubdefines the corresponding mutator method. Because a child game applet might be inactive while the parent is active, this property is not shared.
Next: MultiAppletNews >>
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.
|
|