You are on page 1of 15

Concept of Applets

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.

Parameters Java Application Java Applet

Meaning A Java Application also The Java applet works on


known as application the client side, and runs
program is a type of on the browser and makes
program that use of another application
independently executes on program so that we can
the computer. execute it.
Requirement of main( ) Its execution starts with It does not require the use
method the main( ) method only. of any main() method. Java
The use of the main( ) is applet initializes through
mandatory. init( ) method.

Execution It cannot run It cannot start


independently, but independently but
requires JRE to run. requires APIs for use
(Example. APIs like Web
Installation We need to install the Java Java
API). applet does not need
application first and to be pre-installed.
obviously on the local
Lifecycle of an Applet
The lifecycle of an applet involves several key methods such as init(), start(), stop(), and destroy().
The init() method is used for applet initialization, start() for starting the applet's execution, stop() for
suspending the execution, and destroy() for the applet's termination. These methods provide
control over the applet's execution at different stages.
When the functioning of an applet begins the following methods are
called in a sequence:

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()

Now we will go through each of these methods one by one:

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.

Specifying Local applet:


Remote Applet:
A remote applet is designed and developed by another developer. It is located or available on a
remote computer that is connected to the internet. In order to run the applet stored in the remote
computer, our system is connected to the internet then we can download run it. In order to locate
and load a remote applet, we must know the applet's address on the web that is referred to as
Uniform Recourse Locator(URL).

Specifying Remote applet:


Creating Applets
Create a subclass of the java.applet.Applet class.

Implement the init(), start(), stop(), and destroy() methods.

3. Add any GUI components to the applet's content pane.

Compile the applet and run it using the AppletViewer tool.

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.

• Running the Applet in a web browser compatible with Java.


• Use an applet viewer, like the normal instrument, to view applets. In a window, an applet
viewer runs your 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).

< applet code = "MyApplet" width=400 height=400 >


< /applet >

Run the HTML file

Running Applet using Applet Viewer


Passing Parameters to Applets
parameters are passed to applets in NAME=VALUE pairs in <param> tags between the opening and
closing APPLET tags. Inside the applet, you read the values passed through the PARAM tags with the
getParameter() method of the java.applet.Applet class.

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.*;

public class DrawStringApplet extends Applet {

private String defaultMessage = "Hello!";

public void paint(Graphics g) {

String inputFromPage = this.getParameter("Message");


if (inputFromPage == null) inputFromPage = defaultMessage;
g.drawString(inputFromPage, 50, 25);

}
}

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.

Lack of Components Appearance


AWT does not provide a wide range of AWT's look-and-feel is not as modern and
components for UI development. visually appealing as Swing's.
Limitations of AWT
The AWT defines a basic set of controls, windows, and dialog boxes that support a usable, but
limited graphical interface. One reason for the limited nature of the AWT is that it translates its
various visual components into their corresponding, platform-specific equivalents or peers. This
means that the look and feel of a component is defined by the platform, not by java. Because the
AWT components use native code resources, they are referred to as heavy weight. The use of native
peers led to several problems. First, because of variations between operating systems, a component
might look, or even act, differently on different platforms. This variability threatened java’s
philosophy: write once, run anywhere. Second, the look and feel of each component was fixed and
could not be changed. Third, the use of heavyweight components caused some frustrating
restrictions.
Due to these limitations Swing came and was integrated to java.

Two key Swing features are:

Swing components are light weight,

Swing supports a pluggable look and feel.


MVC Architecture
The Model-View-Controller (MVC) is a well-known design pattern in the web development field. It is
way to organize our code. It specifies that a program or application shall consist of data model,
presentation information and control information. The MVC pattern needs all these components to
be separated as different objects.

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.

The MVC pattern architecture consists of three layers:

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

Component A Component is the Abstract base class for


about the non-menu user-interface controls
of Java SWING. Components are
representing an object with a graphical
representation.
Container A Container is a component that can
container Java SWING Components

JComponent A JComponent is a base class for all swing


UI Components In order to use a swing
component that inherits from JComponent,
the component must be in a containment
hierarchy whose root is a top-level Java
Swing container.
JLabel A JLabel is an object component for placing
text in a container.

JButton This class creates a labeled button.

JColorChooser A JColorChooser provides a pane of controls


designed to allow the user to manipulate
and select a color.

JCheckBox A JCheckBox is a graphical (GUI) component


that can be in either an on-(true) or off-
(false) state.

JRadioButton The JRadioButton class is a graphical (GUI)


component that can be in either an on-
(true) or off-(false) state. in the group

You might also like