SlideShare a Scribd company logo
Applet
Programming
Introduction
• There are two kinds of Java programs-
–Applications (stand-alone programs)
–Applets.
• An Applet is a small Internet-based program that
has the Graphical User Interface (GUI), written in
the Java programming language.
Introduction
• A Java applet is a small application written in
Java and delivered to users in the form
of bytecode.
• A applet is typically embedded in a Web page
and can be run from a browser.
• Applets can be transported over the Internet
from one computer to another.
How applet differ from application
• Applets are the small programs, applications are
larger programs.
• Applets don't have the main method while in an
application execution starts with the main method.
• Applications run independently while Applets
cannot. They run from inside the web page.
• Applets cannot read from or write to the files in
the local computer.
Introduction
• An applet can perform
–arithmetic operation,
–display graphics,
–play sounds,
–accept user input,
–create animation and
–play interactive games.
Local and Remote Applets
• We can embed applets into web pages in
two ways-
1. Create our own applets ( Local )
2. Download an applet from remote computer
system (Remote Applet )
Local Applet
• An applet developed locally and stored in local
system is known as local applet.
• When a web page trying to find a local applet, it
does not need to use the Internet.
• It simply searches the directories in the local
system and locates and loads the specified
applet. local applet
Remote Applet
• A remote applet is that which is developed by
someone else and stored on the remote
computer connected to internet.
• Users can download the remote applet onto
there system and run it.
Local Computer Remote Computer
Internet
Steps involved in developing applets
1. Building an applet code (. java file )
2. Creating an executable applet ( .class file )
3. Designing a Web Page using HTML tags.
4. Preparing <APPLET> tag.
5. Incorporating <APPLET> tag into the Web
Page.
6. Creating HTML file.
7. Testing the Applet Code.
Building Applet Code
• It is essential that applet code uses services of
two classes
–Applet class and
–Graphics class.
Chain of classes inherited by Applet class
java.lang.Object
|
+----java.awt.Component
|
+----java.awt.Container
|
+----java.awt.Panel
|
+----java.applet.Applet
The Applet Class
• The java.applet package is the smallest package
in Java API.
• The Applet class is the only class in the package.
• The Applet class provides life and behaviour to
applet through its methods.
• The Applet class has many methods that are used
to display images, play audio files etc but it has
no main() method.
Methods of Applet Class
• init() :
– used for whatever initializations are needed for
applet.
– Applets can have a default constructor, but it is better
to perform all initializations in the init method
instead of the default constructor.
• start() :
– This method is automatically called after Java calls
the init method.
• stop() :
– This method is automatically called when the user
moves off the page where the applet sits.
• destroy():
– Java calls this method when the browser shuts
down.
paint() Method of Applet Class
• Paint method actually displays the result of applet
code on the screen.
• The output may be text, graphics, or sound.
• Syntax-
public void paint(Graphics g)
• The paint method requires graphics object as an
argument.
• Hence we require to import Graphics class from
java.awt package.
Applet Life Cycle
Born
Running Idle
Dead End
Stopped
Destroyed
Exit browser
Begin
(Load applet) Initialization
Display
paint()
start()
stop()
start()
destroy()
Applet state transition diagram
Born or Initialization State
• An applet begins its life when the web browser
loads its classes and calls its init() method.
• This is called exactly once in Applets lifecycle.
• init() method provides initialization code such as
initialization of variables, loading images or
fonts.
Eg.
public void init()
{
//initialization
}
Running State
• Once the initialization is complete, the web
browser will call the start() method in the applet.
• This method must called atleast once in the
Applets lifecycle.
• The start() method can also be called if the
Applet is in “Idle” state.
Eg. public void start()
{
//Code
}
Stopped State
• The web browser will call the Applets stop()
method, if the user moved to another web page
while the applet was executing.
• The stop() method is called atleast once in
Applets Lifecycle.
Eg. public void stop()
{
//Code
}
Dead State
• Finally, if the user decides to quit the web
browser, the web browser will free up system
resources by killing the applet before it closes.
• To do so, it will call the applets destroy()
method.
Eg.
public void destroy()
{
// Code
}
Display State
• Applet moves to the display state whenever it
has to perform the output operations on the
screen.
• This happens immediately after the applet
enters into the running state.
• The paint() method is called to accomplish this
task.
Eg.
public void paint(Graphics g)
{
//Display Statements
General format of Applet Code
import java.awt.*;
import java.applet.*;
……………………
public class applet_class_name extends Applet
{ ……………
public void paint(Graphics g)
{
………….//applet operation code
}
}
Example.
import java.awt.*;
import java.applet.*;
public class SimpleApplet extends Applet
{
public void paint(Graphics g)
{
g.drawString("My First Applet",40,40);
}
}
/*<applet code="SimpleApplet.class" width=200
height=100>
</applet>*/
Designing a Web Page
• To execute an applet in a web browser, write a short
HTML text file that contains the appropriate APPLET
tag.
• For above example it is
<html>
<body>
<applet code="SimpleApplet.class" width=200
height=100>
</applet>
</body>
</html>
The Applet tag
<applet
[codebase=codebaseURL]
code=”Applet file”
[ALT=”alternative text]
[name=AppletInstanceName]
Width=pixels
height= pixels
[align= alignment]
>
[<param name=”Attributename” value =”Attribute value”]
[<param name=”Attributename” value =”Attribute value”]
</applet>
Passing Parameter to Applet
• We can supply user-defined parameters to an
applet using <param.....> tag.
• Each <param....> tag has a name ,and a value
attribute.
• For e.g. the color of the text can be changed to
red by an applet using a <param...> tag as follows
<applet ……>
<param name=“color” value=“RED”>
</applet>
Passing Parameters to Applet
• To handle parameters, we need to do two things.
1) Include <param.....> tags in HTML document.
2) Provide code in the applet to pass these parameters.
• We can define the init() method in the applet to
get parameters defined in the <param> tags.
•
• This is done using the getParameter() method,
which takes one string argument representing
the name of the parameter and returns a string
containing the value of that parameter.
Example :Parameter Passing
import java.awt.*;
import java.applet.*;
public class HelloJava extends Applet
{ String str;
public void init()
{ str=getParameter(“String”);
if(str==null)
str=“Java”;
str=“Hello ” +str;
}
Public void paint(Graphics g)
{ g.drawString(“str”,10,10)
<html>
<body>
<applet code=“HelloJava.class”
width=400
height=400 >
<param name= “String” value= “Applet!”
>
</applet>
</body>
</html>
Displaying numerical values
import java.awt.*;
import java.applet.*;
public class Hellojava extends Applet{
public void paint(Graphics g)
{ int value1 =10;
int value2 = 20;
int sum =value1+value2;
String s= "Sum:"+String.valueOf(sum);
g.drawString(s,10,100);
}
Using Control Structure
import java.awt.*;
import java.applet.*;
public class Hellojava extends Applet {
public void paint(Graphics g) {
int y=50;
for(int i=1;i<=10;i++)
{ g.drawString(i+” ”,50,y);
y=y+10;
} } }
/*<applet code=“Hellojava.class” width=300 height=100>
</applet> */

More Related Content

PPTX
applet.pptx
PDF
27 applet programming
PPT
Applet and graphics programming
PPTX
PROGRAMMING IN JAVA- unit 4-part I
PPTX
Applet.pptx
PPTX
Java applet
PPTX
Applets in Java
PPTX
javaapplet.pptx bhhhhjhjhjhjhjhjhjhj
applet.pptx
27 applet programming
Applet and graphics programming
PROGRAMMING IN JAVA- unit 4-part I
Applet.pptx
Java applet
Applets in Java
javaapplet.pptx bhhhhjhjhjhjhjhjhjhj

Similar to MSBTE Computer Engineering Java applet.pptx (20)

PPT
Jsp applet
PDF
Java applet basics
PPT
Java files and io streams
PPTX
Applets
PPT
Java programming Java programming Java programming
PPTX
Java applet
PPTX
Java Apple dndkorksnsbsjdkkdjejdjrdndjdj
PPTX
Applet intro
PPT
Unit 7 Java
PDF
6applets And Graphics
PPTX
Applet programming1
PPTX
Appletjava
PPTX
Introduction To Applets methods and simple examples
DOC
Applet
PPT
Advanced Programming, Java Programming, Applets.ppt
PDF
Advanced programming chapter 2 - Java Applet.pdf
PDF
Smart material - Unit 3 (2).pdf
PDF
Smart material - Unit 3 (1).pdf
Jsp applet
Java applet basics
Java files and io streams
Applets
Java programming Java programming Java programming
Java applet
Java Apple dndkorksnsbsjdkkdjejdjrdndjdj
Applet intro
Unit 7 Java
6applets And Graphics
Applet programming1
Appletjava
Introduction To Applets methods and simple examples
Applet
Advanced Programming, Java Programming, Applets.ppt
Advanced programming chapter 2 - Java Applet.pdf
Smart material - Unit 3 (2).pdf
Smart material - Unit 3 (1).pdf
Ad

Recently uploaded (20)

PDF
III.4.1.2_The_Space_Environment.p pdffdf
PPTX
additive manufacturing of ss316l using mig welding
PDF
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
PPTX
Sustainable Sites - Green Building Construction
PPT
Total quality management ppt for engineering students
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
Current and future trends in Computer Vision.pptx
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPT
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
PPTX
Fundamentals of safety and accident prevention -final (1).pptx
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
A SYSTEMATIC REVIEW OF APPLICATIONS IN FRAUD DETECTION
PPTX
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
PPT
Project quality management in manufacturing
III.4.1.2_The_Space_Environment.p pdffdf
additive manufacturing of ss316l using mig welding
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
Foundation to blockchain - A guide to Blockchain Tech
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
Sustainable Sites - Green Building Construction
Total quality management ppt for engineering students
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Current and future trends in Computer Vision.pptx
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
CYBER-CRIMES AND SECURITY A guide to understanding
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
Fundamentals of safety and accident prevention -final (1).pptx
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
A SYSTEMATIC REVIEW OF APPLICATIONS IN FRAUD DETECTION
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
Project quality management in manufacturing
Ad

MSBTE Computer Engineering Java applet.pptx

  • 2. Introduction • There are two kinds of Java programs- –Applications (stand-alone programs) –Applets. • An Applet is a small Internet-based program that has the Graphical User Interface (GUI), written in the Java programming language.
  • 3. Introduction • A Java applet is a small application written in Java and delivered to users in the form of bytecode. • A applet is typically embedded in a Web page and can be run from a browser. • Applets can be transported over the Internet from one computer to another.
  • 4. How applet differ from application • Applets are the small programs, applications are larger programs. • Applets don't have the main method while in an application execution starts with the main method. • Applications run independently while Applets cannot. They run from inside the web page. • Applets cannot read from or write to the files in the local computer.
  • 5. Introduction • An applet can perform –arithmetic operation, –display graphics, –play sounds, –accept user input, –create animation and –play interactive games.
  • 6. Local and Remote Applets • We can embed applets into web pages in two ways- 1. Create our own applets ( Local ) 2. Download an applet from remote computer system (Remote Applet )
  • 7. Local Applet • An applet developed locally and stored in local system is known as local applet. • When a web page trying to find a local applet, it does not need to use the Internet. • It simply searches the directories in the local system and locates and loads the specified applet. local applet
  • 8. Remote Applet • A remote applet is that which is developed by someone else and stored on the remote computer connected to internet. • Users can download the remote applet onto there system and run it. Local Computer Remote Computer Internet
  • 9. Steps involved in developing applets 1. Building an applet code (. java file ) 2. Creating an executable applet ( .class file ) 3. Designing a Web Page using HTML tags. 4. Preparing <APPLET> tag. 5. Incorporating <APPLET> tag into the Web Page. 6. Creating HTML file. 7. Testing the Applet Code.
  • 10. Building Applet Code • It is essential that applet code uses services of two classes –Applet class and –Graphics class.
  • 11. Chain of classes inherited by Applet class java.lang.Object | +----java.awt.Component | +----java.awt.Container | +----java.awt.Panel | +----java.applet.Applet
  • 12. The Applet Class • The java.applet package is the smallest package in Java API. • The Applet class is the only class in the package. • The Applet class provides life and behaviour to applet through its methods. • The Applet class has many methods that are used to display images, play audio files etc but it has no main() method.
  • 13. Methods of Applet Class • init() : – used for whatever initializations are needed for applet. – Applets can have a default constructor, but it is better to perform all initializations in the init method instead of the default constructor. • start() : – This method is automatically called after Java calls the init method.
  • 14. • stop() : – This method is automatically called when the user moves off the page where the applet sits. • destroy(): – Java calls this method when the browser shuts down.
  • 15. paint() Method of Applet Class • Paint method actually displays the result of applet code on the screen. • The output may be text, graphics, or sound. • Syntax- public void paint(Graphics g) • The paint method requires graphics object as an argument. • Hence we require to import Graphics class from java.awt package.
  • 16. Applet Life Cycle Born Running Idle Dead End Stopped Destroyed Exit browser Begin (Load applet) Initialization Display paint() start() stop() start() destroy() Applet state transition diagram
  • 17. Born or Initialization State • An applet begins its life when the web browser loads its classes and calls its init() method. • This is called exactly once in Applets lifecycle. • init() method provides initialization code such as initialization of variables, loading images or fonts. Eg. public void init() { //initialization }
  • 18. Running State • Once the initialization is complete, the web browser will call the start() method in the applet. • This method must called atleast once in the Applets lifecycle. • The start() method can also be called if the Applet is in “Idle” state. Eg. public void start() { //Code }
  • 19. Stopped State • The web browser will call the Applets stop() method, if the user moved to another web page while the applet was executing. • The stop() method is called atleast once in Applets Lifecycle. Eg. public void stop() { //Code }
  • 20. Dead State • Finally, if the user decides to quit the web browser, the web browser will free up system resources by killing the applet before it closes. • To do so, it will call the applets destroy() method. Eg. public void destroy() { // Code }
  • 21. Display State • Applet moves to the display state whenever it has to perform the output operations on the screen. • This happens immediately after the applet enters into the running state. • The paint() method is called to accomplish this task. Eg. public void paint(Graphics g) { //Display Statements
  • 22. General format of Applet Code import java.awt.*; import java.applet.*; …………………… public class applet_class_name extends Applet { …………… public void paint(Graphics g) { ………….//applet operation code } }
  • 23. Example. import java.awt.*; import java.applet.*; public class SimpleApplet extends Applet { public void paint(Graphics g) { g.drawString("My First Applet",40,40); } } /*<applet code="SimpleApplet.class" width=200 height=100> </applet>*/
  • 24. Designing a Web Page • To execute an applet in a web browser, write a short HTML text file that contains the appropriate APPLET tag. • For above example it is <html> <body> <applet code="SimpleApplet.class" width=200 height=100> </applet> </body> </html>
  • 25. The Applet tag <applet [codebase=codebaseURL] code=”Applet file” [ALT=”alternative text] [name=AppletInstanceName] Width=pixels height= pixels [align= alignment] > [<param name=”Attributename” value =”Attribute value”] [<param name=”Attributename” value =”Attribute value”] </applet>
  • 26. Passing Parameter to Applet • We can supply user-defined parameters to an applet using <param.....> tag. • Each <param....> tag has a name ,and a value attribute. • For e.g. the color of the text can be changed to red by an applet using a <param...> tag as follows <applet ……> <param name=“color” value=“RED”> </applet>
  • 27. Passing Parameters to Applet • To handle parameters, we need to do two things. 1) Include <param.....> tags in HTML document. 2) Provide code in the applet to pass these parameters. • We can define the init() method in the applet to get parameters defined in the <param> tags. • • This is done using the getParameter() method, which takes one string argument representing the name of the parameter and returns a string containing the value of that parameter.
  • 28. Example :Parameter Passing import java.awt.*; import java.applet.*; public class HelloJava extends Applet { String str; public void init() { str=getParameter(“String”); if(str==null) str=“Java”; str=“Hello ” +str; } Public void paint(Graphics g) { g.drawString(“str”,10,10)
  • 29. <html> <body> <applet code=“HelloJava.class” width=400 height=400 > <param name= “String” value= “Applet!” > </applet> </body> </html>
  • 30. Displaying numerical values import java.awt.*; import java.applet.*; public class Hellojava extends Applet{ public void paint(Graphics g) { int value1 =10; int value2 = 20; int sum =value1+value2; String s= "Sum:"+String.valueOf(sum); g.drawString(s,10,100); }
  • 31. Using Control Structure import java.awt.*; import java.applet.*; public class Hellojava extends Applet { public void paint(Graphics g) { int y=50; for(int i=1;i<=10;i++) { g.drawString(i+” ”,50,y); y=y+10; } } } /*<applet code=“Hellojava.class” width=300 height=100> </applet> */