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

PPTX
MSBTE Computer Engineering Java applet.pptx
PPTX
Introduction To Applets methods and simple examples
PDF
27 applet programming
PPT
Slide8appletv2 091028110313-phpapp01
PPTX
Applet1 (1).pptx
PPTX
applet.pptx
PPT
Advanced Programming, Java Programming, Applets.ppt
PPTX
Appletjava
MSBTE Computer Engineering Java applet.pptx
Introduction To Applets methods and simple examples
27 applet programming
Slide8appletv2 091028110313-phpapp01
Applet1 (1).pptx
applet.pptx
Advanced Programming, Java Programming, Applets.ppt
Appletjava

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

PPTX
Applet.pptx
PPT
Java: Java Applets
PPTX
Applet progming
PPT
Applets
PPTX
Applets in Java
PPTX
PPT
PPT
Applets
PPT
Jsp applet
PPTX
Applet in java new
PPT
Applet and graphics programming
PPTX
Applets
PPT
Basic of Applet
PPTX
Java applet
PPT
Applet ppt for higher understanding education
PPTX
Applet intro
PPSX
Java Applet
PPT
JAVA APPLET BASICS
PPTX
3. applets
Applet.pptx
Java: Java Applets
Applet progming
Applets
Applets in Java
Applets
Jsp applet
Applet in java new
Applet and graphics programming
Applets
Basic of Applet
Java applet
Applet ppt for higher understanding education
Applet intro
Java Applet
JAVA APPLET BASICS
3. applets
Ad

Recently uploaded (20)

PPTX
IBA_Chapter_11_Slides_Final_Accessible.pptx
PDF
Business Analytics and business intelligence.pdf
PDF
Galatica Smart Energy Infrastructure Startup Pitch Deck
PDF
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
PPTX
iec ppt-1 pptx icmr ppt on rehabilitation.pptx
PPTX
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx
PPTX
AI Strategy room jwfjksfksfjsjsjsjsjfsjfsj
PPTX
Acceptance and paychological effects of mandatory extra coach I classes.pptx
PPTX
Supervised vs unsupervised machine learning algorithms
PPTX
01_intro xxxxxxxxxxfffffffffffaaaaaaaaaaafg
PDF
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
PDF
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
PPTX
IB Computer Science - Internal Assessment.pptx
PPTX
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
PPTX
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
PDF
annual-report-2024-2025 original latest.
PPTX
Qualitative Qantitative and Mixed Methods.pptx
PPTX
climate analysis of Dhaka ,Banglades.pptx
PDF
TRAFFIC-MANAGEMENT-AND-ACCIDENT-INVESTIGATION-WITH-DRIVING-PDF-FILE.pdf
IBA_Chapter_11_Slides_Final_Accessible.pptx
Business Analytics and business intelligence.pdf
Galatica Smart Energy Infrastructure Startup Pitch Deck
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
iec ppt-1 pptx icmr ppt on rehabilitation.pptx
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx
AI Strategy room jwfjksfksfjsjsjsjsjfsjfsj
Acceptance and paychological effects of mandatory extra coach I classes.pptx
Supervised vs unsupervised machine learning algorithms
01_intro xxxxxxxxxxfffffffffffaaaaaaaaaaafg
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
IB Computer Science - Internal Assessment.pptx
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
annual-report-2024-2025 original latest.
Qualitative Qantitative and Mixed Methods.pptx
climate analysis of Dhaka ,Banglades.pptx
TRAFFIC-MANAGEMENT-AND-ACCIDENT-INVESTIGATION-WITH-DRIVING-PDF-FILE.pdf
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); } }