Java Applet
Java Applet
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:
1. Local Applet
It is developed locally and stored locally too. In the case of the local applet, the website e
doesn’t get the information from the internet. The information is specified file name and file
path in local. There are two fields used in defining an applet. The code that defines the file
name that contains the applet code and the codebase which specifies the path name.
2. Remote Applet
A remote applet is developed by another developer and stored on a remote computer that is
connected to the internet. For running the applet stored in the remote computer, our system
is connected to the internet then we can download and run it. It is necessary to know the
applet URL on the web so as to locate and run a remote applet.
Applet class
Applet class provides all necessary support for applet execution, such as initializing and
destroying of applet. It also provide methods that load and display images and methods
that load and play audio clips.
The Object class is at the top of class hierarchy, and each class is its descendant (directly or
indirectly). Object provides behaviors that are required of all objects running in the Java
Virtual Machine. For example, all classes inherit Object's toString method, which returns a
string representation of the object.
We are not required to use every inherited method or variable and we are also allowed to
re-define an inherited method by overriding that method.
The diagram below shows the class hierarchy, highlighting the super classes of Applet .
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.
Methods of Applet Life Cycle
Applet Initialized
There are five methods of an applet life cycle, and they are:
init()
This method is called when the Applet is first loaded into memory. It initializes the Applet
and sets its default values. This method is called only once in the Applet life cycle.
start()
This method is called after the init() method. It starts the execution of the Applet and
performs all necessary tasks.
paint (Graphics g)
This method is called whenever the Applet needs to be painted on the screen. It is
responsible for all the necessary drawing tasks.
stop()
This method is called when the Applet is stopped. It stops the execution of the Applet and
releases any resources that it was using.
destroy()
This method is called when the Applet is being removed from memory. It releases all of the
resources used by the Applet.
1. By html file.
2. By appletViewer tool (for testing purpose).
To execute the applet by html file, create an applet and compile it. After that create an html
file and place the applet code in html file. Now click the html file.
//First.java
import java.applet.*;
import java.awt.*;
public class First extends Applet{
public void paint(Graphics g){
g.drawString("welcome",150,150);
} }
myapplet.html
<html>
<body>
<applet code="First.class" width="300" height="300">
</applet>
</body>
</html>
c:\>appletviewer First.java
#https://www.studytonight.com/java/java-applet.php
An application program is
An applet program is needed to
Program needed to perform some tasks
perform small tasks or part of them.
directly for the user.
• Browser-based
Applets run inside a web browser, providing a convenient way to add
interactivity and dynamic content to web pages.
• Deployment
Applets are typically embedded in HTML pages using the <applet> tag or the
newer <object> tag.
• Platform-independent
Applets are written in Java and can run on any platform that supports a Java
Virtual Machine (JVM), which makes them highly portable.
• Security
Applets provide a high level of security and cannot access system resources or
sensitive information.
• Access to Resources
Applications have access to system resources allowing them to perform complex
tasks and manipulate data.
• User interaction
Applications provide user interaction through a GUI or CLI.
• Deployment
They are generally distributed as executable files or installers. It can be
downloaded and installed on a computer or other device.
• Platform-specific
They are usually developed for specific operating systems or hardware platforms.
• Extensibility
Applications can be extended through plug-ins, add-ons, or other third-party
software components.
Swing Applet
Swing have look and feel according to user Applet Does not provide this facility.
view you can change look and feel using
UIManager.
Swing uses for stand lone Applications, Applet need HTML code for Run the Applet.
Swing have main method to execute the
program.
Swing uses MVC Model view Controller. Applet not.
Swing have its own Layout like most popular Applet uses AWT Layouts like flowlayout.
Box Layout.
Swing have some Thread rules. Applet doesn't have any rule.
Requesting Repainting
§ An applet writes to its window only when its update() or paint( ) method is called by
the AWT.
§ Whenever your applet needs to update the information displayed in its window, it
simply calls repaint( ).
§ The repaint( ) method is defined by the AWT. It causes the AWT run-time system to
execute a call to your applet’s update( ) method, which, in its default implementation,
calls paint( ).
update()
paint()
{
drawstrings()