0% found this document useful (0 votes)
3 views9 pages

Java Applet

Java Applets are embedded programs that run in web browsers to create dynamic content, offering advantages such as client-side execution and security. There are two types of applets: local and remote, with a defined life cycle consisting of methods like init(), start(), paint(), stop(), and destroy(). Applets differ from Java applications in terms of execution requirements, resource access, and security restrictions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

Java Applet

Java Applets are embedded programs that run in web browsers to create dynamic content, offering advantages such as client-side execution and security. There are two types of applets: local and remote, with a defined life cycle consisting of methods like init(), start(), paint(), stop(), and destroy(). Applets differ from Java applications in terms of execution requirements, resource access, and security restrictions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

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:

o It works at client side so less response time.


o Secured [Applet have strict security rule that are enforced by the web browser]
o An Applete is a Java class that extends the java.applet.applet class.
o Use public void paint() instead of main() method.
o It can be run on any browser which has JVM running in it.
o It can be executed by browsers running under many plateforms, including Linux,
Windows, Mac Os etc.

What are the Types of Applets in Java?


In java there are two types of applets:

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.

All object-oriented language use classes,


objects, (instance of classes) and
methods. When we create our own applet
classes, they descend from the Applet class and
inherit methods and variables defined in
the Applet class.

Not only do our own applets inherit from


the Applet class but also from the super
classes of Applet such as Panel, Container,
Component and Object.

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 .

Let’s understand first how many Package does GUI support:


1. AWT(Abstract Window Toolkit)
2. Swing

Applet Life Cycle in Java


In Java, an applet 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.
Methods of Applet Life Cycle

Applet Initialized

Applet Stop Applet Destroy


Applet Running

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.

Who is responsible to manage the life cycle of an applet?

Java Plug-in software.

How to run an Applet?

There are two ways to run an applet

1. By html file.
2. By appletViewer tool (for testing purpose).

Simple example of Applet by html file:

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>

To execute the applet by appletviewer tool, write in command prompt:


c:\>javac First.java

Check that a class file has been created...

> dir First.*

c:\>appletviewer First.java

#https://www.studytonight.com/java/java-applet.php

The difference between Application and Applet:

Parameters Java Application Java Applet

Applets are small Java programs that


Applications are just like a Java
are designed to be included with the
program that can be executed
Definition HTML web document. They require a
independently without using
Java-enabled web browser for
the web browser.
execution.
Parameters Java Application Java Applet

The application program The applet does not require the


main ()
requires a main() method for main() method for its execution
method
its execution. instead init() method is required.

The “javac” command is used


Applet programs are compiled with
to compile application
the “javac” command and run using
Compilation programs, which are then
either the “appletviewer” command
executed using the “java”
or the web browser.
command.

Java application programs


Applets don’t have local disk and
File access have full access to the local file
network access.
system and network.

Applications can access all Applets can only access browser-


Access level kinds of resources available on specific services. They don’t have
the system. access to the local system.

First and foremost, the


installation of a Java The Java applet does not need to be
Installation
application on the local installed beforehand.
computer is required.

Applications can execute the


Applets cannot execute programs
Execution programs from the local
from the local machine.
system.

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.

It cannot start on its own, but it can


It cannot run on its own; it
Run be executed using a Java-enabled
needs JRE to execute.
web browser.

Connection Connectivity with other servers It is unable to connect to other


with servers is possible. servers.
Parameters Java Application Java Applet

Read and It supports the reading and


It does not support the reading and
Write writing of files on the local
writing of files on the local computer.
Operation computer.

Application can access the Executed in a more restricted


system’s data and resources environment with tighter security.
Security
without any security They can only use services that are
limitations. exclusive to their browser.

Java applications are self-


Applet programs cannot run on their
contained and require no
Restrictions own, necessitating the maximum
additional security because
level of security.
they are trusted.

Features of Java Applet:


The main features of the applet are discussed below.
• Small size
Applets are typically small, making them easy to download and embed in web
pages.

• Browser-based
Applets run inside a web browser, providing a convenient way to add
interactivity and dynamic content to web pages.

• Restricted access to resources


Applets are run in a sandboxed environment, which restricts their access to
system resources such as memory, processing power, and storage.

• Limited user interaction


Applets typically provide limited user interaction, usually through HTML controls
or UI components provided by the applet itself.

• 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.

Java Application Features


The main features of the application are discussed below.
• Standalone
Applications are programs installed on a computer or other device and run
independently of a web browser or other software.

• 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.

Difference between Swing and Applet

Swing Applet

Swing is light weight Component. Applet is heavy weight Component.

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.

To execute Swing no need any browser By To execute Applet programe we should


which we can create stand alone application need any one browser like Appletviewer,
But Here we have to add container and web browser. Because Applet using browser
maintain all action control with in frame container to run and all action control with
container. in browser container.

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

• Four forms of repaint()


1. void repaint( )
2. void repaint(int left, int top, int width, int height)
3. void repaint(long maxDelay)
4. void repaint(long maxDelay, int x, int y, int width, int height)

§ The first version causes the entire window to be repainted.


§ The second version specifies a region that will be repainted. Here, the coordinates of
the upper-left corner of the region are specified by left and top, and the width and
height of the region are passed in width and height.
§ In 3rd and 4th forms, maxDelay specifies the maximum number of milliseconds that
can elapse before update( ) is called.
repaint()

update()

paint()
{

drawstrings()

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy