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

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

Applet.pptx
Applet.pptxApplet.pptx
Applet.pptx
LakachewYezihalem
 
Java: Java Applets
Java: Java AppletsJava: Java Applets
Java: Java Applets
Tareq Hasan
 
Applet progming
Applet progmingApplet progming
Applet progming
VIKRANTHMALLIKARJUN
 
Applets
AppletsApplets
Applets
SanthiNivas
 
Applets in Java
Applets in JavaApplets in Java
Applets in Java
RamaPrabha24
 
Applet
AppletApplet
Applet
Priyanka Pradhan
 
Applets
AppletsApplets
Applets
Inayat Sharma
 
Applets
AppletsApplets
Applets
Abhishek Khune
 
Jsp applet
Jsp appletJsp applet
Jsp applet
Sanoj Kumar
 
Applet in java new
Applet in java newApplet in java new
Applet in java new
Kavitha713564
 
Applet and graphics programming
Applet and graphics programmingApplet and graphics programming
Applet and graphics programming
mcanotes
 
Java
JavaJava
Java
janani thirupathi
 
Applets
AppletsApplets
Applets
Nuha Noor
 
Basic of Applet
Basic of AppletBasic of Applet
Basic of Applet
suraj pandey
 
Java applet
Java appletJava applet
Java applet
Elizabeth alexander
 
Applet ppt for higher understanding education
Applet ppt for higher understanding educationApplet ppt for higher understanding education
Applet ppt for higher understanding education
BhanuPriya93439
 
Applet intro
Applet introApplet intro
Applet intro
Nitin Birari
 
Java Applet
Java AppletJava Applet
Java Applet
jalinder123
 
JAVA APPLET BASICS
JAVA APPLET BASICSJAVA APPLET BASICS
JAVA APPLET BASICS
Shanid Malayil
 
3. applets
3. applets3. applets
3. applets
AnusAhmad
 

Recently uploaded (20)

apidays Singapore 2025 - What exactly are AI Agents by Aki Ranin (Earthshots ...
apidays Singapore 2025 - What exactly are AI Agents by Aki Ranin (Earthshots ...apidays Singapore 2025 - What exactly are AI Agents by Aki Ranin (Earthshots ...
apidays Singapore 2025 - What exactly are AI Agents by Aki Ranin (Earthshots ...
apidays
 
Retort Instrumentation laboratory practi
Retort Instrumentation laboratory practiRetort Instrumentation laboratory practi
Retort Instrumentation laboratory practi
ADINDADYAHMUKHLASIN
 
apidays New York 2025 - Building Scalable AI Systems by Sai Prasad Veluru (Ap...
apidays New York 2025 - Building Scalable AI Systems by Sai Prasad Veluru (Ap...apidays New York 2025 - Building Scalable AI Systems by Sai Prasad Veluru (Ap...
apidays New York 2025 - Building Scalable AI Systems by Sai Prasad Veluru (Ap...
apidays
 
Part Departement Head Presentation for Business
Part Departement Head Presentation for BusinessPart Departement Head Presentation for Business
Part Departement Head Presentation for Business
Rizki229625
 
Pause Travail 22 Hostiou Girard 12 juin 2025.pdf
Pause Travail 22 Hostiou Girard 12 juin 2025.pdfPause Travail 22 Hostiou Girard 12 juin 2025.pdf
Pause Travail 22 Hostiou Girard 12 juin 2025.pdf
Institut de l'Elevage - Idele
 
METHODS OF DATA COLLECTION (Research methodology)
METHODS OF DATA COLLECTION (Research methodology)METHODS OF DATA COLLECTION (Research methodology)
METHODS OF DATA COLLECTION (Research methodology)
anwesha248
 
Report_Government Authorities_Index_ENG_FIN.pdf
Report_Government Authorities_Index_ENG_FIN.pdfReport_Government Authorities_Index_ENG_FIN.pdf
Report_Government Authorities_Index_ENG_FIN.pdf
OlhaTatokhina1
 
apidays New York 2025 - Lessons From Two Technical Transformations by Leah Hu...
apidays New York 2025 - Lessons From Two Technical Transformations by Leah Hu...apidays New York 2025 - Lessons From Two Technical Transformations by Leah Hu...
apidays New York 2025 - Lessons From Two Technical Transformations by Leah Hu...
apidays
 
apidays New York 2025 - Two tales of API Change Management by Eric Koleda (Coda)
apidays New York 2025 - Two tales of API Change Management by Eric Koleda (Coda)apidays New York 2025 - Two tales of API Change Management by Eric Koleda (Coda)
apidays New York 2025 - Two tales of API Change Management by Eric Koleda (Coda)
apidays
 
1-2. Lab Introduction to Linux environment.ppt
1-2. Lab Introduction to Linux environment.ppt1-2. Lab Introduction to Linux environment.ppt
1-2. Lab Introduction to Linux environment.ppt
Wahajch
 
apidays Singapore 2025 - 4 Identity Essentials for Scaling SaaS in Large Orgs...
apidays Singapore 2025 - 4 Identity Essentials for Scaling SaaS in Large Orgs...apidays Singapore 2025 - 4 Identity Essentials for Scaling SaaS in Large Orgs...
apidays Singapore 2025 - 4 Identity Essentials for Scaling SaaS in Large Orgs...
apidays
 
BE PROGRAMjwjwjwjsjsjsjsME TEMPLATE.pptx
BE PROGRAMjwjwjwjsjsjsjsME TEMPLATE.pptxBE PROGRAMjwjwjwjsjsjsjsME TEMPLATE.pptx
BE PROGRAMjwjwjwjsjsjsjsME TEMPLATE.pptx
AaronBaluyut
 
What is FinOps as a Service and why is it Trending?
What is FinOps as a Service and why is it Trending?What is FinOps as a Service and why is it Trending?
What is FinOps as a Service and why is it Trending?
Amnic
 
apidays New York 2025 - The Challenge is Not the Pattern, But the Best Integr...
apidays New York 2025 - The Challenge is Not the Pattern, But the Best Integr...apidays New York 2025 - The Challenge is Not the Pattern, But the Best Integr...
apidays New York 2025 - The Challenge is Not the Pattern, But the Best Integr...
apidays
 
apidays New York 2025 - Fast, Repeatable, Secure: Pick 3 with FINOS CCC by Le...
apidays New York 2025 - Fast, Repeatable, Secure: Pick 3 with FINOS CCC by Le...apidays New York 2025 - Fast, Repeatable, Secure: Pick 3 with FINOS CCC by Le...
apidays New York 2025 - Fast, Repeatable, Secure: Pick 3 with FINOS CCC by Le...
apidays
 
apidays New York 2025 - The FINOS Common Domain Model for Capital Markets by ...
apidays New York 2025 - The FINOS Common Domain Model for Capital Markets by ...apidays New York 2025 - The FINOS Common Domain Model for Capital Markets by ...
apidays New York 2025 - The FINOS Common Domain Model for Capital Markets by ...
apidays
 
apidays New York 2025 - The Future of Small Business Lending with Open Bankin...
apidays New York 2025 - The Future of Small Business Lending with Open Bankin...apidays New York 2025 - The Future of Small Business Lending with Open Bankin...
apidays New York 2025 - The Future of Small Business Lending with Open Bankin...
apidays
 
Advanced_English_Pronunciation_in_Use.pdf
Advanced_English_Pronunciation_in_Use.pdfAdvanced_English_Pronunciation_in_Use.pdf
Advanced_English_Pronunciation_in_Use.pdf
leogoemmanguyenthao
 
apidays Singapore 2025 - Enhancing Developer Productivity with UX (Government...
apidays Singapore 2025 - Enhancing Developer Productivity with UX (Government...apidays Singapore 2025 - Enhancing Developer Productivity with UX (Government...
apidays Singapore 2025 - Enhancing Developer Productivity with UX (Government...
apidays
 
apidays New York 2025 - Boost API Development Velocity with Practical AI Tool...
apidays New York 2025 - Boost API Development Velocity with Practical AI Tool...apidays New York 2025 - Boost API Development Velocity with Practical AI Tool...
apidays New York 2025 - Boost API Development Velocity with Practical AI Tool...
apidays
 
apidays Singapore 2025 - What exactly are AI Agents by Aki Ranin (Earthshots ...
apidays Singapore 2025 - What exactly are AI Agents by Aki Ranin (Earthshots ...apidays Singapore 2025 - What exactly are AI Agents by Aki Ranin (Earthshots ...
apidays Singapore 2025 - What exactly are AI Agents by Aki Ranin (Earthshots ...
apidays
 
Retort Instrumentation laboratory practi
Retort Instrumentation laboratory practiRetort Instrumentation laboratory practi
Retort Instrumentation laboratory practi
ADINDADYAHMUKHLASIN
 
apidays New York 2025 - Building Scalable AI Systems by Sai Prasad Veluru (Ap...
apidays New York 2025 - Building Scalable AI Systems by Sai Prasad Veluru (Ap...apidays New York 2025 - Building Scalable AI Systems by Sai Prasad Veluru (Ap...
apidays New York 2025 - Building Scalable AI Systems by Sai Prasad Veluru (Ap...
apidays
 
Part Departement Head Presentation for Business
Part Departement Head Presentation for BusinessPart Departement Head Presentation for Business
Part Departement Head Presentation for Business
Rizki229625
 
METHODS OF DATA COLLECTION (Research methodology)
METHODS OF DATA COLLECTION (Research methodology)METHODS OF DATA COLLECTION (Research methodology)
METHODS OF DATA COLLECTION (Research methodology)
anwesha248
 
Report_Government Authorities_Index_ENG_FIN.pdf
Report_Government Authorities_Index_ENG_FIN.pdfReport_Government Authorities_Index_ENG_FIN.pdf
Report_Government Authorities_Index_ENG_FIN.pdf
OlhaTatokhina1
 
apidays New York 2025 - Lessons From Two Technical Transformations by Leah Hu...
apidays New York 2025 - Lessons From Two Technical Transformations by Leah Hu...apidays New York 2025 - Lessons From Two Technical Transformations by Leah Hu...
apidays New York 2025 - Lessons From Two Technical Transformations by Leah Hu...
apidays
 
apidays New York 2025 - Two tales of API Change Management by Eric Koleda (Coda)
apidays New York 2025 - Two tales of API Change Management by Eric Koleda (Coda)apidays New York 2025 - Two tales of API Change Management by Eric Koleda (Coda)
apidays New York 2025 - Two tales of API Change Management by Eric Koleda (Coda)
apidays
 
1-2. Lab Introduction to Linux environment.ppt
1-2. Lab Introduction to Linux environment.ppt1-2. Lab Introduction to Linux environment.ppt
1-2. Lab Introduction to Linux environment.ppt
Wahajch
 
apidays Singapore 2025 - 4 Identity Essentials for Scaling SaaS in Large Orgs...
apidays Singapore 2025 - 4 Identity Essentials for Scaling SaaS in Large Orgs...apidays Singapore 2025 - 4 Identity Essentials for Scaling SaaS in Large Orgs...
apidays Singapore 2025 - 4 Identity Essentials for Scaling SaaS in Large Orgs...
apidays
 
BE PROGRAMjwjwjwjsjsjsjsME TEMPLATE.pptx
BE PROGRAMjwjwjwjsjsjsjsME TEMPLATE.pptxBE PROGRAMjwjwjwjsjsjsjsME TEMPLATE.pptx
BE PROGRAMjwjwjwjsjsjsjsME TEMPLATE.pptx
AaronBaluyut
 
What is FinOps as a Service and why is it Trending?
What is FinOps as a Service and why is it Trending?What is FinOps as a Service and why is it Trending?
What is FinOps as a Service and why is it Trending?
Amnic
 
apidays New York 2025 - The Challenge is Not the Pattern, But the Best Integr...
apidays New York 2025 - The Challenge is Not the Pattern, But the Best Integr...apidays New York 2025 - The Challenge is Not the Pattern, But the Best Integr...
apidays New York 2025 - The Challenge is Not the Pattern, But the Best Integr...
apidays
 
apidays New York 2025 - Fast, Repeatable, Secure: Pick 3 with FINOS CCC by Le...
apidays New York 2025 - Fast, Repeatable, Secure: Pick 3 with FINOS CCC by Le...apidays New York 2025 - Fast, Repeatable, Secure: Pick 3 with FINOS CCC by Le...
apidays New York 2025 - Fast, Repeatable, Secure: Pick 3 with FINOS CCC by Le...
apidays
 
apidays New York 2025 - The FINOS Common Domain Model for Capital Markets by ...
apidays New York 2025 - The FINOS Common Domain Model for Capital Markets by ...apidays New York 2025 - The FINOS Common Domain Model for Capital Markets by ...
apidays New York 2025 - The FINOS Common Domain Model for Capital Markets by ...
apidays
 
apidays New York 2025 - The Future of Small Business Lending with Open Bankin...
apidays New York 2025 - The Future of Small Business Lending with Open Bankin...apidays New York 2025 - The Future of Small Business Lending with Open Bankin...
apidays New York 2025 - The Future of Small Business Lending with Open Bankin...
apidays
 
Advanced_English_Pronunciation_in_Use.pdf
Advanced_English_Pronunciation_in_Use.pdfAdvanced_English_Pronunciation_in_Use.pdf
Advanced_English_Pronunciation_in_Use.pdf
leogoemmanguyenthao
 
apidays Singapore 2025 - Enhancing Developer Productivity with UX (Government...
apidays Singapore 2025 - Enhancing Developer Productivity with UX (Government...apidays Singapore 2025 - Enhancing Developer Productivity with UX (Government...
apidays Singapore 2025 - Enhancing Developer Productivity with UX (Government...
apidays
 
apidays New York 2025 - Boost API Development Velocity with Practical AI Tool...
apidays New York 2025 - Boost API Development Velocity with Practical AI Tool...apidays New York 2025 - Boost API Development Velocity with Practical AI Tool...
apidays New York 2025 - Boost API Development Velocity with Practical AI Tool...
apidays
 
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); } }