Third: Java applet

Source: Internet
Author: User

The first thing I want to say is that the applet is an application, it is a small application written by Java, usually such an application 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 and interpreted by a Java-enabled web browser. Of course, we use more than 3.0 of the Internet Explorer version number is to support the Java applet, with such a small application, our web page can have a certain interactive function.

In Java applets, it is possible to achieve graphical drawing, font and color control, animation and sound insertion, human-computer interaction and network communication functions. The applet also provides a form environment development tool called the abstract window Toolkit, AWT. AWT leverages the GUI elements of a user's computer to create a standard graphical user interface, such as a form, button, scroll bar, and so on. For now, there are many examples of applets on the web that vividly illustrate these features, and readers are able to access 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, You will download the corresponding applet code and run the applet on the local computer.

The download of the applet, like a graphics file, takes a certain amount of time, and after a few seconds it is displayed on the screen. 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 executes at the same speed as the program on the local computer.

Applets are also able to download other resources, such as sound files, image files, or many other Java code while they are running on a user's computer, and some applets also agree to user interaction. But this needs to be repeated link and download, so very slow, this is an urgent problem to be solved, can think of a good way is to use similar fast cache technology, each download of files are temporarily saved on the user's hard disk, although the first time to use more time, but when used again, Simply reading files directly from your hard disk without having to connect to the Internet can greatly improve performance.

Ii. applets and application

In the Java language, a program that can be executed independently is called a Java application (application). The Java language also has a second program--application program.

The main structural differences between Javaapplet and Javaapplication are now:

(1) different ways of execution. The Java applet program cannot be executed separately, 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 be executed independently, and only with Java-enabled virtual machines, which can be executed independently without the need for additional file support.

(2) different execution tools. The interpreter that executes 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 has been compiled, the Java applet must be executed by a Web browser or applet viewer, using a common Java interpreter to make its side-interpreted edges executable.

(3) The program structure is different. Each Java application program must contain one and only one main method, when the program executes, it first looks for the main method and starts execution as the 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 execute 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) The Java applet program can directly utilize 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 on the site's disk files can neither read operations, nor write operations. However, because of the introduction of the applet, so that the Web page has dynamic multimedia effects and interactive performance, which makes the HTML language called hypertext, the real plain text of the Web page is really a hypertext feature, not only can display text information, but also can have a variety of picture effects and dynamic graphics effects, This makes the page lively and beautiful; In addition, the applet adds interactivity by adding features such as a button to a Web page.

The main differences between Javaapplet and javaapplication in implementation are now: Java application is typically performed 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. It is then executed on the local machine.

III. Basic Framework

The basic framework of an applet consists of a set of methods that provide the interface between the browser and the applet and the basic mechanism for the control of the two, and the main law is the following:

1. Init ():

Run when an applet is created, and the method is only run once;

This method is run when the applet is first loaded by a Java-enabled browser, and in the life cycle of the applet, the method is only run once, so it can perform some initialization operations that are only run once, such as handling the parameters passed in by the browser, joining the user interface components, Loading images and sound files, etc.; The applet program has a default construction method, but it is accustomed to running all initialization in the init () method, not within the default constructor method;

2. Start ():

This method runs multiple times when the browser is restored from the icon to a form, or when it is returned to the home page;

The system will invoke this method itself after the init () method is called, and the system will run the start () method again whenever the browser resumes from the icon to a form, or when the user leaves the home page that includes the applet and then returns. This method is called multiple times in the life cycle of the applet program to initiate the operation of the applet, which is different from the init () method, which is the main body of the applet, which can run some tasks that need to be run repeatedly or activate a thread again, such as: To start an animation or play a sound, etc.;

3. Stop ():

This method is run several times, when the browser becomes an icon, or when it leaves the homepage, the main function is to stop some of the work of consuming system resources;

In contrast to start (), this method is invoked on its own initiative when the user leaves the page where the applet is located or when the browser becomes an icon, so that the method is called multiple times during the life cycle of the applet, so that it can be used when the user is not aware of the applet program. Stop some of the work that consumes system resources (such as breaking a thread) to avoid affecting the execution speed of the system, and does not require that the method be artificially called; it is assumed that your applet does not include animations, sounds, and so on, and usually does not have to overload the method;

4, Destroy ():

This method is used to release resources and run after stop ();

When the browser shuts down gracefully, Java calls this method itself; This method is used to reclaim any system-independent memory resources, but assuming that the applet is still active, Java will call the Stop () method on its own before invoking the method;

5. Other methods:

Paint (GRAPHICSG): This method is used for detailed interface drawing;

Update (): This method is used for detailed interface refresh;

Repaint (): This method is used for detailed interface redrawing;

Iv. Examples of 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 run through a Web browser. Applets do not require the main () method, which is run 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 execution in the sandbox (the applet's execution environment).

When the applet executes in the sandbox:

⑴ can not execute whatever 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. This method is also called when a user goes from another page to a page that includes an applet.

Stop (): Called when the user leaves a page that includes an 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. Only the display of the applet needs to be refreshed, and the paint () method is called by the browser environment. For example, such calls occur when the display size of the applet changes, or if the browser form is minimized or required to be displayed as an icon.

We should write our own paint () method so that it will work properly whenever it is called. Calls to it are generated asynchronously and are driven by the applet's execution environment rather than by the program.

The paint () method has a parameter, which is an instance of the Java.awt.Graphics class. This parameter always creates a graphical context for the applet's panel, and we can draw or write text in the applet using this graphical context.

5. Applets get information from web pages

Just as an application can obtain information through command-line parameters, an applet can obtain 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 folder to go down and look for the class file.

⑶archive: Optional attribute that lists Java archive files, files that include class files, 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 writing a script, it is sufficient to represent the applet with the name specified for the attribute.

Appendix: AWT Animation, or discussion of the relationship between paint (), update (), repaint ()

The update shows the completion of 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 time, so our program must be able to update the display at random times.

Another scenario is when a program re-paints a screen with new content. Such a 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 limited to the extent of destruction.

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 of update (). The Update method usually clears the current display and calls Paint (). The update () method can be altered, for example, to call paint () directly to reduce flicker without clearing the display.

Third: Java applet

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.