Java Applets are small applications written in the Java language. These programs are directly embedded into the page and are interpreted and executed by a browser that supports Java (IE or Nescape) to produce special effects. It can greatly improve the interactive capabilities and dynamic execution capabilities of Web pages. Web pages that contain Applets are called Java-powered pages and can be called Java-supported web pages.
When a user visits such a web page, the Applet is downloaded to the user's computer for execution, but only if the user is using a Java-enabled web browser. Since the Applet is executed on the user's computer, its execution speed is not limited by network bandwidth or Modem access speed. Users can better appreciate the multimedia effects produced by the Applet on the web page.
The implementation of Applet small application mainly relies on the Applet class in the java.applet package. Unlike general applications, Applet applications must be embedded in HTML pages before they can be interpreted and executed; at the same time, Applets can obtain parameters from Web pages and interact with Web pages.
The HTML file code of a web page containing an Applet must contain a pair of tags such as <applet> and </applet>. When a web browser that supports Java encounters this pair of tags, it will download the corresponding applet code and store it locally. Execute the Applet applet on the computer.
Applet is a small Java program, which is downloaded and run by a web browser that supports Java by using the HTML file of the Applet. It can also be run through the appletviewer of the Java development tool. The Applet program is inseparable from the HTML files that use it. The information about the Applet in this HTML file should contain at least the following three points:
1) The bytecode file name (compiled Java file, with .class as the suffix)
2) The address of the bytecode file
3) Display the Applet on the web page way.
Adding Applet-related content to an HTML file only makes the web page more lively, such as adding sounds, animations and other attractive features. It will not change the elements in the HTML file that are not related to Applets.
(1) Applet program development steps
The main steps of Applet program development are as follows:
1) Use tools such as EDIT or Windows Notepad as editors to create Java Applet source programs.
2) Convert the Applet source program into a bytecode file.
3) Prepare HTML files using classes. Put the necessary <APPLET> statements in the HTML file.
The following is the simplest HelloWorld example to illustrate the development process of the Applet program:
(1) Edit the Applet's java source file
to create a folder C:ghq, and create the source code of the HelloWorld.java file in this folder
as follows:
import java.awt.*;
import java.applet.*;
public class HelloWorld extends Applet //Inherit the Applet class, which is the characteristic of the Applet Java program
{
public void paint(Graphics g)
{
g.drawString("Hello World!",5,35);
}
}
Save the above program in the C:ghqHelloWorld.java file.
(2) To compile the Applet
and compile the HelloWorld.java source file, you can use the following JDK command:
C:ghq>javac HelloWorld.java<Enter>
Note: If the written source program violates the syntax rules of the Java programming language, the Java compiler will display a syntax error message on the screen. The source file must not contain any grammatical errors so that the Java compiler can successfully convert the source program into a bytecode program that can be executed by the appletviewer and browser.
After successfully compiling the Java applet, the response bytecode file HelloWorld.class file is generated. Use the resource manager or DIR command to list the directory. You will find that there is an additional file named HelloWorld.class in the directory C:ghq.
(3) Create an HTML file.
Before running the created HelloWorld.class, you need to create an HTML file. The appletviewer or browser will access the created Applet through this file. In order to run HelloWorld.class, you need to create a file named HelloWorld.html that contains the following HTML statements.
<HTML>
<TITLE>HelloWorld! Applet</TITLE>
<APPLET
CODE="JavaWorld.class"
WIDTH=200
HEIGHT=100>
</APPLET>
</HTML>
In this example, the <APPLET> statement specifies the Applet bytecode class file name and the size of the window in pixels. Although the file name used for the HTML file here is HelloWorld.HTML, which corresponds to the name of HelloWorld.java, this correspondence is not necessary. The HTML file can be named with any other name (such as Ghq.HTML). However, maintaining a corresponding relationship between file names can bring convenience to file management.
(4) Execute HelloWorld.html
If you use appletviewer to run HelloWorld.html, you need to enter the following command line:
C:ghq>appletviewer JavaWorld.html<ENTER>
As can be seen, this command starts the appletviewer and specifies the HTML file, which contains the <APPLET> statement corresponding to HelloWorld.
If you use a browser to run HelloWorld Applet, you need to enter the HTML file URL address in the browser's address bar.
At this point, the entire process of developing and running an Applet program is over (including java source files, compiled class files, html files, and running with appletviewer or a browser).
(2) Applet class
The Applet class is the base class for all Applet applications, and all Java applets must inherit this class. As shown below.
import java.applet.*;
public class OurApplet extends Applet
{
...
...
}
There is only one constructor of the Applet class, namely: public Applet().
Applet implements many basic methods. The following lists the common methods and uses in the Applet class.
public final void setStub(AppletStub stub)
//Set the Applet's stub.stub is the code bit that converts parameters between Java and C and returns a value. It is automatically set by the system.
public boolean isActive(); // Determine whether an Applet is active.
public URL getDocumentBase(); // Retrieve the object representing the file directory where the Applet runs.
public URL getCodeBase(); // Get the URL address of the Applet code.
public String getParameter(String name); // Get the value of the parameter specified by name of the Applet.
public AppletContext getAppletContext(); // Returns the browser or applet observer.
public void resize(int width,int height); //Adjust the window size of the Applet running.
public void resize(Dimension d); //Adjust the size of the window where the Applet runs.
public void showStatus(String msg); // Display the specified information in the browser's status bar.
public Image getImage(URL url); // Load the image according to the address specified by url.
public Image getImage(URL url,String name); // Load the image according to the address and file name specified by url.
public AudioClip getAudioClip(URL url); // Get the sound file according to the address specified by url.
public AudioClip getAudioClip(URL url, String name); // Get the sound according to the address and file name specified by url.
public String getAppletInfo(); // Returns the author, version and copyright information about the Applet application;
public String[][] getParameterInfo();
// Returns a string array describing the Applet parameters. The array usually contains three strings: the parameter name, the type of the value required by the parameter, and the description of the parameter.
public void play(URL url); // Load and play an audio clip specified by url.
public void destroy(); //Undo the Applet and the resources it occupies. If the Applet is active, terminate the running of the Applet first.
(1) Basic methods for controlling Applet running status.
The four basic methods in the Applet class are used to control its running status: init(), start(), stop(), destroy().
Theinit() method
is mainly for Applet
.Perform some initialization work during normal operation. When an Applet is called by the system, the system first calls this method. Usually, operations such as passing parameters from the web page to the Applet and adding basic components of the user interface can be completed in this method.
Thestart() method
system will automatically call the start() method after calling the init() method. Moreover, every time the user leaves the homepage containing the Applet and then returns, the system will execute the start() method again. This means that the start() method can be executed multiple times, unlike the init() method. Therefore, you can put code that you only want to execute once in the init() method. You can start a thread in the start() method, such as continuing an animation, sound, etc.
The stop() method
is executed when the user leaves the page where the Applet is located, so it can also be executed multiple times. It allows you to stop some work that consumes system resources when the user is not paying attention to the Applet, so as not to affect the running speed of the system, and there is no need to manually call this method. If the Applet does not contain animation, sound and other programs, it is usually not necessary to implement this method.
The destroy() method
is different from the object's finalize() method. Java only calls this method when the browser is closed. The Applet is embedded in the HTML file, so the destruction() method does not care when the Applet is closed, it is automatically executed when the browser is closed. In the destroy() method, you can generally request to recover the occupied non-memory independent resources. (If the browser is closed while the Applet is still running, the system will first execute the stop() method, and then execute the destroy() method.
(2) Relevant parameter descriptions of the Applet application
Use Applet to receive parameters passed from HTML, The following is a brief explanation of these parameters:
* CODE flag
The CODE flag specifies the Applet class name; the WIDTH and HEIGHT flags specify the pixel size of the Applet window. Some other flags can also be used in the APPLET statement.
* CODEBASE flag
The CODEBASE flag specifies the Applet. URL address. Applet's universal resource location URL, which can be an absolute address, such as www.sun.com . It can also be a relative address relative to the directory where the current HTML is located, such as /AppletPath/Name if the HTML file does not specify CODEBASE. flag, the browser will use the same URL as the HTML file.
*ALT flag
Although Java is very popular on the WWW, not all browsers support it. If a browser cannot run Java Applets, it will The text information specified by the ALT flag will be displayed when using the APPLET statement.
* ALIGN flag
The ALIGN flag can be used to control where the Applet window is displayed in the HTML document window. Like the HTML<LMG> statement, the value specified by the ALIGN flag can be TOP or MIDDLE. Or BOTTOM.
* VSPACE and HSPACE flags
The VSPACE and HSPACE flags specify the size of the horizontal and vertical blank bars that the browser displays around the Applet window, in pixels. Use this flag to leave space above and below the Applet window. 50 pixels of space, leaving 25 pixels of space on the left and right:
* NAME flag
The NAME flag gives the specified name to the current instance of the Applet. When the browser is running two or more Applets at the same time, each Applet can. Reference or exchange information with each other by name. If the NAME flag is ignored, the Applet's name will correspond to its class name.
* PARAM flag
universality is one of the goals pursued by users or programmers. Applet's completion of different tasks is a concrete manifestation of versatility. Obtaining information from HTML files is an effective way to improve the versatility of Applet.
Suppose you program an Applet that scrolls the name of a company on the screen in order to make the Applet more versatile. , the Applet can obtain the text information that needs to be scrolled from the HTML file. In this way, if you want to display the name of another company, you do not need to modify the Java Applet itself, you only need to modify the HTML file.
The PARAM flag can be used in the HTML file. Specify parameters in the format as follows:
PARAM Name="name" Value="Liter"
Java Applet can call the getParameter method to obtain the parameter value set in the HTML file.