SlideShare a Scribd company logo
DEPARTMENT OF COMPUTER SCIENCE
& ENGINEERING
Subject Name: Java Programming
Topics Covered: Java Applets
1
Name of the School: School of Computer Science and
Engineering
Course Code: E1UC307C Course Name:Java Programming
Faculty Name: Programe Name:
Types of Java Application
2
1. Stand alone application(Java application)
2. Web Application( Applet & JSP/Servlet)
Web applications are the applications users can use through a browser. Standalone
applications are known as Desktop/ Offline applications and can be accessed
offline.
Java applications
 Run in stand-alone mode
 No additional software required (such as a Web browser)
Java applets
 Compiled Java class files
 Run within a Web browser (or an appletviewer)
 Loaded from anywhere on the Internet
Applet
3
An Applet in Java is a small application that runs within a web browser or an
applet viewer. Applets are written in Java and are typically used to provide
interactive features in a web page, such as animations, games, or interactive
forms.
A main() method is not invoked on an applet, and an applet class will not define
main(). Applets are designed to be embedded within an HTML page.
Here is a basic example of how to create an applet in Java:
Steps to Create a Java Applet:
1.Import the necessary packages.
2.Extend the Applet class.
3.Override the init(), start(), stop(), and destroy() methods as needed.
4.Use the paint() method to draw content on the applet window.
5.Create an HTML file to embed the applet in a webpage (for older browsers).
Life Cycle Methods of Applet
4
Following are the methods for a full applet cycle.
 init() method
 start() method
 paint() method
 stop() method
 destroy() method
Life Cycle of Applet
5
Applet Life Cycle
Born
Running
Idle
Dead
Begin
init()
start()
paint()
stop()
start()
destroy()
End
In the life-cycle of execution, the
applet exists (lives) in one of these
4 states.
There are methods that can
changes the state of the applet. All
of these methods have a name and
they are called as callback methods.
These methods are named so
because they are called
automatically by the browser when
required for smooth execution of the
applet.
Here, programmers write the
above-mentioned methods with
some code but never calls.
Life Cycle steps
6
Four methods in the Applet class give you the framework on which you build any applet:
 init: This method is intended for whatever initialization is needed for your applet. It is
called after the param tags inside the applet tag have been processed.
 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.
 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.
 destroy: This method is only called when the browser shuts down normally. Because
applets are meant to live on an HTML page, you should not normally leave resources
behind after a user leaves the page that contains the applet.
 paint: Invoked immediately after the start() method, and also any time the applet
needs to repaint itself in the browser. The paint() method is actually inherited from the
java.awt.
7
init()
start()
stop()
destroy()
paint
do other work
 init and destroy are only
called once each
 start and stop are called
whenever the browser
enters and leaves the page
 do some work is code called
by your listeners
 paint is called again when
the applet needs to be
repainted
Applet Program
8
import java.applet.Applet;
import java.awt.Graphics;
public class HelloWorld extends Applet {
public void paint(Graphics g) {
g.drawString("Hello World!", 50, 25);
}
}
Compile this file HelloWorld.java and mention the path in HTML file
Now you have to create an HTML File that Includes the Applet.
Using a text editor, create a file named Hello.html in the same directory that
contains HelloWorld.class. This HTML file should contain the following text:
Embedding Applet in Web page
9
<HTML>
<HEAD>
<TITLE>
Hello World Applet
</TITLE>
</HEAD>
<body>
<h1>Hi, This is My First Java Applet on the Web!</h1>
<APPLET CODE="HelloWorld.class" width=500 height=400></APPLET>
</body>
</HTML>
Compile and Run an Applet
10
To compile: javac HelloWorld.java  Generates HelloWorld.class
To run:
a) Use the appletviewer from JDK
open command prompt and reach to folder
C:desktop> appletviewer index.html
b) Open page from browser:
index.html
Hello World.java
11
import java.applet.Applet;
import java.awt.*;
public class GraphicsDemo extends Applet{
public void paint(Graphics g){
g.setColor(Color.red);
g.drawString("Welcome",50, 50);
g.drawLine(20,30,20,300);
g.drawRect(70,100,30,30);
g.fillRect(170,100,30,30);
g.drawOval(70,200,30,30);
g.setColor(Color.pink);
g.fillOval(170,200,30,30);
g.drawArc(90,150,30,30,30,270);
g.fillArc(270,150,30,30,0,180); }
}
Displaying Image in Applet
12
import java.awt.*;
import java.applet.*;
public class DisplayImage extends Applet {
Image picture;
public void init() {
picture = getImage(getDocumentBase(),“abc.jpg");
}
public void paint(Graphics g) {
g.drawImage(picture, 30,30, this);
}
}
Passing Parameters to Applet
13
<HTML>
<HEAD><TITLE> Hello World Applet</TITLE></HEAD>
<body>
<h1>Hi, This is My First Communicating Applet on the Web!</h1>
<APPLET
CODE="HelloAppletMsg.class" width=500 height=400>
<PARAM NAME="Greetings" VALUE="Hello Friend, How are you?">
</APPLET>
</body>
</HTML>
Applet Program Accepting Parameters
14
//HelloAppletMsg.java
import java.applet.Applet;
import java.awt.*;
public class HelloAppletMsg extends Applet {
String msg;
public void init()
{
msg = getParameter("Greetings");
if( msg == null)
msg = "Hello";
}
public void paint(Graphics g) {
g.drawString (msg,10, 100);
}
} This is name of parameter specified in PARAM tag;
This method returns the value of parameter.
Output
15
Interactive Applet Program
16
import java.applet.Applet;
import java.awt.*;
public class SumNumsInteractive extends
Applet {
TextField text1, text2;
public void init()
{
text1 = new TextField(10);
text2 = new TextField(10);
text1.setText("0");
text2.setText("0");
add(text1);
add(text2);
}
public void paint(Graphics g) {
int num1 = 0;
int num2 = 0;
int sum;
String s1, s2, s3;
g.drawString("Input a number in each
box ", 10, 50);
try {
s1 = text1.getText();
num1 = Integer.parseInt(s1);
s2 = text2.getText();
num2 = Integer.parseInt(s2);
}
catch(Exception e1)
{}
Displaying Numeric Value
17
//SumNums.java
import java.applet.Applet;
import java.awt.*;
public class SumNums extends Applet {
public void paint(Graphics g) {
int num1 = 10;
int num2 = 20;
int sum = num1 + num2;
String str = "Sum: "+String.valueOf(sum);
g.drawString (str,100, 125);
}
}
References:
18
https://www.geeksforgeeks.org/
https://www.javatpoint.com/exception-handling-in-java
https://www.tutorialspoint.com/java/java_exceptions.htm
The complete reference, eleventh edition, available at:
https://gfgc.kar.nic.in/sirmv-science/GenericDocHandler/138-a2973dc6-c024
-4d81-be6d-5c3344f232ce.pdf
19
Thank you

More Related Content

PPT
Basic of Applet
PPTX
Applet in java new
PPT
Slide8appletv2 091028110313-phpapp01
PPT
Java: Java Applets
PDF
27 applet programming
PPTX
PPTX
Java Applet presentation............pptx
PPTX
Applet progming
Basic of Applet
Applet in java new
Slide8appletv2 091028110313-phpapp01
Java: Java Applets
27 applet programming
Java Applet presentation............pptx
Applet progming

Similar to Applets in Java. Learn java program with applets (20)

PDF
Lecture 22
PPTX
Applets in Java
PPTX
Appletjava
PPTX
oops with java modules iii & iv.pptx
PDF
Smart material - Unit 3 (2).pdf
PDF
Smart material - Unit 3 (1).pdf
PPT
Unit 7 Java
PPT
Applets
PPT
Applets
PDF
Java applet basics
PDF
Advanced programming chapter 2 - Java Applet.pdf
PPTX
Introduction To Applets methods and simple examples
PPT
171-33B - Java Programming-Building Applets.ppt
PPTX
Applet.pptx
PPTX
PPTX
Applet1 (1).pptx
PPTX
Java applet
PPTX
Java applet - java
PPT
Graphics programming in Java
Lecture 22
Applets in Java
Appletjava
oops with java modules iii & iv.pptx
Smart material - Unit 3 (2).pdf
Smart material - Unit 3 (1).pdf
Unit 7 Java
Applets
Applets
Java applet basics
Advanced programming chapter 2 - Java Applet.pdf
Introduction To Applets methods and simple examples
171-33B - Java Programming-Building Applets.ppt
Applet.pptx
Applet1 (1).pptx
Java applet
Java applet - java
Graphics programming in Java
Ad

Recently uploaded (20)

PDF
AI Lect 2 Identifying AI systems, branches of AI, etc.pdf
PPTX
Extract Transformation Load (3) (1).pptx
PDF
Company Profile 2023 PT. ZEKON INDONESIA.pdf
PPTX
Purple and Violet Modern Marketing Presentation (1).pptx
PPTX
Data-Driven-Credit-Card-Launch-A-Wells-Fargo-Case-Study.pptx
PPTX
Measurement of Afordability for Water Supply and Sanitation in Bangladesh .pptx
PDF
Digital Infrastructure – Powering the Connected Age
PPTX
Bharatiya Antariksh Hackathon 2025 Idea Submission PPT.pptx
PDF
A Systems Thinking Approach to Algorithmic Fairness.pdf
PPTX
PPT_Dream_45_NEET_Organic_Chemistry_Pankaj_Sijariya_Sir_Sanjeet.pptx
PDF
Mastering Financial Analysis Materials.pdf
PDF
Chad Readey - An Independent Thinker
PPTX
batch data Retailer Data management Project.pptx
PPTX
1intro to AI.pptx AI components & composition
PDF
Nashik East side PPT 01-08-25. vvvhvjvvvhvh
PPTX
artificial intelligence deeplearning-200712115616.pptx
PPT
Chapter 3 METAL JOINING.pptnnnnnnnnnnnnn
PPTX
Understanding Prototyping in Design and Development
PPTX
Challenges and opportunities in feeding a growing population
PPTX
Economic Sector Performance Recovery.pptx
AI Lect 2 Identifying AI systems, branches of AI, etc.pdf
Extract Transformation Load (3) (1).pptx
Company Profile 2023 PT. ZEKON INDONESIA.pdf
Purple and Violet Modern Marketing Presentation (1).pptx
Data-Driven-Credit-Card-Launch-A-Wells-Fargo-Case-Study.pptx
Measurement of Afordability for Water Supply and Sanitation in Bangladesh .pptx
Digital Infrastructure – Powering the Connected Age
Bharatiya Antariksh Hackathon 2025 Idea Submission PPT.pptx
A Systems Thinking Approach to Algorithmic Fairness.pdf
PPT_Dream_45_NEET_Organic_Chemistry_Pankaj_Sijariya_Sir_Sanjeet.pptx
Mastering Financial Analysis Materials.pdf
Chad Readey - An Independent Thinker
batch data Retailer Data management Project.pptx
1intro to AI.pptx AI components & composition
Nashik East side PPT 01-08-25. vvvhvjvvvhvh
artificial intelligence deeplearning-200712115616.pptx
Chapter 3 METAL JOINING.pptnnnnnnnnnnnnn
Understanding Prototyping in Design and Development
Challenges and opportunities in feeding a growing population
Economic Sector Performance Recovery.pptx
Ad

Applets in Java. Learn java program with applets

  • 1. DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Subject Name: Java Programming Topics Covered: Java Applets 1 Name of the School: School of Computer Science and Engineering Course Code: E1UC307C Course Name:Java Programming Faculty Name: Programe Name:
  • 2. Types of Java Application 2 1. Stand alone application(Java application) 2. Web Application( Applet & JSP/Servlet) Web applications are the applications users can use through a browser. Standalone applications are known as Desktop/ Offline applications and can be accessed offline. Java applications  Run in stand-alone mode  No additional software required (such as a Web browser) Java applets  Compiled Java class files  Run within a Web browser (or an appletviewer)  Loaded from anywhere on the Internet
  • 3. Applet 3 An Applet in Java is a small application that runs within a web browser or an applet viewer. Applets are written in Java and are typically used to provide interactive features in a web page, such as animations, games, or interactive forms. A main() method is not invoked on an applet, and an applet class will not define main(). Applets are designed to be embedded within an HTML page. Here is a basic example of how to create an applet in Java: Steps to Create a Java Applet: 1.Import the necessary packages. 2.Extend the Applet class. 3.Override the init(), start(), stop(), and destroy() methods as needed. 4.Use the paint() method to draw content on the applet window. 5.Create an HTML file to embed the applet in a webpage (for older browsers).
  • 4. Life Cycle Methods of Applet 4 Following are the methods for a full applet cycle.  init() method  start() method  paint() method  stop() method  destroy() method
  • 5. Life Cycle of Applet 5 Applet Life Cycle Born Running Idle Dead Begin init() start() paint() stop() start() destroy() End In the life-cycle of execution, the applet exists (lives) in one of these 4 states. There are methods that can changes the state of the applet. All of these methods have a name and they are called as callback methods. These methods are named so because they are called automatically by the browser when required for smooth execution of the applet. Here, programmers write the above-mentioned methods with some code but never calls.
  • 6. Life Cycle steps 6 Four methods in the Applet class give you the framework on which you build any applet:  init: This method is intended for whatever initialization is needed for your applet. It is called after the param tags inside the applet tag have been processed.  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.  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.  destroy: This method is only called when the browser shuts down normally. Because applets are meant to live on an HTML page, you should not normally leave resources behind after a user leaves the page that contains the applet.  paint: Invoked immediately after the start() method, and also any time the applet needs to repaint itself in the browser. The paint() method is actually inherited from the java.awt.
  • 7. 7 init() start() stop() destroy() paint do other work  init and destroy are only called once each  start and stop are called whenever the browser enters and leaves the page  do some work is code called by your listeners  paint is called again when the applet needs to be repainted
  • 8. Applet Program 8 import java.applet.Applet; import java.awt.Graphics; public class HelloWorld extends Applet { public void paint(Graphics g) { g.drawString("Hello World!", 50, 25); } } Compile this file HelloWorld.java and mention the path in HTML file Now you have to create an HTML File that Includes the Applet. Using a text editor, create a file named Hello.html in the same directory that contains HelloWorld.class. This HTML file should contain the following text:
  • 9. Embedding Applet in Web page 9 <HTML> <HEAD> <TITLE> Hello World Applet </TITLE> </HEAD> <body> <h1>Hi, This is My First Java Applet on the Web!</h1> <APPLET CODE="HelloWorld.class" width=500 height=400></APPLET> </body> </HTML>
  • 10. Compile and Run an Applet 10 To compile: javac HelloWorld.java  Generates HelloWorld.class To run: a) Use the appletviewer from JDK open command prompt and reach to folder C:desktop> appletviewer index.html b) Open page from browser: index.html
  • 11. Hello World.java 11 import java.applet.Applet; import java.awt.*; public class GraphicsDemo extends Applet{ public void paint(Graphics g){ g.setColor(Color.red); g.drawString("Welcome",50, 50); g.drawLine(20,30,20,300); g.drawRect(70,100,30,30); g.fillRect(170,100,30,30); g.drawOval(70,200,30,30); g.setColor(Color.pink); g.fillOval(170,200,30,30); g.drawArc(90,150,30,30,30,270); g.fillArc(270,150,30,30,0,180); } }
  • 12. Displaying Image in Applet 12 import java.awt.*; import java.applet.*; public class DisplayImage extends Applet { Image picture; public void init() { picture = getImage(getDocumentBase(),“abc.jpg"); } public void paint(Graphics g) { g.drawImage(picture, 30,30, this); } }
  • 13. Passing Parameters to Applet 13 <HTML> <HEAD><TITLE> Hello World Applet</TITLE></HEAD> <body> <h1>Hi, This is My First Communicating Applet on the Web!</h1> <APPLET CODE="HelloAppletMsg.class" width=500 height=400> <PARAM NAME="Greetings" VALUE="Hello Friend, How are you?"> </APPLET> </body> </HTML>
  • 14. Applet Program Accepting Parameters 14 //HelloAppletMsg.java import java.applet.Applet; import java.awt.*; public class HelloAppletMsg extends Applet { String msg; public void init() { msg = getParameter("Greetings"); if( msg == null) msg = "Hello"; } public void paint(Graphics g) { g.drawString (msg,10, 100); } } This is name of parameter specified in PARAM tag; This method returns the value of parameter.
  • 16. Interactive Applet Program 16 import java.applet.Applet; import java.awt.*; public class SumNumsInteractive extends Applet { TextField text1, text2; public void init() { text1 = new TextField(10); text2 = new TextField(10); text1.setText("0"); text2.setText("0"); add(text1); add(text2); } public void paint(Graphics g) { int num1 = 0; int num2 = 0; int sum; String s1, s2, s3; g.drawString("Input a number in each box ", 10, 50); try { s1 = text1.getText(); num1 = Integer.parseInt(s1); s2 = text2.getText(); num2 = Integer.parseInt(s2); } catch(Exception e1) {}
  • 17. Displaying Numeric Value 17 //SumNums.java import java.applet.Applet; import java.awt.*; public class SumNums extends Applet { public void paint(Graphics g) { int num1 = 10; int num2 = 20; int sum = num1 + num2; String str = "Sum: "+String.valueOf(sum); g.drawString (str,100, 125); } }