Java Applet Program Design Basics

Source: Internet
Author: User

Java Applet is a number of small applications written in Java language. These programs are directly embedded into the page, and are supported by Java browsers (IE or Nescape) explain the execution of programs that can produce special results. It can greatly improve the web page interaction and dynamic execution capabilities. A webpage containing an Applet is called a Java-powered page, which can be called a webpage supported by Java.

When a user accesses such a webpage, the Applet is downloaded to the user's computer for execution, provided that the user is using a Web browser that supports Java. Since the Applet is executed on the user's computer, its execution speed is not limited by the network bandwidth or Modem access speed. Users can better enjoy the Multimedia effect of the Applet on the web page.

The implementation of Applet small applications mainly relies on the applet Class in the java. Applet package. Unlike general applications, Applet applications must be embedded in the HTML page for interpretation and execution. Meanwhile, the Applet can obtain parameters from the Web page and interact with the Web page.

The HTML file code of a webpage containing an Applet must contain a pair of tags such as <applet> and </applet>. When a web browser supporting Java encounters this pair of tags, the corresponding Applet code will be downloaded and the Applet will be executed on the local computer.

An Applet is a Java Applet that is downloaded and run by a Web browser that supports Java by using the HTML file of the Applet. You can also run it through the appletviewer of the java development tool. An Applet cannot be separated from its HTML files. The Applet information in this HTML file should include at least three points:

1) bytecode file name (the compiled Java file is suffixed with. class)

2) Address of the bytecode File

3) display the Applet on the webpage.

Adding Applet-related content to an HTML file only makes webpages more angry, such as adding sound, animation, and other attractive features. It does not change the elements irrelevant to the Applet in the HTML file.


(1) Applet development steps

The main steps for Applet development are as follows:

1) select EDIT or Windows Notepad as the editor to create a Java Applet source program.

2) convert the Applet source program to a bytecode file.

3) Compile an HTML file using class. Add the necessary <APPLET> statement to the HTML file.

The following is a simple HelloWorld example to illustrate the development process of the Applet:

(1) edit the java source file of the Applet

Create the C: ghq folder and create HelloWorld. java

The source code of the file is as follows:

Import java. awt .*;
Import java. applet .*;
Public class HelloWorld extends Applet // inherits the Appelet class, which is a feature of the Appelet Java program
{
Public void paint (Graphics g)
{
G. drawString ("Hello World! ", 5, 35 );
}
}

Save the preceding program in the C: ghqHelloWorld. java file.

(2) Compile the Applet

To compile the HelloWorld. java source file, run the following JDK command:

C:ghq>javac HelloWorld.java<Enter>
Note: If the source program violates the syntax rules of the Java programming language, the Java compiler displays a syntax error message on the screen. The source file must not contain any syntax errors. the Java compiler can successfully convert the source program to the bytecode program that can be executed by appletviewer and the browser.

After the Java applet is compiled successfully, the HelloWorld. class file of the response bytecode file is generated. Use the resource manager or DIR command to list the directories. A file named HelloWorld. class is added to the directory C: ghq.

(3) create an HTML file

Before running the created HelloWorld. class, you must create an HTML file. The appletviewer or browser accesses the created Applet through the file. To run HelloWorld. class, create a file named helloworld.html containing the following HTML sentence.

<HTML>
<TITLE>HelloWorld! Applet</TITLE>
<APPLET
CODE="JavaWorld.class"
WIDTH=200
HEIGHT=100>
</APPLET>
</HTML>

In this example, the <APPLET> statement specifies the size of the file name of the Applet bytecode class and the window in pixels. Although the HTML file is named HelloWorld. HTML, which corresponds to HelloWorld. java name, but this correspondence is not required. You can use any other name (such as Ghq. HTML) Name the HTML file. However, keeping the file name in a corresponding relationship can facilitate file management.

(4) execute HelloWorld.html

If you use appletviewerto run helloworld.html, enter the following command line:

C:ghq>appletviewer JavaWorld.html<ENTER>
As you can see, this command starts appletviewer and specifies the HTML file, which contains the <APPLET> statement corresponding to HelloWorld.

To run the HelloWorld Applet in a browser, enter the URL of the HTML file in the address bar of the browser.

So far, the entire process of developing and running an Applet 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 of all Applet applications. All Java small applications must inherit this class. As shown below.

import java. applet.*;
public class OurApplet extends Applet
{
......
......
}

The Applet Class has only one constructor, namely: public Applet ()

The Applet implements many basic methods. The common methods and usage in the Applet Class are listed below.

Public final void setStub (AppletStub stub)
// Set the Applet's stub. stub is the code bit that converts parameters and returns values between Java and C. It is automatically set by the system.
Public boolean isActive (); // determines whether an Applet is active.
Public URL getDocumentBase (); // retrieve the objects in the directory where the Applet runs.
Public URL getCodeBase (); // obtain the URL of the Applet code.
Public String getParameter (String name); // gets the value of the parameter specified by name for this Applet.
Public AppletContext getAppletContext (); // return to the browser or small application observer.
Public void resize (int width, int height); // adjust the window size of the Applet.
Public void resize (Dimension d); // adjust the window size of the Applet running.
Public void showStatus (String msg); // display the specified information in the browser status bar.
Public Image getImage (URL url); // load the Image at the address specified by the url.
Public Image getImage (URL url, String name); // load the Image Based on the address and file name specified by the url.
Public AudioClip getAudioClip (URL url); // obtain the audio file at the address specified by the url.
Public AudioClip getAudioClip (URL url, String name); // obtain the sound by url and file name.
Public String getAppletInfo (); // return the author, version, and copyright information of the Applet application;
Public String [] [] getParameterInfo ();
// Return a string array that describes the Applet parameter. This array usually contains three strings: parameter name, type of value required for this parameter, and description of this parameter.
Public void play (URL url); // load and play an audio clip specified by a url.
Public void destroy (); // undo the Applet and its resources. If the Applet is active, it is terminated first.

(1) Basic Method of Applet running status control

The four basic methods in the Applet Class are used to control the running status: init (), start (), stop (), destroy ()

Init () method

This method initializes the normal operation of the Applet. When an Applet is called by the system, the system first calls this method. In this method, parameters can be transferred from a webpage to the Applet, and basic components of the user interface can be added.

Start () method

After the init () method is called, the start () method is automatically called. The start () method is executed again every time the user leaves the home page containing the Applet and returns again. This means that the start () method can be executed multiple times, unlike the init () method. Therefore, you can put the code that only needs to be executed once in the init () method. You can start a thread in the start () method, such as continuing an animation or sound.

Stop () method

This method is executed when the user leaves the page where the Applet is located. Therefore, it can also be executed multiple times. It allows you to stop consuming system resources when you do not pay attention to the Applet, so as not to affect the system running speed, and you do not need to manually call this method. This method is usually not required if the Applet does not contain programs such as animation and sound.

Destroy () method

Unlike the finalize () method of an object, Java calls this method only when the browser is closed. The Applet is embedded in an HTML file, so the destroty () method does not care when the Applet is closed, and is automatically executed when the browser is closed. In the destroy () method, the occupied Non-memory independent resources can be reclaimed. (If the browser is closed when the Applet is still running, the system will first execute the stop () method and then the destroy () method.

(2) Description of parameters of the Applet Application

The Applet is used to receive parameters passed from HTML. The following describes these parameters:

* CODE flag

The CODE flag specifies the Applet Class Name; the WIDTH and HEIGHT flag specifies the pixel size of the Applet window. Other flags can be used in APPLET statements.

* CODEBASE flag

The CODEBASE flag specifies the URL of the Applet. The universal resource location URL of the Applet. It can be an absolute address, such as www.sun.com. It can also be the relative address relative to the current HTML directory, such as/AppletPath/Name. If the CODEBASE flag is not specified for an HTML file, the browser uses the same URL as the HTML file.

* ALT flag

Although Java is very popular on WWW, not all browsers support it. If a browser cannot run a Java Applet, it displays the text specified by the ALT flag when encountering an APPLET statement.

* ALIGN flag

The ALIGN flag can be used to control the position of the Applet window in the HTML document window. Like the HTML <LMG> statement, the ALIGN flag specifies TOP, MIDDLE, or BOTTOM.

* VSPACE and HSPACE flag

The VSPACE and heat map flag indicate that the browser is displayed in the Applet window.

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.