This document provides an overview of Java applets, including:
- Java programs are divided into applications and applets, with applets running across the internet to make web pages dynamic.
- Applets override lifecycle methods like init(), start(), stop(), and destroy() and are run by web browsers with Java plugins.
- Creation of applets involves extending the Applet class, overriding methods, compiling, and embedding in an HTML file.
- Pros of applets include faster response time and security, while a con is requiring browser plugins.
- A sample applet draws a string using the paint() method, and this can be run from an HTML file or the applet viewer tool.
3. Introduction
Java programs are divided into two main categories , applets
and applications .
An application is an ordinary java program.
An applet is a kind of java program that can be run across the
internet .
Applets are mainly used convert a static web page to dynamic
page.
Web is created with the help of hypertext markup language
(HTML).
4. Web site is collection of webpages of a particular organization.
A browser is a software which is used to browse the internet ie.,
used to view web pages.
Applets do not use main () method.
Applets needs html code to run .
Applets are run by java enabled web browser.
Java Plug-in software is responsible to manage the life cycle of
an applet.
5. There are two ways to run an applet :
By html file.
By appletViewer tool (for testing purpose).
The java.applet.Applet class four (init() , start() , stop() , destroy())
life cycle methods.
Java.awt.Component class provides one (paint()) life cycle methods
for an applet.
6. Creation Of Applets
In order to create an applet the following steps are to be
followed :
Create a class by extending applet class.
Override any one of the applet class method .
Compile the program to obtain the class file .
Create a html document and embed the
applet.
Run the applet along with the html page in
java enabled web browser.
7. Pros & Cons
PROS -
It works at client side so less response time.
Secured.
It can be executed by browsers running under many
plateforms ,including linux , windows , mac Os etc.
CONS –
Plugins is required at client browser to execute applet.
8. Applet Class
To deal with applets in java a special class is defined in java.applet
package called as Applet.
Applet class contains five special methods :
Init() – used for initialization of applet.
Start() – used to start an applet .
Paint() – used to paint the contents of the applet on the
window.
Stop() – used to stop an applet .
Destroy() – used to destroy an applet.
10. Example :Applet by html file
import java.applet.Applet;
import java.awt.Graphics;
public class First extends Applet
{
public void paint(Graphics g)
{
g.drawString("welcome",150,150);
}
}
12. Example : Applet by applet
viewer tool
import java.applet.Applet;
import java.awt.Graphics;
public class First extends Applet
{
public void paint(Graphics g)
{
g.drawString("welcome to applet",150,150);
}
}