Professional Documents
Culture Documents
Applets are small applications designed to be transmitted over the internet and executed within a
web browser. They provide interactive content and are embedded in HTML pages.
What is Applet
A Java applet is a Java program that can embed in web pages. It runs in a web browser and works
on the client-side. We can embed an applet in the HTML page using the <applet> or <object> tags
and hosted on the webserver.
Applets provide web applications with interactive functionality that is not possible with only HTML.
In response to user operations, an applet can change the graphic content provided. It makes an
applet suitable for demonstration, display, and teaching.
Differences between Applets and
Applications
While both are Java programs, applets are designed to be embedded in web pages, whereas
applications are standalone programs. Applets require a web browser plugin to run, while
applications do not.
Applets run in a sandbox environment, whereas applications have full access to system resources.
1. init()
2. start()
3. paint()
When the functioning of an applet is about to stop, then the following methods are
called in a sequence:
1. stop()
2. destroy()
init() : This is the very first method that is called. It is the place where we
should initialize variables. This method is called only one time.
start() : This method is called after init() method. This is called to restart
the applet after it terminates.
paint() : This method is called each time an AWT-based applet’s output is
reconstructed. This situation can take place frequently for many reasons.
stop() : This method is invoked whenever there is a situation that a web
browser leaves out the HTML document that contains the applet.
destroy( ) : The method is invoked whenever the environment signifies
that the applet requires to be removed completely from the memory. At this
point, we should release any resources that is begin used by the applet.
Types of Applets
A special type of Java program that runs in a Web browser is referred to as Applet. It has less
response time because it works on the client-side. It is much secured executed by the browser
under any of the platforms such as Windows, Linux and Mac OS etc. There are two types of applets
that a web page can contain.
1. Local Applet
2. Remote Applet
Local Applet:
Local Applet is written on our own, and then we will embed it into web pages. Local Applet is
developed locally and stored in the local system. A web page doesn't need the get the information
from the internet when it finds the local Applet in the system. It is specified or defined by the file
name or pathname. There are two attributes used in defining an applet, i.e., the codebase that
specifies the path name and code that defined the name of the file that contains Applet's code.
Simple Applet:
import java.awt.*;
import java.applet.*;
public class Simple extends Applet
{
public void paint(Graphics g)
{
g.drawString("A simple Applet", 20, 20);
}
}
How to run an Applet Program
In the same manner as you compiled your console programs, an Applet program is compiled. There
are, however, two methods of running an applet.
Create brief HTML file in the same folder to execute an Applet in a web browser. Include the
following code in the file's body tag. (Applet tag loads class Applet).
The program below demonstrates this with a generic string drawing applet. The applet parameter
"Message" is the string to be drawn.
import java.applet.*;
import java.awt.*;
}
}
You also need an HTML file that references your applet. The following simple HTML file will do:
<HTML>
<HEAD>
Introduction of Swings
Swing has about four times the number of User Interface [UI] components as AWT and is part of the
standard Java distribution. By today’s application GUI requirements, AWT is a limited
implementation, not quite capable of providing the components required for developing complex
GUIs required in modern commercial applications. The AWT component set has quite a few bugs
and does take up a lot of system resources when compared to equivalent Swing resources.
Netscape introduced its Internet Foundation Classes [IFC] library for use with Java. Its Classes
became very popular with programmers creating GUI’s for commercial applications.
Limitations of AWT
AWT (Abstract Window Toolkit) is an older toolkit for creating graphical user interfaces in Java. It has
limitations in terms of functionality and looks when compared to Swing. AWT lacks components like
tabbed panes, sliders, and other rich GUI elements.
In this section, we will discuss the MVC Architecture in Java, alongwith its advantages and
disadvantages and examples to understand the implementation of MVC in Java.
Model: It represents the business layer of application. It is an object to carry the data that can
also contain the logic to update controller if data is changed.
View: It represents the presentation layer of application. It is used to visualize the data that the
model contains.
Controller: It works on both the model and view. It is used to manage the flow of application,
i.e. data flow in the model object and to update the view whenever data is changed.
Class Description