SlideShare a Scribd company logo
APPLET IN JAVA
JAVA PROGRAMMING
Presented by
Mrs.N.Kavitha,
Department of Computer Science.
Applet in Java
An applet is a special kind of Java program that runs in a Java enabled browser.
This is the first Java program that can run over the network using the browser.
Applet is typically embedded inside a web page and runs in the browser.
 Applets are small Java applications that can be accessed on an Internet server,
transported over Internet, and can be automatically installed and run as apart of a
web document.
 The applet can produce a graphical user interface.
To create an applet, a class must extends java.applet.Applet class.
An Applet class does not have any main() method.
It is viewed using JVM. The JVM can use either a plug-in of the Web browser or a
separate runtime environment to run an applet application.
JVM creates an instance of the applet class and invokes init() method to initialize
an Applet.
Lifecycle of Java Applet
The following are the stages in Applet
1.Applet is initialized.
2.Applet is started
3.Applet is painted.
4.Applet is stopped.
5.Applet is destroyed.
Applet in java new
A Simple Applet
import java.awt.*;
import java.applet.*;
public class Simple extends Applet
{
public void paint(Graphics g)
{
g.drawString("A simple Applet", 20, 20);
}
}
Every Applet application must import two packages - java.awt and java.applet.
java.awt.* imports the Abstract Window Toolkit (AWT) classes.
Applets interact with the user (either directly or indirectly) through the AWT.
The AWT contains support for a window-based, graphical user interface. java.applet.* imports
the applet package, which contains the class Applet.
 Every applet that you create must be a subclass of Applet class.
The class in the program must be declared as public, because it will be accessed by code that is
outside the program.
Every Applet application must declare a paint() method.
This method is defined by AWT class and must be overridden by the applet.
The paint() method is called each time when an applet needs to redisplay its output.
The execution of an applet does not begin at main() method.
In fact an applet application does not have any main() method.
Advantages of Applets
1.It takes very less response time as it works on the client side.
2.It can be run on any browser which has JVM running in it.
Applet class
Applet class provides all necessary support for applet execution, such as initializing and
destroying of applet.
It also provide methods that load and display images and methods that load and play audio
clips.
An Applet Skeleton
Most applets override these four methods. These four methods forms Applet lifecycle.
init() : init() is the first method to be called. This is where variable are initialized. This
method is called only once during the runtime of applet.
start() : start() method is called after init(). This method is called to restart an applet after it
has been stopped.
stop() : stop() method is called to suspend thread that does not need to run when applet is not
visible.
destroy() : destroy() method is called when your applet needs to be removed completely from
memory.
Example of an Applet Skeleton
import java.awt.*;
import java.applet.*;
public class AppletTest extends Applet
{
public void init()
{
//initialization
}
public void start ()
{ //start or resume execution
}
public void stop()
{ //suspend execution }
public void destroy() {
//perform shutdown activity
}
public void paint (Graphics g)
{ //display the content of window
}
}
Example of an Applet
import java.applet.*;
import java.awt.*;
public class MyApplet extends Applet
{
int height, width;
public void init()
{
height = getSize().height;
width = getSize().width;
setName("MyApplet");
}
public void paint(Graphics g)
{
g.drawRoundRect(10, 30, 120, 120, 2, 3);
}
}
Parameter in Applet
User-define Parameter can be applied in applet
using <PARAM…> tags. Each <PARAM…> tag has a
name and value attribute.
Example:
name = color Value = red
Syntax:
<PARAM name = ……… Value = “………” >
In an applet code, applet can refer to a parameter by its name and then
find its value.
The two most important thing to handle and set up the parameter is the
<PARAM> tag in the HTML document and an applet code to parse this
parameter.
init() method is used to get hold of the parameters which is defined in the
<PARAM> tags. And getParameter() method is used for getting the
parameters.
In Applet, Parameters are passed on applet when it is loaded.
Example:
param.java
import java.applet.*;
import java.awt.*;
public class param extends Applet
{
String str;
public void init()
{
str=getParameter("pname");
if (str == null)
str = "Welcome to New World";
str = "Hello " + str;
}
public void paint(Graphics g)
{
g.drawString(str, 200, 200);
How to run an Applet Program
An Applet program is compiled in the same way as you have been compiling
your console programs.
However there are two ways to run an applet.
Executing the Applet within Java-compatible web browser.
Using an Applet viewer, such as the standard tool, applet viewer.
 An applet viewer executes your applet in a window
For executing an Applet in an web browser, create short HTML file in the
same directory.
Inside body tag of the file, include the following code. (applet tag loads the
Applet class)
< applet code = "MyApplet" width=400 height=400 > < /applet >
Sr No. Methods Description
1 public abstract void drawString(String str, int x, int y) Used to draw specified string.
2 public void drawRect(int x, int y, int width, int height) Used to draw a rectangle of specified width and height.
3 public abstract void fillRect(int x, int y, int width, int height) Used to draw a rectangle with a default colourof
specified width and height.
4 public abstract void drawOval(int x, int y, int width, int height) Used to draw oval of specified width and height.
5 public abstract void fillOval(int x, int y, int width, int height) Used to draw oval with a default colour of specified
width and height.
6 public abstract void drawLine(int x1, int y1, int x2, int y2) Used for drawing lines between the point (x1, x1) and
(x2, y2).
7 public abstract booleandrawImage(Image img, int x, int y, ImageObserver observer) Used for drawing a specified image.
8 public abstract void drawArc(int x, int y, int width, int height, intstartAngle, intarcAngle) Used for drawing a circular arc.
9 public abstract void fillArc(int x, int y, int width, int height, intstartAngle, intarcAngle) Used for filling circular arc.
10 public abstract void setColor(Color c) Used to set a colour to the object.
11 public abstract void setFont(Font font) Used to set font.
Example:
GraphicsDemo1.java
import java.applet.Applet;
import java.awt.*;
public class GraphicsDemo1 extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.black);
g.drawString("Welcome to Computer Science",50, 50);
g.setColor(Color.blue);
g.fillOval(170,200,30,30);
g.drawArc(90,150,30,30,30,270);
g.fillArc(270,150,30,30,0,180);
g.drawLine(21,31,20,300);
g.drawRect(70,100,30,30);
g.fillRect(170,100,30,30);
g.drawOval(70,200,30,30);
}
}
GraphicsDemo1.html
<html>
<body>
<applet code="GraphicsDemo1.class" width="300"
height="300">
</applet>
</body>
</html>
Working with Images in Applet
In Applet programs, images also can be used
java.awt.Image class is used for representing an image.
java.applet, java.awt and java.awt.image are the packages which are used for event
handling.
Loading an image
In Applet, images are loaded using getImage() method.
 This method works when the constructor of the Applet is called.
It is always suggested to call the constructor in init() method.
Here are some examples:
Image image1 = getImage(getCodeBase(), "image1.gif");
Image image2 = getImage(getDocumentBase(), "image1.jpeg");
Image image3 = getImage(new URL("http://java.sun.com/graphics/image.gif"));
Displaying an image
In Applet, images are displayed using drawImage() method.
This method is supplied by the Graphics object, which is passed to paint() method.
• Aimage.java
import java.awt.*;
import java.applet.*;
public class Aimage extends Applet
{
Image img1;
public void init()
{
img1=getImage(getDocumentBase(),"icon.png");
}
public void paint(Graphics g)
{
g.drawImage(img1,100,100,this);
}
}
Aimage.html
<html>
<body>
<applet code=Aimage height=300 width=300>
</applet>
</body>
</html>

More Related Content

DOCX
Methods in Java
PPTX
Interface in java
PPTX
Multithreading in java
PPT
Java adapter
PPT
Unit 7 Java
PPTX
Interfaces in JAVA !! why??
PPTX
Java interface
PPT
Java interfaces
Methods in Java
Interface in java
Multithreading in java
Java adapter
Unit 7 Java
Interfaces in JAVA !! why??
Java interface
Java interfaces

What's hot (20)

PPS
Interface
PPTX
PPTX
Interpreter Design Pattern in Javascript
PPTX
Polymorphism presentation in java
PPTX
Interfaces and abstract classes
PPTX
JAVA AWT
PDF
Towards Improving Interface Modularity in Legacy Java Software Through Automa...
DOC
Cs2312 OOPS LAB MANUAL
PDF
Automated Refactoring of Legacy Java Software to Default Methods Talk at ICSE...
PPTX
Multithreading in java
PPTX
Applets in java
PPTX
Chapter 2.4
PPT
02basics
PDF
Object Oriented Programming Lab Manual
PPTX
Introduction to java Programming
PPT
JAVA APPLET BASICS
PPT
Unit 8 Java
PDF
Java lab1 manual
PPT
Applets
PDF
java-06inheritance
Interface
Interpreter Design Pattern in Javascript
Polymorphism presentation in java
Interfaces and abstract classes
JAVA AWT
Towards Improving Interface Modularity in Legacy Java Software Through Automa...
Cs2312 OOPS LAB MANUAL
Automated Refactoring of Legacy Java Software to Default Methods Talk at ICSE...
Multithreading in java
Applets in java
Chapter 2.4
02basics
Object Oriented Programming Lab Manual
Introduction to java Programming
JAVA APPLET BASICS
Unit 8 Java
Java lab1 manual
Applets
java-06inheritance
Ad

Similar to Applet in java new (20)

PPTX
Java Applet presentation............pptx
PPT
Basic of Applet
PPTX
Applets in Java. Learn java program with applets
PPTX
Applet progming
PDF
27 applet programming
PDF
Smart material - Unit 3 (2).pdf
PDF
Smart material - Unit 3 (1).pdf
PPTX
oops with java modules iii & iv.pptx
PPTX
Java chapter 7
PPT
Graphics programming in Java
PPTX
Java applet - java
PPTX
PPTX
Java applet
PPT
Java: Java Applets
PPT
Slide8appletv2 091028110313-phpapp01
PPTX
APPLET.pptx
PPT
Creating applets.60
PPT
Applet ppt for higher understanding education
PPTX
Appletjava
Java Applet presentation............pptx
Basic of Applet
Applets in Java. Learn java program with applets
Applet progming
27 applet programming
Smart material - Unit 3 (2).pdf
Smart material - Unit 3 (1).pdf
oops with java modules iii & iv.pptx
Java chapter 7
Graphics programming in Java
Java applet - java
Java applet
Java: Java Applets
Slide8appletv2 091028110313-phpapp01
APPLET.pptx
Creating applets.60
Applet ppt for higher understanding education
Appletjava
Ad

More from Kavitha713564 (13)

PPTX
Python Virtual Machine concept- N.Kavitha.pptx
PPTX
Operators Concept in Python-N.Kavitha.pptx
PPTX
THE PACKAGES CONCEPT IN JAVA PROGRAMMING.pptx
PPTX
The Java Server Page in Java Concept.pptx
PPTX
Programming in python in detail concept .pptx
PPTX
The Input Statement in Core Python .pptx
PPTX
The Datatypes Concept in Core Python.pptx
PPTX
Packages in java
PPTX
Exception handling in java
PPTX
Multithreading in java
PPTX
Basic of java
PPTX
Input output files in java
PPTX
Arrays,string and vector
Python Virtual Machine concept- N.Kavitha.pptx
Operators Concept in Python-N.Kavitha.pptx
THE PACKAGES CONCEPT IN JAVA PROGRAMMING.pptx
The Java Server Page in Java Concept.pptx
Programming in python in detail concept .pptx
The Input Statement in Core Python .pptx
The Datatypes Concept in Core Python.pptx
Packages in java
Exception handling in java
Multithreading in java
Basic of java
Input output files in java
Arrays,string and vector

Recently uploaded (20)

PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Pharma ospi slides which help in ospi learning
PDF
01-Introduction-to-Information-Management.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
master seminar digital applications in india
PDF
Pre independence Education in Inndia.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PDF
RMMM.pdf make it easy to upload and study
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Cell Structure & Organelles in detailed.
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
Cell Types and Its function , kingdom of life
O5-L3 Freight Transport Ops (International) V1.pdf
O7-L3 Supply Chain Operations - ICLT Program
Pharma ospi slides which help in ospi learning
01-Introduction-to-Information-Management.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Module 4: Burden of Disease Tutorial Slides S2 2025
FourierSeries-QuestionsWithAnswers(Part-A).pdf
master seminar digital applications in india
Pre independence Education in Inndia.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
VCE English Exam - Section C Student Revision Booklet
Anesthesia in Laparoscopic Surgery in India
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
RMMM.pdf make it easy to upload and study
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Cell Structure & Organelles in detailed.
Abdominal Access Techniques with Prof. Dr. R K Mishra
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Cell Types and Its function , kingdom of life

Applet in java new

  • 1. APPLET IN JAVA JAVA PROGRAMMING Presented by Mrs.N.Kavitha, Department of Computer Science.
  • 2. Applet in Java An applet is a special kind of Java program that runs in a Java enabled browser. This is the first Java program that can run over the network using the browser. Applet is typically embedded inside a web page and runs in the browser.  Applets are small Java applications that can be accessed on an Internet server, transported over Internet, and can be automatically installed and run as apart of a web document.  The applet can produce a graphical user interface. To create an applet, a class must extends java.applet.Applet class. An Applet class does not have any main() method. It is viewed using JVM. The JVM can use either a plug-in of the Web browser or a separate runtime environment to run an applet application. JVM creates an instance of the applet class and invokes init() method to initialize an Applet.
  • 3. Lifecycle of Java Applet The following are the stages in Applet 1.Applet is initialized. 2.Applet is started 3.Applet is painted. 4.Applet is stopped. 5.Applet is destroyed.
  • 5. A Simple Applet import java.awt.*; import java.applet.*; public class Simple extends Applet { public void paint(Graphics g) { g.drawString("A simple Applet", 20, 20); } }
  • 6. Every Applet application must import two packages - java.awt and java.applet. java.awt.* imports the Abstract Window Toolkit (AWT) classes. Applets interact with the user (either directly or indirectly) through the AWT. The AWT contains support for a window-based, graphical user interface. java.applet.* imports the applet package, which contains the class Applet.  Every applet that you create must be a subclass of Applet class. The class in the program must be declared as public, because it will be accessed by code that is outside the program. Every Applet application must declare a paint() method. This method is defined by AWT class and must be overridden by the applet. The paint() method is called each time when an applet needs to redisplay its output. The execution of an applet does not begin at main() method. In fact an applet application does not have any main() method.
  • 7. Advantages of Applets 1.It takes very less response time as it works on the client side. 2.It can be run on any browser which has JVM running in it. Applet class Applet class provides all necessary support for applet execution, such as initializing and destroying of applet. It also provide methods that load and display images and methods that load and play audio clips. An Applet Skeleton Most applets override these four methods. These four methods forms Applet lifecycle. init() : init() is the first method to be called. This is where variable are initialized. This method is called only once during the runtime of applet. start() : start() method is called after init(). This method is called to restart an applet after it has been stopped. stop() : stop() method is called to suspend thread that does not need to run when applet is not visible. destroy() : destroy() method is called when your applet needs to be removed completely from memory.
  • 8. Example of an Applet Skeleton import java.awt.*; import java.applet.*; public class AppletTest extends Applet { public void init() { //initialization } public void start () { //start or resume execution } public void stop() { //suspend execution } public void destroy() { //perform shutdown activity } public void paint (Graphics g) { //display the content of window } }
  • 9. Example of an Applet import java.applet.*; import java.awt.*; public class MyApplet extends Applet { int height, width; public void init() { height = getSize().height; width = getSize().width; setName("MyApplet"); } public void paint(Graphics g) { g.drawRoundRect(10, 30, 120, 120, 2, 3); } }
  • 10. Parameter in Applet User-define Parameter can be applied in applet using <PARAM…> tags. Each <PARAM…> tag has a name and value attribute. Example: name = color Value = red Syntax: <PARAM name = ……… Value = “………” >
  • 11. In an applet code, applet can refer to a parameter by its name and then find its value. The two most important thing to handle and set up the parameter is the <PARAM> tag in the HTML document and an applet code to parse this parameter. init() method is used to get hold of the parameters which is defined in the <PARAM> tags. And getParameter() method is used for getting the parameters. In Applet, Parameters are passed on applet when it is loaded.
  • 12. Example: param.java import java.applet.*; import java.awt.*; public class param extends Applet { String str; public void init() { str=getParameter("pname"); if (str == null) str = "Welcome to New World"; str = "Hello " + str; } public void paint(Graphics g) { g.drawString(str, 200, 200);
  • 13. How to run an Applet Program An Applet program is compiled in the same way as you have been compiling your console programs. However there are two ways to run an applet. Executing the Applet within Java-compatible web browser. Using an Applet viewer, such as the standard tool, applet viewer.  An applet viewer executes your applet in a window For executing an Applet in an web browser, create short HTML file in the same directory. Inside body tag of the file, include the following code. (applet tag loads the Applet class) < applet code = "MyApplet" width=400 height=400 > < /applet >
  • 14. Sr No. Methods Description 1 public abstract void drawString(String str, int x, int y) Used to draw specified string. 2 public void drawRect(int x, int y, int width, int height) Used to draw a rectangle of specified width and height. 3 public abstract void fillRect(int x, int y, int width, int height) Used to draw a rectangle with a default colourof specified width and height. 4 public abstract void drawOval(int x, int y, int width, int height) Used to draw oval of specified width and height. 5 public abstract void fillOval(int x, int y, int width, int height) Used to draw oval with a default colour of specified width and height. 6 public abstract void drawLine(int x1, int y1, int x2, int y2) Used for drawing lines between the point (x1, x1) and (x2, y2). 7 public abstract booleandrawImage(Image img, int x, int y, ImageObserver observer) Used for drawing a specified image. 8 public abstract void drawArc(int x, int y, int width, int height, intstartAngle, intarcAngle) Used for drawing a circular arc. 9 public abstract void fillArc(int x, int y, int width, int height, intstartAngle, intarcAngle) Used for filling circular arc. 10 public abstract void setColor(Color c) Used to set a colour to the object. 11 public abstract void setFont(Font font) Used to set font.
  • 15. Example: GraphicsDemo1.java import java.applet.Applet; import java.awt.*; public class GraphicsDemo1 extends Applet { public void paint(Graphics g) { g.setColor(Color.black); g.drawString("Welcome to Computer Science",50, 50); g.setColor(Color.blue); g.fillOval(170,200,30,30); g.drawArc(90,150,30,30,30,270); g.fillArc(270,150,30,30,0,180); g.drawLine(21,31,20,300); g.drawRect(70,100,30,30); g.fillRect(170,100,30,30); g.drawOval(70,200,30,30); } }
  • 17. Working with Images in Applet In Applet programs, images also can be used java.awt.Image class is used for representing an image. java.applet, java.awt and java.awt.image are the packages which are used for event handling. Loading an image In Applet, images are loaded using getImage() method.  This method works when the constructor of the Applet is called. It is always suggested to call the constructor in init() method. Here are some examples: Image image1 = getImage(getCodeBase(), "image1.gif"); Image image2 = getImage(getDocumentBase(), "image1.jpeg"); Image image3 = getImage(new URL("http://java.sun.com/graphics/image.gif")); Displaying an image In Applet, images are displayed using drawImage() method. This method is supplied by the Graphics object, which is passed to paint() method.
  • 18. • Aimage.java import java.awt.*; import java.applet.*; public class Aimage extends Applet { Image img1; public void init() { img1=getImage(getDocumentBase(),"icon.png"); } public void paint(Graphics g) { g.drawImage(img1,100,100,this); } }
  • 19. Aimage.html <html> <body> <applet code=Aimage height=300 width=300> </applet> </body> </html>