0% found this document useful (0 votes)
5 views11 pages

Java Unit 5

The document provides an overview of Java applets, including their advantages, disadvantages, and lifecycle methods. It outlines the steps for developing an applet and details the key methods like init, start, stop, and destroy. Additionally, it includes sample code for creating and executing a simple applet using HTML and the applet viewer tool.

Uploaded by

pardhivmaroju09
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)
5 views11 pages

Java Unit 5

The document provides an overview of Java applets, including their advantages, disadvantages, and lifecycle methods. It outlines the steps for developing an applet and details the key methods like init, start, stop, and destroy. Additionally, it includes sample code for creating and executing a simple applet using HTML and the applet viewer tool.

Uploaded by

pardhivmaroju09
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

INTRODUCTION

Applet is a small program


-can be placed on a web page
willbe executed by the web browser
give web pages *dynamic content"
- Applet class is one of the AWT components

" Java applets are usually graphical


-Draw graphics in a defined screen area
- Enable user interaction with GUI elements
ADVANTAGES AND DISADVANTAGES (T
APPLET

Advantages
" 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.
Disadvantages
Plugin is
applet.
required at client browser to
execute
STEPS IN DEVELOPMENT OFCt
APPLET
1. Build an applet code (java)
2. Create an executable applet (.class)
3. Desigining a web page using HTML tags
4. Prepare <APPLET> Tag
5. Incorporate <APPLET> Tag into web page
6. Create HTML file
7. Test the Applet

LIFECYCLE OF JAVA APPLET C

Applet is initialized.
" Applet is started.
Applet is painted.
Applet is stopped.
Applet is destroyed.
Methods are called in this order

" init and destroy are only


init() called once each
start and stop are called
start() whenever the browser enters
and leaves the page
do some work
do some work is code called
by your listeners
stop()
paint is called when the
applet needs to be repainted
destroy()

APPLET METHODS
public void init ()
public void start ()
public void stop ()
public void destroy ()
public void paint (Graphics)
Also:
public void repaint()
public void update (Graphics)
public void showStatus(String)
public String getParameter(String)
LIFECYCLE METHODS FORAPPLET
[Link] class
It provides 4 life cycle methods of applet.
public void init): is used to initialized the Applet. It is invoked
only once.
public void start(): is invoked after the init() method or browser is
maximized. It is used to start the Applet.
public void stop(): is used to stop the Applet. It is invoked when
Applet is stop or browser is minimized.
public void destroy0: is used to destroy the Applet.
[Link] class
public void paint(Graphics g): is used to paint the Applet. It
provides Graphics class object that can be used for drawing oval,
rectangle, arc etc.

public void init ()


" This is the first method to execute
" It is an ideal place to initialize variables
" It is the best place to define the GUI
Components (buttons, text fields, scrollbars,
etc.), lay them out, and add listeners to them
" Almost every applet you ever write will have
an init( ) method
publicvoid start ()
" Not always needed
" Called after init( )
" Called cach time the page is loaded and
restarted
"Used mostly in conjunction with stop( )
" start() and stop( ) are used when the Applet is
doing time-consuming calculations that you
don't want to continue when the page is not in
front

public void stop()


" Not always needed
" Called when the browser leaves the page
Called just before destroy( )
Use stop() if the applet is doing heavy
computation that you don't want to continue
when the browser is on some other page
Used mostly in conjunction with start()
public void destroy()
" Rarely needed
" Called after stop( )
" Use to explicitly release system resources (like
thrcads)
System resources are usually released
automatically

SAMPLE GRAPHICS METHODS W


A
Graphics is something you can paint on
[Link]("Hello", 20, 20); Hello
[Link](x, y, width, height);
[Link](x, y, width, height);
[Link](x, y, width, height);
[Link](x, y, width, height);
[Link]([Link]);
SYNTAX
import [Link];
import [Link];

[Link] class Appletname extends Applet

public void paint(Graphics g)

SIMPLE EXAMPLE OF APPLET W


To cxecute 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.

I/[Link] [Link]
<html>
import [Link];
import [Link]; <body>
<applet code-"[Link]" width="300" h
public class First extends Applet cight="300">
<applet>
public void paint(Graphics g) <body>
<html>
[Link]("welcome", I50, I50);
EXECUTINGAND RUNNING AN C
APPLET
There are two ways to run an applet
" By html file.
" By appletViewer tool (for testing purpose).

Execute the applet by appletviewer tool, write in


command prompt:
"c:>javac [Link]
" c:>appletviewer [Link]

SIMPLIFIED PROGRAM
import [Link];
import [Link];
*<applet code="[Link]" width="300" height="300">
</applet>*!

public class First extends Applet{


public void paint(Graphics g){
[Link]("Welcome to Java Programming!", 150, 150);

Iaoolet Vinet Welc omnonlel rlat


Google
EXECUTING AND RUNNING AN Ci
APPLET

There are two ways to run an applet


" By html file.
" By appletViewer tool (for testing purpose).

Execute the applet by appletviewer tool, write in


command prompt:
c:>javac [Link]
c:>appletviewer [Link]

SIMPLIFIED PROGRAM W
import [Link];
import [Link];
t<applet code="[Link]" width="300" height="300">
</applet>*/

public class First extends Applet{

public void paint(Graphics g){


[Link]("Welcome to Java Pronrammingt" 150.150):

You might also like