The first thing I want to say is that the applet is an application, it is a small application written by Java, usually this kind of application is like his name, is a very small program, perhaps some friends will ask, then what is it for? Java program is Java program Ah, why do you want to make a small application?
First, the initial knowledge of the applet
Applet programming is a unique feature of the Java programming language, which differs from the typical Java program in that it can be embedded in an HTML Web page and dynamically downloaded by a Java-enabled web browser to interpret execution, Of course, we use more than 3.0 of the Internet Explorer version of the Java applet support, with this small application, our web page can have a certain interactive function.
In the Java applet, it can realize the functions of drawing, font and color control, animation and sound inserting, human-computer interaction and network communication. The applet also provides a window environment development tool called the Abstract Window Toolbox (Toolkit, AWT). AWT uses the GUI elements of the user's computer to create a standard graphical user interface, such as Windows, buttons, scroll bars, and so on. At present, there are many examples of applets on the network to vividly show these functions, readers can go to the corresponding pages to see their effects.
How applets work
The HTML file code of the Web page containing the applet has a pair of tags such as <applet> and </applet>, when a Java-enabled web browser encounters this pair of tags, The appropriate applet code is downloaded and executed on the local computer.
The download of the applet takes a certain amount of time as a graphics file, and it can be displayed on the screen in a few seconds. The amount of time to wait depends on the size of the applet and the speed of the user's network connection. Once downloaded, it runs at the same speed as the program on the local computer.
Applets can also download other resources such as sound files, image files, or more Java code while they are executing on the user's computer, and some applets allow the user to interactively manipulate them. But this needs to repeat the link and the download, therefore the speed is very slow, this is an urgent problem, can think of a good way is to use a similar cache technology, each download of the files are temporarily saved on the user's hard disk, although the first time spent more time, but when used again, You can significantly improve performance by simply reading files directly from your hard disk without having to connect to the Internet.
Ii. applets and application
In the Java language, a program that can run independently is called a Java application (application). The Java language also has another program--application program.
The main structural differences between Javaapplet and Javaapplication are shown in:
(1) different modes of operation. The Java applet program cannot run alone, it must be attached to a Web page written in HTML and embedded, and controlled by a Java-compatible browser. Java application is a complete program that can run on its own, as long as there is a Java-enabled virtual machine that can run independently without the need for additional file support.
(2) different running tools. The interpreter running the Java applet is not a standalone software, but is embedded in the browser as part of the browser software. After the Java application program is compiled, the Java applet can be executed by using a common Java interpreter to interpret the edges, which must be performed by a Web browser or applet viewer.
(3) The program structure is different. Each Java application program must contain one and only one main method, and when the program executes, it first looks for the main method and starts it as an entry point. The class that contains the main method, often referred to as the main class, means that the Java application program contains a main class. The applet program does not contain the main class of the Main method, which is why the applet program cannot run independently. Although the applet does not have a main class with the Main method, the applet must have a class derived from Java.applet.Applet, which is provided by the Java system.
(4) Java applet programs can directly take advantage of the graphical user interface provided by the browser or Appletviewer, while the Java application program must also write special code to build its own graphical interface.
(5) Restrictions different Java application programs can be designed to perform a variety of operations, including read/write file operations, but the Java Applet to the site's disk files can neither read operation, nor write operations. However, due to the introduction of the applet, so that the Web page has dynamic multimedia effects and interactive performance, which makes the HTML language called hypertext, Real Plain text into a Web page really has a hypertext function, not only can display text information, but also can have a variety of picture effects and dynamic graphics effects, This makes the page appear vivid and beautiful; In addition, the applet adds functionality such as buttons to the Web page, which increases interactivity.
The main difference between Javaapplet and javaapplication in implementation is that Java application is typically run on a local machine, and Java applets are generally placed on the server, which is downloaded to the local machine based on the local machine's request. Then run on the local machine.
III. Basic Framework
The basic framework of applets is composed of a set of methods that provide the interface between the browser and the applet and the basic mechanism of the execution control of the two parties, the main law is as follows:
1. Init ():
Executes when an applet is created, and the method is executed only once;
This method is executed when the applet is first loaded by a Java-enabled browser, and the method is executed only once during the applet's lifetime, so it can perform some initialization operations, such as handling parameters passed in by the browser, adding user interface components, Loading images and sound files, etc.; The applet program has a default construction method, but it is accustomed to performing all initialization in the init () method, not within the default constructor method;
2. Start ():
The method executes multiple times when the browser is restored from the icon to a window, or when it is returned to the home page;
The system calls this method automatically after the Init () method is called, and the system executes the start () method again whenever the browser resumes from the icon to a window, or when the user leaves the home page containing the applet and then returns. This method is called multiple times in the life cycle of the applet program to initiate the execution of the applet, which is different from the init () method, which is the body of the applet, where you can perform some tasks that need to be performed repeatedly or reactivate a thread, for example: Start an animation or play a sound, etc.;
3. Stop ():
This method is executed many times, when the browser becomes an icon, or when it leaves the homepage, the main function is to stop some work that consumes system resources;
In contrast to start (), this method is called automatically when the user leaves the page where the applet is located or when the browser becomes an icon, so the method is called multiple times during the life cycle of the applet, so that the user does not pay attention to the applet program. Stop some of the work that consumes system resources (such as breaking a thread) to avoid affecting the speed of the system, and does not require the method to be artificially called; if your applet does not contain animations, sounds and other programs, you usually do not have to overload the method;
4, Destroy ():
This method is used to release resources and execute after stop ();
Java automatically calls this method when the browser shuts down normally, and the method is used to reclaim any system-independent memory resources, but if the applet is still active, then Java will call the Stop () method automatically before calling the method;
5. Other methods:
Paint (GRAPHICSG): This method is used for concrete interface drawing;
Update (): This method is used for specific interface refresh;
Repaint (): This method is used for concrete interface redrawing;
Iv. Sample Procedures
1. Applet Code:
<span style= "FONT-SIZE:18PX;" >importjava.io.*;importjava.awt.*;importjava.lang.*;importjava.applet.*;p ublicclass MyApplet extends Applet{ private String strUserName; Private String strpassword; private int iCount; private int iposx; public void init () { strusername =this.getparameter ("username"); strpassword =this.getparameter ("password"); ICount = Integer.parseint (This.getparameter ("Count")); Iposx = +; } public void Paint (Graphics g) { G.drawrect (ten, ten, Iwidth, Iheigh); g.DrawString ("UserName:" +strusername + "Count:" + ICount, IPOSX, $); g.DrawString ("Password:" +strpassword, Iposx,);} } </span>
2. HTML code:
<span style= "FONT-SIZE:18PX;" >
V. Issues related to applets
1. What is an applet?
Applets, also known as Java applets, are Java classes that can be embedded in an HTML page and can be downloaded and executed through a Web browser. The applet does not require the main () method, which is executed by a Java virtual machine call embedded in the Web browser.
2. Security restrictions for applets
Because applets are downloaded from the remote server and executed locally, security is especially important. The applet is secured to the local system by restricting the applet to run in the sandbox (the applet's running environment).
When the applet is running in the sandbox:
⑴ cannot run any local executable program;
⑵ except for servers that store downloaded applets, applets cannot communicate with other hosts.
⑶ cannot read and write to the local file system.
3. The life cycle of the applet
Init (): This method is called when the browser loads the applet and initializes it.
Start (): Called After the init () method. The method is also called when the user goes from another page to the page that contains the applet.
Stop (): Called when the user leaves the page containing the applet.
Destroy (): The method is called when the applet is no longer being used, or when the browser exits.
4. Paint () method This paint still comes from the AWT
Applets are essentially graphical, and we should draw our display in a graphical environment.
We can draw on the applet's panel by creating a Paint () method. As long as the applet's display needs to be refreshed, the paint () method is called by the browser environment. For example, this call occurs when the display size of an applet changes, or if the browser window is minimized or required to be displayed as an icon.
We should write our own paint () method so that it can be called at any time and work normally. The call to it is generated asynchronously and is driven by the applet's running environment, not the program.
The paint () method has a parameter, which is an instance of the Java.awt.Graphics class. This parameter always establishes the graphical context of the applet's panel, which we can use to draw or write text in the applet.
5. Applets get information from web pages
Just as an application can obtain information through command-line arguments, an applet can get information from a Web page by using the Param tag.
Displaying information in the browser: calling the Showstatus () method in the Appletcontext interface
Requests the browser to display the specified Web page: Call the showdocument () method in the Appletcontext interface.
6. HTML tags and attributes for applets
n the Applet property used for positioning
⑴width and Height: the necessary attributes, in pixels, to set the width and height of the applet.
⑵align: Optional attribute that specifies how the applet is aligned.
Left: The applet is placed on the right side of the page, and the text behind it moves to the applet.
Right: Place the applet on the left side of the page, and the text will be moved to the other side of the applet.
Bottom: Aligns the bottom of the applet with the bottom of the current line text.
Top: Aligns the top of the applet with the top of the current line.
Texttop: Aligns the top of the applet with the top of the current line text.
Middle: Aligns the middle of the applet with the current line baseline.
Absmiddle: Aligns the middle of the applet with the middle of the current row.
Baseline: Aligns the bottom of the applet with the current line baseline.
Absbottom: Aligns the bottom of the applet with the bottom of the current line.
⑶vspace and hspace: Optional properties that specify the number of pixels (vspace) on the applet or the number of pixels (hspace) on both sides of the applet.
Applet Properties for encoding
⑴code: Specifies the name of the applet class file. The name is either relative to codebase, then relative to the current page.
⑵codebase: Optional attribute that tells the browser which directory to go down to to find the class file.
⑶archive: Optional attribute that lists the Java archive file, the file containing the class file, or other resources required by the applet.
(4) Object: Another method used to specify the applet class file.
⑸name: Optional Properties, page scripting people want to give the applet a name attribute, so that when scripting, you can use the name specified for the attribute to represent the applet.
Appendix: AWT Animation, or discussion of the relationship between paint (), update (), repaint ()
The update display is done by a separate thread called the AWT thread. This thread can be used to handle two situations related to displaying updates.
The first case is exposure (exposure), which appears when it is first displayed, or when a partial display has been corrupted and must be refreshed. The damage shown may occur at any moment, so our program must be able to update the display at any time.
The second scenario is when the program re-paints the screen with the new content. This redraw may require that the original image be erased first.
Paint (GRAPHICSG) method
Called when the component is first displayed, or if the damaged part needs to be repaired. Unless necessary, the update does not completely cover the entire graphics area, but is strictly restricted to the extent of the damage.
Repaint () method
A call to repaint () notifies the system that you want to change the display, so the system will call paint ().
Update (GRAPHICSG) method
Repaint () actually produces an AWT thread that calls another method, update (). The Update method usually clears the current display and calls Paint (). The update () method can be modified, for example, by calling paint () directly to reduce flicker without clearing the display.
&NBSP;