0% found this document useful (0 votes)
7 views17 pages

20 - Applet

Uploaded by

Vaibhav Damdhar
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)
7 views17 pages

20 - Applet

Uploaded by

Vaibhav Damdhar
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/ 17

Applet

Java Applet
Learning Objectives :

 Introduction of Applet

 Hierarchy of Applet

 Life Cycle of an Applet

 Life Cycle methods for Applet

 Applet Execution

 Example
Introduction of Java Applet
 Java Applications are stand alone programs
executed with Java interpreter.

 Applet is a small program that


 can be placed in a web page.
 will be executed by the web browser.
 gives web pages a “dynamic content”.

 Applet runs inside the browser and works at client


side.
Hierarchy of Applet

Applet class extends Panel.


Panel class extends Container which is the subclass of
Component.
Life Cycle of an Applet

Life Cycle of an Applet


1. Applet is initialized.
2. Applet is started.
3. Applet is painted.
4. Applet is stopped.
5.Applet is destroyed.
Lifecycle methods for Applet
 For creating any applet java.applet.Applet class
must be inherited.

 It provides 4 life cycle methods of applet.

 public void init(): This method is intended for


whatever initialization is needed for applet.
 It is called after the param tags inside the applet tag
have been processed.

 public void start() : This method is automatically


called after the browser calls the init method.
 It is also called whenever the user returns to the
page containing the applet after having gone off to
other pages.
Lifecycle methods for Applet
 public void stop(): This method is automatically
called when the user moves off the page on which
the applet sits.
 It can, therefore, be called repeatedly in the same
applet.

 public void destroy(): This method is only called


when the browser shuts down normally.
Applet Execution – HTML File
//First.java
import java.applet.Applet;
import java.awt.Graphics;
public class First extends Applet{
public void paint(Graphics g){
g.drawString(“Hello World",150,150); }}
HTML File
<html>
<body>
<applet code="First.class" width="300" height="300">
</applet>
</body>
</html>
Applet Layout Manager
A layout manager dictates the style of arranging the
components in a container.
Java supports following Layout Mangers:
FlowLayout

 BorderLayout
Applet Layout Manager
GridLayout

CardLayout

GridBagLayout
Bounding Box Concept
 A shape can be filled or unfilled, depending on which
method is invoked.
 The method parameters specify coordinates and
sizes.
 Shapes with curves, like an oval, are usually drawn
by specifying the shape’s bounding rectangle.
 An arc can be thought of as a section of an oval.
Drawing a Rectangle
50 X

20

40

100

page.drawRect (50, 20, 100, 40);

12
Drawing a Line
10 150 X

20

45

page.drawLine (10, 20, 150, 45);


or
page.drawLine (150, 45, 10, 20);
13
Drawing an Oval
17 X

2
0

80

boundi
ng
Y rectang 50
le

page.drawOval (175, 20, 50,


80);
14
Relative Co-ordinate System
Interview Questions
 What is an Applet ?
 What is a layout manager? Which is the default layout
managers for Applet ?
 Explain the lifecycle of Applet.
 What is the use of paint() in Applet ?
 What is passed as an argument to the paint() method?
 What is the significance of codebase attribute in
applet?
 Can you write a Java class that can be used both as an
an Applet as well as an application?
Any Questions

You might also like