Tricks of the java programing gurus
By Glenn L. Vanderburg. et al.
Communication between the 1.applet
table of Contents
GetApplet: Official mechanism static variables and method network communication based communication summary
Consider the task you want to complete, an applet, even a few separate applets, sometimes not enough. Fortunately, the applet can communicate, and you can complete some more complex tasks through collaboration. The effectiveness that a group of collaboration can make the single applet cannot be comparable.
Communication between Applets can be implemented by traditional methods: Applets can call each other's member methods or communicate via Socket or data stream. In fact, there are many ways to find each other between the applet, and each method has its own advantages and disadvantages. This article will discuss four communication mechanisms, and give a more complex example, in this example we will use one of the communication mechanisms.
GetApplet: "Official" mechanism
The Java API itself is useful to support the characteristics of the Applet programming: the getApplet and GetApplets methods for the AppletContext class. With these two functions, the Applet program can find and access each other by name. You can call getApplet like this:
Applet friend = getappletContext (). Getapplet ("friend");
Once the call ends, the variable Friend has become an instance of the applet called "Friend" (if such a "Friend" Applet exists). For example: If "Friend" is an instance of Sun's Animator Applet, Friend will include a reference to this instance.
The name of the applet is specified in HTML instead of in the Java code. In order to create an Animator applet found by the previous instance code, you can insert the following lines in HTML:
The getApplets method and getApplet are similar, but getapplets returns an enumeration, which lists all Applets that can be accessed. Then we can query an applet based on the different properties of the applet, including the name of the applet. The following demonstrates how to find an applet called "Friend" with getapplets:
Applet friends; for (enumeration E = getAppletContext (). Getapplets (); E.haASMoreElements (); {try {applet t = (applet) E.NEXTELEMENT (); if ("friends" .Equals (t.GetParameter) "name"))) {friend = T; Break;}} catch (classcastexception e) {}}
Obviously, the above methods have a lot of work, you will not use this way to find an applet. However, if you want to query multiple applets, or you don't know the exact name of the applet you want to find, this will be a way to go. For example, use GetApplets to find all applets named after "Helper" will be easy, your applet can communicate with all Helper Applet in the same page. Unfortunately, this officially recommended Applet communication mechanism has at least two problems. First, this mechanism is currently not particularly embodied, so different applications have different implementations of this mechanism. For example, getApplets returns an applet that can be accessed, but what is accessible? There is no clear definition. You may get the applet on the same page, an applet loaded from the same site, or the intersection of both, depending on the browser running the applet. These problems have prompted Sun and Java's licensees to make a more strict, clearly defined Applet collaborative mechanism. However, so far, the inconsistency of this applet communication and value is still a headache.
Another problem is not so simple. It looks easy to understand, but it will make things complicated to surprise Multi Applet programmers. The problem will not allow you to get an applet that has not been fully loaded and initialized in GetApplet and GetApplets. Due to the uncertainty of the network and other factors, such as the size of the applet, we cannot determine which applet on the page is first loaded, which one is last loaded. This means we originally plan: first start and control an applet, find other applets, and collaborate with them, if you do not add additional processing, it is not possible to achieve the expected purpose.
Of course, there are many methods to solve the above problems. Control Applet can check its collaboration partner, if they are still not ready, go to wait for sleep (one second or so), then collaborate the status of Applet until all collaborative members have been initialized. Although this method is very efficient, it will lead to a longer start time, how to say it is still effective. A better solution is "Two-Way Search-And Notification Mechanism), when an applet is initialized, control this applet to find other applets and inform them" I am ready. " Using this mechanism, after all auxiliary applets are initialized, the master Applet will be able to immediately find them and start collaboration, if which auxiliary Appplet is later complete, the master applet will be notified in time.
Static variables and methods
In many cases, we can construct an internal Applet communication mechanism by using a static member variable and method using a general class. If multiple applets rely on this general class, they can use this class as a collection point of a message, where they register their existence and see other applets.
Here is an example. If the ColorRelay Applet is used in a page, different instances displays different pictures by collaboration (some different colors of typewriter ribbon). You can imagine that these applets rely on a flag. If an applet shows a colored picture, the other can only realistic black and white pictures. Figure 1.1 is a screenshot of the runtime, inventory 1.1 is an HTML code.
Figure 1.1: ColorRelay Applet runs in the list
1.1. ColorRelay.html.