Java Applet
Java 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.
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.
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
An applet may be invoked by embedding directives in an HTML file and viewing the
file through an applet viewer or Java-enabled browser.
The <applet> tag is the basis for embedding an applet in an HTML file. Following is
an example that invokes the "Hello, World" applet −
<html>
<title>The Hello, World Applet</title>
<hr>
<applet code = "HelloWorldApplet.class" width = "320" height = "120">
If your browser was Java-enabled, a "Hello, World"
message would appear here.
</applet>
<hr>
</html>
Note − You can refer to HTML Applet Tag to understand more about calling applet
from HTML.
The code attribute of the <applet> tag is required. It specifies the Applet class to run.
Width and height are also required to specify the initial size of the panel in which an
applet runs. The applet directive must be closed with an </applet> tag.
If an applet takes parameters, values may be passed for the parameters by adding
<param> tags between <applet> and </applet>. The browser ignores text and other
tags between the applet tags.
Non-Java-enabled browsers do not process <applet> and </applet>. Therefore,
anything that appears between the tags, not related to the applet, is visible in non-
Java-enabled browsers.
The viewer or browser looks for the compiled Java code at the location of the
document. To specify otherwise, use the codebase attribute of the <applet> tag as
shown −
<applet codebase = "https://amrood.com/applets" code = "HelloWorldApplet.class"
width = "320" height = "120">
If an applet resides in a package other than the default, the holding package must be
specified in the code attribute using the period character (.) to separate
package/class components. For example −
<applet = "mypackage.subpackage.TestApplet.class"
width = "320" height = "120">
setBackground (Color.black);
setForeground (fg);
}
Event Handling
Applets inherit a group of event-handling methods from the Container class. The
Container class defines several methods, such as processKeyEvent and
processMouseEvent, for handling particular types of events, and then one catch-all
method called processEvent.
In order to react to an event, an applet must override the appropriate event-specific
method.
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.applet.Applet;
import java.awt.Graphics;
Displaying Images
An applet can display images of the format GIF, JPEG, BMP, and others. To display
an image within the applet, you use the drawImage() method found in the
java.awt.Graphics class.
Following is an example illustrating all the steps to show images −
import java.applet.*;
import java.awt.*;
import java.net.*;
Playing Audio
An applet can play an audio file represented by the AudioClip interface in the
java.applet package. The AudioClip interface has three methods, including −
public void play() − Plays the audio clip one time, from the beginning.
public void loop() − Causes the audio clip to replay continually.
public void stop() − Stops playing the audio clip.
To obtain an AudioClip object, you must invoke the getAudioClip() method of the
Applet class. The getAudioClip() method returns immediately, whether or not the
URL resolves to an actual audio file. The audio file is not downloaded until an
attempt is made to play the audio clip.
Following is an example illustrating all the steps to play an audio −
import java.applet.*;
import java.awt.*;
import java.net.*;
public class AudioDemo extends Applet {
private AudioClip clip;
private AppletContext context;
is a special type of program embedded in the web page to generate dynamic content. Applet is a
class in Java.
The applet life cycle can be defined as the process of how the object is created,
started, stopped, and destroyed during the entire execution of its application. It
basically has five core methods namely init(), start(), stop(), paint() and
destroy().These methods are invoked by the browser to execute.
Along with the browser, the applet also works on the client side, thus having less
processing time.
There are five methods of an applet life cycle, and they are:
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.
1. init()
2. start()
3. paint()
1. stop()
2. destroy()
1. class TestAppletLifeCycle extends Applet {
2. public void init() {
3. // initialized objects
4. }
5. public void start() {
6. // code to start the applet
7. }
8. public void paint(Graphics graphics) {
9. // draw the shapes
10. }
11. public void stop() {
12. // code to stop the applet
13. }
14. public void destroy() {
15. // code to destroy the applet
16. }
17. }