Communication between applets (1) - Tricks of the Java Programming Gurus

zhaozj2021-02-17  61

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.

Used Applets Sale!

low, low Price!

this Week ONLY!

We Won't be undersold!

Listing 1.2 shows the ColorRelay Appletç program framework, and the specific method is replaced with an annotation. We will provide a complete code in the later list.

List

1.2. ColorRelay.java (Part 1).

/ ** ColorRelay.java 1.0 96/04/14 Glenn Vanderburg * / package com.mcp.samsnet.tjg; import java.applet. *; Import java.awt. *; Import java.awt.image. *; / * ** An applet which coordinates with other instances of itself on a Web * page to alternately flash copies of an image in different colors. ** @version 1.0, 14 Mar 1996 * @author Glenn Vanderburg * / public class ColorRelay extends Applet implements Runnable {// These are used to maintain the list of active instances static ColorRelay list, listTail; ColorRelay next, prev; // This thread switches between instances static Thread relayer; // This is the original, unmodified base image which all // of the instances use static Image originalImage;.. // The color that this instance uses to flash White is the default Color flashColor = Color.white;.. // The modified, colorized image Image modifiedImage; // The image currently being displayed. This reason // Alternates Between OriginalImage and Mo Difiedimage. Image Image; // We use a media tracker to help manage Tracker; // the time we wait while flashing. Two seconds is the default. int SleepSecs = 2;

// Method: static synchronized // addToList (ColorRelay elem) Listing 1.3 // Method: static synchronized // removeFromList (ColorRelay elem) Listing 1.3 // Method: public init () Listing 1.4 // Method: public start () Listing 1.5 //Method: public stop () listing 1.5 // method: public run () listing 1.5 // method: public getappletinfo () on CD // Method: Public GetParameterInfo () on CD // Method: Public Paint (Graphics g) On CD // Method: Public Update (Graphics G) on CD // Method: Flash () on CD // Method: Parsergb (String Str) ON CD} As you can see, there are some ordinary variables in the program: Two IAMGEs, a color, a MediaTracker, a time value, and two static methods are used to insert the ColorRelay object into the linked list. There are four static variables: OriginalImage, when the applet does not have a round to shiny; a thread object, used to coordinate the activity between the applet, and the first node of the Applet Link list.

Not finished, to be continued.

Translation: chenyuan_tongji (chenyuan_tongji@sian.com)

转载请注明原文地址:https://www.9cbs.com/read-30083.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.038, SQL: 9