Java Applet Basics __java

Source: Internet
Author: User
Tags numeric

An applet is a Java program. It is typically run in a Java-enabled web browser. Because it has full Java API support, the applet is a full-featured Java application.

This is an important difference between a stand-alone Java application and an applet program, as follows: The Java applet class inherits the Java.applet.Applet class applet class does not define main (), so an applet program does not invoke the main () method. Applets is designed to be embedded in an HTML page. When the user browses the HTML page containing the applet, the applet's code is downloaded to the user's machine. To see if an applet requires a JVM. The JVM can be a plug-in to a Web browser, or a stand-alone run-time environment. The JVM on the user's machine creates an instance of the Applet class and invokes various methods in the applet lifecycle process. Applets has strict security rules enforced by web browsers, and the applet's security is called sandbox security. Other classes required by the applet can be downloaded in the form of a Java archive (JAR) file. the life cycle of applets

The four methods in the Applet class give you a framework in which you can develop applets: init: The purpose of this method is to provide any initialization you need for your applets. This method is called after the Param label inside the applet tag is processed. Start: After the browser invokes the Init method, the method is invoked automatically. This method is called whenever the user returns from another page to the page that contains the applet. Stop: This method is automatically invoked when a user removes a page that contains an applet. Therefore, you can call the method repeatedly in the same applet. Destroy: This method is invoked only when the browser shuts down normally. Because applets only works on HTML pages, you should not omit any resources after the user leaves the page containing the applet. Paint: This method is invoked immediately after the start () method, or when the applet needs to be redrawn in the browser. The paint () method actually inherits from java.awt. "Hello, World" Applet:

The following is a simple applet program Helloworldapplet.java:

Import java.applet.*;
Import java.awt.*;
 
public class Helloworldapplet extends applets
{public
   void paint (Graphics g)
   {
      g.drawstring ("Hello World ",);
   }

The Import statements import the following classes into our applet class:

Java.applet.Applet.
Java.awt.Graphics.

Without these import statements, the Java compiler will not recognize applets and graphics classes. Applet class

Each applet is a subclass of the Java.applet.Applet class, and the underlying applet class provides a method for invoking the derived class to obtain information and services for the browser context.

These methods do the following: Get the parameters of the applet to get the network location of the applet's HTML file to get the status information of the Web location print browser of the Applet class directory get a picture get an audio fragment play an audio fragment adjust the size of this applet

In addition, the applet class provides an interface for the viewer or browser to obtain information about the applet and to control the execution of the applet.

The viewer may be: request an applet author, version, and copyright information request a description of the parameters identified by the applet Initialize applet destroy applet start applet end Execution Applet

The Applet class provides a default implementation of these methods that can be overridden when needed.

"Hello,world" applets are written according to standards. The only method that is rewritten is the paint method. calls to applets

An applet is a Java program. It is typically run in a Java-enabled web browser. Because it has full Java API support, the applet is a full-featured Java application.

<applet> tags are the basis for embedding applets in HTML files. Here is an example of invoking the "Hello World" applet;

 

Note: You can refer to the HTML applet tag to learn more about how to invoke applets from HTML.

<applet> the properties of the label specify the applet class to run. Width and height are used to specify the initial size of the applet run panel. The applet must be closed using the </applet> tab.

If the applet accepts parameters, the value of the parameter needs to be added to the label, which is between <applet> and </applet>. The browser ignores the text and other labels between the applet tabs.

Browsers that do not support Java cannot perform <applet> and </applet>. As a result, anything that is displayed between tags and has nothing to do with the applet is visible in browsers that are not supported in Java.

Viewer or browser in the location of the document to look for compiled Java code, to specify the path of the document, you have to use the <applet> tag codebase property specified.

As shown below:

<applet codebase= "Http://amrood.com/applets" code= "Helloworldapplet.class" width= "the" height= "
>"

If the applet is in a package instead of the default package, the package in which it resides must be specified in the code attribute, for example:

<applet code= "Mypackage.subpackage.TestApplet.class"
           width= "height=" >
Get applet parameters

The following example shows how to use an applet response to set the parameters specified in the file. The applet displays a black checkerboard pattern and a second color.

The second color and the size of each column are specified by the parameters of the applet in the document.

Checkerapplet gets its arguments in the init () method. You can also get its arguments in the paint () method. However, it is convenient and efficient for the applet to start to get the value and save the settings instead of getting a value every time it refreshes.

The applet viewer or browser invokes the Init () method each time the applet runs. After the Applet is loaded, the viewer immediately calls the Init () method (Applet.init () does nothing), overrides the default implementation of the method, and adds some custom initialization code.

The Applet.getparameter () method gets the parameter value by giving the parameter name. If the resulting value is numeric or other non-numeric data, it must be resolved to a string type.

The following example is a synopsis of Checkerapplet.java:

Import java.applet.*;
Import java.awt.*;
public class Checkerapplet extends Applet
{
   int squaresize = 50;//initialization default size public
   void init () {}
   
Related Article

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.