Applet 2
Applet 2
Applet is a special type of program that is embedded in the webpage to generate the
dynamic content. It runs inside the browser and works at client side.
Advantage of Applet
There are many advantages of applet. They are as follows:
Drawback of Applet
o Plugin is required at client browser to execute applet.
Hierarchy of Applet
An applet is a Java program that runs in a Web browser. An applet can be a fully
functional Java application because it has the entire Java API at its disposal.
There are some important differences between an applet and a standalone Java
application, including the following −
• An applet is a Java class that extends the java.applet.Applet class.
• A main() method is not invoked on an applet, and an applet class will not define main().
• Applets are designed to be embedded within an HTML page.
• When a user views an HTML page that contains an applet, the code for the applet is
downloaded to the user's machine.
• A JVM is required to view an applet. The JVM can be either a plug-in of the Web browser
or a separate runtime environment.
• The JVM on the user's machine creates an instance of the applet class and invokes
various methods during the applet's lifetime.
• Applets have strict security rules that are enforced by the Web browser. The security of
an applet is often referred to as sandbox security, comparing the applet to a child playing
in a sandbox with various rules that must be followed.
• Other classes that the applet needs can be downloaded in a single Java Archive (JAR)
file.
o init(): The init() method is the first method to run that initializes the applet. It can be invoked
only once at the time of initialization. The web browser creates the initialized objects, i.e.,
the web browser (after checking the security settings) runs the init() method within the
applet.
o start(): The start() method contains the actual code of the applet and starts the applet. It is
invoked immediately after the init() method is invoked. Every time the browser is loaded or
refreshed, the start() method is invoked. It is also invoked whenever the applet is
maximized, restored, or moving from one tab to another in the browser. It is in an inactive
state until the init() method is invoked.
o stop(): The stop() method stops the execution of the applet. The stop () method is invoked
whenever the applet is stopped, minimized, or moving from one tab to another in the
browser, the stop() method is invoked. When we go back to that page, the start() method is
invoked again.
o destroy(): The destroy() method destroys the applet after its work is done. It is invoked
when the applet window is closed or when the tab containing the webpage is closed. It
removes the applet object from memory and is executed only once. We cannot start the
applet once it is destroyed.
o paint(): The paint() method belongs to the Graphics class in Java. It is used to draw shapes
like circle, square, trapezium, etc., in the applet. It is executed after the start() method and
when the browser or applet windows are resized.
// initialized objects
• java.applet.Applet
• java.awt.Graphics
Without those import statements, the Java compiler would not recognize the classes
Applet and Graphics, which the applet class refers to.
How to run an Applet?
There are two ways to run an applet
1. By html file.
2. By appletViewer tool (for testing purpose).
AppletViewer
c:\>javac HelloWorld.java
c:\>appletviewer HelloWorld.java
HTML Webpage
<html>
<body>
</applet>
</body>
</html>
• Request information about the author, version, and copyright of the applet
• Request a description of the parameters the applet recognizes
• Initialize the applet
• Destroy the applet
• Start the applet's execution
• Stop the applet's execution
The Applet class provides default implementations of each of these methods. Those
implementations may be overridden as necessary.
The "Hello, World" applet is complete as it stands. The only method overridden is the
paint method.
Invoking an Applet
setBackground (Color.black);
setForeground (fg);
}