[go: up one dir, main page]

0% found this document useful (0 votes)
71 views14 pages

Lecture-3.1.4

The document discusses Java applets, including their lifecycle and passing parameters. It describes how applets run in browsers at the client-side and provide dynamic content to webpages. The key stages of an applet's lifecycle are initialization, starting, painting, stopping, and destruction. Parameters can be passed to applets through HTML using <PARAM> tags and read within the applet using getParameter(). Sample code demonstrates a simple string drawing applet that passes a "Message" parameter to control the text displayed.

Uploaded by

Ýäšh Gãutâm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views14 pages

Lecture-3.1.4

The document discusses Java applets, including their lifecycle and passing parameters. It describes how applets run in browsers at the client-side and provide dynamic content to webpages. The key stages of an applet's lifecycle are initialization, starting, painting, stopping, and destruction. Parameters can be passed to applets through HTML using <PARAM> tags and read within the applet using getParameter(). Sample code demonstrates a simple string drawing applet that passes a "Message" parameter to control the text displayed.

Uploaded by

Ýäšh Gãutâm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

INSTITUTE : UIE

DEPARTMENT : CSE
Bachelor of Engineering (Computer Science & Engineering)
Java Programming (20CST-218)
TOPIC OF PRESENTATION:

Applet class, Applet life-cycle,


Passing parameters embedding in HTML

DISCOVER . LEARN . EMPOWER


Lecture Objectives

In this lecture, we will discuss:


• Applet class, Applet life-cycle,
• Passing parameters embedding
in HTML

2
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:
• 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.
Drawback of Applet
• Plugin is required at client browser to execute applet.
Lifecycle of Java Applet

• Applet is initialized.
• Applet is started.
• Applet is painted.
• Applet is stopped.
• Applet is destroyed.
Lifecycle methods for Applet
The java.applet.Applet class 4 life cycle methods and java.awt.Component class provides 1 life cycle
methods for an applet.
java.applet.Applet class
For creating any applet java.applet.Applet class must be inherited. 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 destroy(): is used to destroy the Applet. It is invoked only once.

java.awt.Component class
The Component class provides 1 life cycle method of applet.
• 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.
How to run an Applet?

There are two ways to run an applet


By html file.
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   myapplet.html
import java.applet.Applet;   <html>  
import java.awt.Graphics;   <body>  
public class First extends Applet{   <applet code="First.class" width="300" height="300">  
   </applet>  
public void paint(Graphics g){   </body>  
g.drawString("welcome",150,150);   </html>  
}  
}  

Note: class must be public because its object is created by


Java Plugin software that resides on the browser.
Simple example of Applet by appletviewer tool:

To execute the applet by appletviewer tool, create an applet that contains applet tag in comment and
compile it. After that run it by: appletviewer First.java. Now Html file is not required but it is for testing
purpose only.

//First.java  
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);  
}  }  
/* 
<applet code="First.class" width="300" height="300"> 
</applet> 
*/  

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


c:\>javac First.java
c:\>appletviewer First.java
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.
Passing Parameters to Applets
The program below demonstrates this with a generic string drawing applet. The applet parameter "Message" is the
string to be drawn.

You also need an HTML file that references your applet.


import java.applet.*; The following simple HTML file will do:
import java.awt.*; <HTML>
public class DrawStringApplet extends Applet { <HEAD>
private String defaultMessage = "Hello!"; <TITLE> Draw String </TITLE>
public void paint(Graphics g) { </HEAD>
String inputFromPage = <BODY>
this.getParameter("Message"); This is the applet:<P>
if (inputFromPage == null) inputFromPage = <APPLET code="DrawStringApplet" width="300"
height="50">
defaultMessage;
<PARAM name="Message" value="Howdy, there!">
g.drawString(inputFromPage, 50, 25); This page will be very boring if your
} browser doesn't understand Java.
} </APPLET>
</BODY>
</HTML>
MCQ
HTML stands for

HighText Machine Language


HyperText and links Markup Language
HyperText Markup Language
None of these

11
Summary:

In this session, you were able to :


• Learn about Applet class, Applet life-cycle,
• Understand Passing parameters embedding in HTML
References:
Books:
1. Balaguruswamy, Java.
2. A Primer, E.Balaguruswamy, Programming with Java, Tata McGraw Hill
Companies
3. John P. Flynt Thomson, Java Programming.

Video Lectures :
https://youtu.be/cC_Ij7WmP_k
https://youtu.be/WA4FDPTZsQk

Reference Links:
http://www.cafeaulait.org/course/week5/16.html
https://www.javatpoint.com/java-applet
https://www.javatpoint.com/Painting-in-applet
THANK YOU

You might also like