The Java Game Framework.

Examples, tutorials, documentation and download of the Genuts Framework.
Games made with the Genuts Frameworks.
General articles around games.
Links covering all needs around game dev.
Who are we?
Terms of Service

Articles >> Java >> Inter-applet communication

Java: Inter-applet communication

What?

Using the AppletContext getApplet(String) and getApplets() methods, an applet can get Applet objects from the applets list running on the same page. Once an applet has another's Applet context, the first one is able to invoke methods on the second one.

Why?

Getting two or more applets within a single Web page to talk to each other has some benefits:
  • Many small applets that communicate with one another may together make up an entire big application.
  • In terms of Web page design, it's better in some cases to place small applets in different parts of the page, leaving the rest to be filled with text and images.

How?

The following sample shows the inter-applet communication working. A first applet, named "RC Toy", displays a square cursor. The second applet, named "Remote Control" and figuring a joystick, allows moving the first applet's cursor. The first applet implements the convenience, user defined, RemotelyControllable interface, specifying for this example the method that the "Remote Control" will use to move the cursor.

The most significant code parts are highlighted below. Feel free to download, test and play with the whole source code. Please notice that the Genuts Framework is required.

The RC Toy.

// The RemotelyControllable interface defines 
// a method that will allow the "Remote Control"
// to control the "RC Toy".

public interface RemotelyControllable {
  public void setXY(int x, int y);
}


[...]

// The "RC Toy" applet implements the 
// RemotelyControllable interface.

public class Toy extends Applet
  implements RemotelyControllable {

[...]


<!-- The applet's name is specified within the 
  <APPLET> tag -->

<APPLET CODE="Toy.class" ARCHIVE="genuts.jar"
  NAME="RCToy" WIDTH=160 HEIGHT=160></APPLET>


[...]

public class RemoteControl extends Applet 
  implements MouseListener, MouseMotionListener {

[...]

    // The "Remote Control" applet try to find the "RC Toy" 
    // applet by looking it up by name, specified with a 
    // <PARAM> tag, using the AppletContext getApplet 
    // method.
    Applet rc = null;
    String name = getParameter("RC");
    rc = getAppletContext().getApplet(name);

    if (rc != null && 
        rc instanceof RemotelyControllable) {
      // The "Remote Control" sends a message by invoking 
      // the "RC Toy" applet's method.
      ((RemotelyControllable)rc).setXY(x,y);
    }

[...]


<!-- The "RC Toy" applet's name is specified with a 
  <PARAM> tag -->
<APPLET CODE="RemoteControl.class" ARCHIVE="genuts.jar" 
  WIDTH=80 HEIGHT=80><PARAM NAME="RC" VALUE="RCToy"></APPLET>
The Remote Control - Drag me!.

And now, what are you going to control with your joystick?

Links

Articles >> Java >> Inter-applet communication