SlideShare a Scribd company logo
Applets
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
2
Topics
 What is Applet?
 Difference b/w Application and Applet
 Restrictions
 Life Cycle of Applet
 Applet States and Methods
 Applet program Structure
 First Applet and Execution
 Embedding Applet in Web Page
 Displaying images using Applets
 Passing Parameters to Applet
 More Samples!!!
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
3
Applets
 An applet (little application) is a small program which
should be run within a larger program.
 An applet is a program that is typically embedded in a
Web page and can be run from a browser
 They can be transported over the Internet from one
computer (web server) to another (client computers).
 You need special HTML tag in the Web page to tell the
browser about the applet
 Created by subclassing from the
java.applet.Applet class
 Examples of Java enabled web browsers are Internet
Explorer, Firefox, chrome (only older versions)
Pig is to piglet as Application is to ??
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
4
Applet: Accessed over the Web
Hello
Hello Java
<app=
“Hello”>
4
APPLET
Development
“hello.java”
AT
SUN.COM
The Internet
hello.class
AT SUN’S
WEB
SERVER
2 31 5
Create
Applet
tag in
HTML
document
Accessing
from
Your Organisation
The browser
creates
a new
window and
a new thread
and
then runs the
code
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Difference - Applets and Applications
 An applet is basically designed for deploying on the web.
An application is designed to work as a standalone program.
 Applets are created by extending the java.applet.Applet class
There is no such constraint for an application.
 Applets run on any browser.
Applications run using Java interpreter/terminal.
 Execution of applets begin with the init() method. Execution
of applications begins with main() method.
 It is not mandatory to declare main() for an applet.
In case of application, main() has to be included in a public
class.
 Output to an Applet’s window is done by using different AWT
methods such as drawString().
In case of an application System.out.println() method is used.
5Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Difference - Applets and Applications
6Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
7
Restrictions
 Although both the Applets and stand-alone applications
are Java programs, there are certain restrictions are
imposed on Applets due to security concerns:
 They are embedded inside a web page and executed in
browsers.
 They cannot read from or write to the files on local
computer.
 They cannot communicate with other servers on the
network.
 They cannot run any programs from the local
computer.
 They are restricted from using libraries from other
languages.
 The above restrictions ensures that an Applet cannot do
any damage to the local system.Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Life cycle of an Applet
 Life cycle of an Applet specifies stages the object has
to pass right from its creation until it is destroyed.
 Every applet inherits a set of default behaviours from
the Applet class. As a result, when an applet is
loaded, it undergoes a series of changes in its state.
 For each event/state, a method is automatically
called.
 The applet states include:
 Initialisation – invokes init()
 Running – invokes start()
 Display – invokes paint()
 Idle – invokes stop()
 Dead/Destroyed State – invokes destroy()
8Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
9
Applet States
 Initialisation – invokes init() – only once
 Invoked when applet is first loaded.
 Running – invokes start() – more than once
 For the first time, it is called automatically by the
system after init() method execution.
 It is also invoked when applet moves from idle/stop()
state to active state.
 Display – invokes paint() - more than once
 It happens immediately after the applet enters into the
running state. It is responsible for displaying output.
 Idle – invokes stop() - more than once
 It is invoked when the applet is stopped from running.
For example, it occurs when we leave a web page.
 Dead/Destroyed State – invokes destroy() - only once
 This occurs automatically by invoking destroy()
method when we quite the browser.
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Redraw
Applet
stop( )
Start
state
start( ) paint( )
Life cycle of an Applet Contd…
Applet
Working
Applet
Born
Applet
Displayed
Idle
State
Applet
Destroyed
Initialization
state
destroy( )
Destroy
Appletinit( )
10Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
11
Applet Life Cycle Diagram
Born
Running Idle
Dead
Begin
init()
start()
paint()
stop()
start()
destroy()
End
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
12
Applet Program Structure
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
13
Building Applet Code: An Example
//HelloWorldApplet.java
import java.applet.Applet;
import java.awt.*;
public class HelloApplet extends Applet {
public void paint(Graphics g) {
g.drawString ("Hello World of Java!",25, 25);
}
}
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
14
Embedding Applet in Web Page
<HTML>
<HEAD>
<TITLE>
Hello World Applet
</TITLE>
</HEAD>
<body>
<h1>Hi, This is My First Java Applet on the Web!</h1>
<applet code="HelloApplet.class" width=500 height=400>
</applet>
</body>
</HTML>
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
15
Accessing Web page (runs Applet)
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
A simple applet
Output
import java.awt.*;
import java.applet.*;
public class FirstApplet extends Applet
{
String str;
public void init()
{
str = "Java is interesting!";
}
public void paint(Graphics g)
{
g.drawString(str, 70, 80);
}
}
16Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
 Create a HTML page to display the applet
<html>
<applet code=FirstApplet.class width=200 height=200>
</applet>
</html>
 Then type the following at command prompt:
 appletviewer abc.html where abc.html is the name of
the html file.
Creating an Applet
 An applet is compiled using the Java compiler: javac
 javac FirstApplet.java
17Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Displaying images using Applets
Output
/*
<applet code = DisplayImage width = 200 height = 200>
</applet>
*/
import java.awt.*;
import java.applet.*;
public class DisplayImage extends Applet
{
Image img;
public void init()
{
img = getImage(getCodeBase(),"duke.gif");
}
public void paint(Graphics g)
{
g.drawImage(img,20,20,this);
}
}
18Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Displaying images using Applets
 To display images, we need to make use of the Image
and Graphics classes.
 getCodeBase() method gets the base URL of the applet
 getImage() method returns an Image object which can
be drawn on the screen
 drawImage() takes four parameters – Image object,
location in terms of x and y coordinates and an object
of type ImageObserver
19Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
20
Sample Graphics methods
 A Graphics is something you can paint on
g.drawRect(x, y, width, height);
g.fillRect(x, y, width, height);
g.drawOval(x, y, width, height);
g.fillOval(x, y, width, height);
g.setColor(Color.red);
g.drawString(“Hello”, 20, 20); Hello
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
21
repaint( )
 Call repaint( ) when you have changed something
and want your changes to show up on the screen
 You do not need to call repaint() when something in
Java’s own components (Buttons, TextFields, etc.)
 You do need to call repaint() after drawing commands
(drawRect(...), fillRect(...), drawString(...), etc.)
 repaint( ) is a request--it might not happen
 When you call repaint( ), Java schedules a call to
update(Graphics g)
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
22
Passing Parameters to Applet
<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>Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
23
Applet Accepting Parameters
//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 paramter.
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
24
HelloAppletMsg.html
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
25
Displaying Numeric Values
//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);
}
}
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
26
SunNums.html
<HTML>
<HEAD>
<TITLE>
Hello World Applet
</TITLE>
</HEAD>
<body>
<h1>Sum of Numbers</h1>
<APPLET CODE="SumNums.class" width=500 height=400>
</APPLET>
</body>
</HTML>
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
27
Applet – Sum Numbers
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
28
The End…
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Ad

More Related Content

What's hot (20)

C sharp
C sharpC sharp
C sharp
sanjay joshi
 
Java Programming
Java ProgrammingJava Programming
Java Programming
Anjan Mahanta
 
Understanding java streams
Understanding java streamsUnderstanding java streams
Understanding java streams
Shahjahan Samoon
 
Jdbc architecture and driver types ppt
Jdbc architecture and driver types pptJdbc architecture and driver types ppt
Jdbc architecture and driver types ppt
kamal kotecha
 
Applet
 Applet Applet
Applet
swapnac12
 
Visibility control in java
Visibility control in javaVisibility control in java
Visibility control in java
Tech_MX
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
Michelle Anne Meralpis
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
Tanmoy Barman
 
ASP.NET Web form
ASP.NET Web formASP.NET Web form
ASP.NET Web form
Md. Mahedee Hasan
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
SIVASHANKARIRAJAN
 
Full Stack Web Development
Full Stack Web DevelopmentFull Stack Web Development
Full Stack Web Development
SWAGATHCHOWDARY1
 
Structure of C++ - R.D.Sivakumar
Structure of C++ - R.D.SivakumarStructure of C++ - R.D.Sivakumar
Structure of C++ - R.D.Sivakumar
Sivakumar R D .
 
Packages in java
Packages in javaPackages in java
Packages in java
Elizabeth alexander
 
Jsp lifecycle
Jsp   lifecycleJsp   lifecycle
Jsp lifecycle
chauhankapil
 
Fundamentals of JAVA
Fundamentals of JAVAFundamentals of JAVA
Fundamentals of JAVA
KUNAL GADHIA
 
Object oriented programming c++
Object oriented programming c++Object oriented programming c++
Object oriented programming c++
Ankur Pandey
 
OOP concepts -in-Python programming language
OOP concepts -in-Python programming languageOOP concepts -in-Python programming language
OOP concepts -in-Python programming language
SmritiSharma901052
 
Features of java
Features of javaFeatures of java
Features of java
WILLFREDJOSE W
 
Full stack devlopment using django main ppt
Full stack devlopment using django main pptFull stack devlopment using django main ppt
Full stack devlopment using django main ppt
SudhanshuVijay3
 
Java awt (abstract window toolkit)
Java awt (abstract window toolkit)Java awt (abstract window toolkit)
Java awt (abstract window toolkit)
Elizabeth alexander
 
Understanding java streams
Understanding java streamsUnderstanding java streams
Understanding java streams
Shahjahan Samoon
 
Jdbc architecture and driver types ppt
Jdbc architecture and driver types pptJdbc architecture and driver types ppt
Jdbc architecture and driver types ppt
kamal kotecha
 
Visibility control in java
Visibility control in javaVisibility control in java
Visibility control in java
Tech_MX
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
Michelle Anne Meralpis
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
Tanmoy Barman
 
Full Stack Web Development
Full Stack Web DevelopmentFull Stack Web Development
Full Stack Web Development
SWAGATHCHOWDARY1
 
Structure of C++ - R.D.Sivakumar
Structure of C++ - R.D.SivakumarStructure of C++ - R.D.Sivakumar
Structure of C++ - R.D.Sivakumar
Sivakumar R D .
 
Fundamentals of JAVA
Fundamentals of JAVAFundamentals of JAVA
Fundamentals of JAVA
KUNAL GADHIA
 
Object oriented programming c++
Object oriented programming c++Object oriented programming c++
Object oriented programming c++
Ankur Pandey
 
OOP concepts -in-Python programming language
OOP concepts -in-Python programming languageOOP concepts -in-Python programming language
OOP concepts -in-Python programming language
SmritiSharma901052
 
Full stack devlopment using django main ppt
Full stack devlopment using django main pptFull stack devlopment using django main ppt
Full stack devlopment using django main ppt
SudhanshuVijay3
 
Java awt (abstract window toolkit)
Java awt (abstract window toolkit)Java awt (abstract window toolkit)
Java awt (abstract window toolkit)
Elizabeth alexander
 

Similar to Java applet programming concepts (20)

Basic of Applet
Basic of AppletBasic of Applet
Basic of Applet
suraj pandey
 
Applets in Java
Applets in JavaApplets in Java
Applets in Java
RamaPrabha24
 
Applets in Java. Learn java program with applets
Applets in Java. Learn java program with appletsApplets in Java. Learn java program with applets
Applets in Java. Learn java program with applets
halaplay385
 
Advanced programming chapter 2 - Java Applet.pdf
Advanced programming chapter 2 - Java Applet.pdfAdvanced programming chapter 2 - Java Applet.pdf
Advanced programming chapter 2 - Java Applet.pdf
fikadumeuedu
 
Class notes(week 10) on applet programming
Class notes(week 10) on applet programmingClass notes(week 10) on applet programming
Class notes(week 10) on applet programming
Kuntal Bhowmick
 
Unit 7 Java
Unit 7 JavaUnit 7 Java
Unit 7 Java
arnold 7490
 
Appletjava
AppletjavaAppletjava
Appletjava
DEEPIKA T
 
Applet in java new
Applet in java newApplet in java new
Applet in java new
Kavitha713564
 
3. applets
3. applets3. applets
3. applets
AnusAhmad
 
Java applet
Java appletJava applet
Java applet
Elizabeth alexander
 
Java: Java Applets
Java: Java AppletsJava: Java Applets
Java: Java Applets
Tareq Hasan
 
Introduction To Applets methods and simple examples
Introduction To Applets methods  and simple examplesIntroduction To Applets methods  and simple examples
Introduction To Applets methods and simple examples
MsPariyalNituLaxman
 
MSBTE Computer Engineering Java applet.pptx
MSBTE Computer Engineering Java applet.pptxMSBTE Computer Engineering Java applet.pptx
MSBTE Computer Engineering Java applet.pptx
kunalgaikwad1705
 
Applet progming
Applet progmingApplet progming
Applet progming
VIKRANTHMALLIKARJUN
 
Applet Architecture - Introducing Java Applets
Applet Architecture - Introducing Java AppletsApplet Architecture - Introducing Java Applets
Applet Architecture - Introducing Java Applets
amitksaha
 
Applet
AppletApplet
Applet
Priyanka Pradhan
 
Smart material - Unit 3 (2).pdf
Smart material - Unit 3 (2).pdfSmart material - Unit 3 (2).pdf
Smart material - Unit 3 (2).pdf
GayathriRHICETCSESTA
 
Smart material - Unit 3 (1).pdf
Smart material - Unit 3 (1).pdfSmart material - Unit 3 (1).pdf
Smart material - Unit 3 (1).pdf
GayathriRHICETCSESTA
 
applet.pptx
applet.pptxapplet.pptx
applet.pptx
SachinBhosale73
 
Applet and graphics programming
Applet and graphics programmingApplet and graphics programming
Applet and graphics programming
mcanotes
 
Applets in Java. Learn java program with applets
Applets in Java. Learn java program with appletsApplets in Java. Learn java program with applets
Applets in Java. Learn java program with applets
halaplay385
 
Advanced programming chapter 2 - Java Applet.pdf
Advanced programming chapter 2 - Java Applet.pdfAdvanced programming chapter 2 - Java Applet.pdf
Advanced programming chapter 2 - Java Applet.pdf
fikadumeuedu
 
Class notes(week 10) on applet programming
Class notes(week 10) on applet programmingClass notes(week 10) on applet programming
Class notes(week 10) on applet programming
Kuntal Bhowmick
 
Java: Java Applets
Java: Java AppletsJava: Java Applets
Java: Java Applets
Tareq Hasan
 
Introduction To Applets methods and simple examples
Introduction To Applets methods  and simple examplesIntroduction To Applets methods  and simple examples
Introduction To Applets methods and simple examples
MsPariyalNituLaxman
 
MSBTE Computer Engineering Java applet.pptx
MSBTE Computer Engineering Java applet.pptxMSBTE Computer Engineering Java applet.pptx
MSBTE Computer Engineering Java applet.pptx
kunalgaikwad1705
 
Applet Architecture - Introducing Java Applets
Applet Architecture - Introducing Java AppletsApplet Architecture - Introducing Java Applets
Applet Architecture - Introducing Java Applets
amitksaha
 
Applet and graphics programming
Applet and graphics programmingApplet and graphics programming
Applet and graphics programming
mcanotes
 
Ad

More from Victer Paul (13)

OOAD - UML - Sequence and Communication Diagrams - Lab
OOAD - UML - Sequence and Communication Diagrams - LabOOAD - UML - Sequence and Communication Diagrams - Lab
OOAD - UML - Sequence and Communication Diagrams - Lab
Victer Paul
 
OOAD - UML - Class and Object Diagrams - Lab
OOAD - UML - Class and Object Diagrams - LabOOAD - UML - Class and Object Diagrams - Lab
OOAD - UML - Class and Object Diagrams - Lab
Victer Paul
 
OOAD - Systems and Object Orientation Concepts
OOAD - Systems and Object Orientation ConceptsOOAD - Systems and Object Orientation Concepts
OOAD - Systems and Object Orientation Concepts
Victer Paul
 
Java - Strings Concepts
Java - Strings ConceptsJava - Strings Concepts
Java - Strings Concepts
Victer Paul
 
Java - Packages Concepts
Java - Packages ConceptsJava - Packages Concepts
Java - Packages Concepts
Victer Paul
 
Java - OOPS and Java Basics
Java - OOPS and Java BasicsJava - OOPS and Java Basics
Java - OOPS and Java Basics
Victer Paul
 
Java - Exception Handling Concepts
Java - Exception Handling ConceptsJava - Exception Handling Concepts
Java - Exception Handling Concepts
Victer Paul
 
Java - Class Structure
Java - Class StructureJava - Class Structure
Java - Class Structure
Victer Paul
 
Java - Object Oriented Programming Concepts
Java - Object Oriented Programming ConceptsJava - Object Oriented Programming Concepts
Java - Object Oriented Programming Concepts
Victer Paul
 
Java - Basic Concepts
Java - Basic ConceptsJava - Basic Concepts
Java - Basic Concepts
Victer Paul
 
Java - File Input Output Concepts
Java - File Input Output ConceptsJava - File Input Output Concepts
Java - File Input Output Concepts
Victer Paul
 
Java - Inheritance Concepts
Java - Inheritance ConceptsJava - Inheritance Concepts
Java - Inheritance Concepts
Victer Paul
 
Java - Arrays Concepts
Java - Arrays ConceptsJava - Arrays Concepts
Java - Arrays Concepts
Victer Paul
 
OOAD - UML - Sequence and Communication Diagrams - Lab
OOAD - UML - Sequence and Communication Diagrams - LabOOAD - UML - Sequence and Communication Diagrams - Lab
OOAD - UML - Sequence and Communication Diagrams - Lab
Victer Paul
 
OOAD - UML - Class and Object Diagrams - Lab
OOAD - UML - Class and Object Diagrams - LabOOAD - UML - Class and Object Diagrams - Lab
OOAD - UML - Class and Object Diagrams - Lab
Victer Paul
 
OOAD - Systems and Object Orientation Concepts
OOAD - Systems and Object Orientation ConceptsOOAD - Systems and Object Orientation Concepts
OOAD - Systems and Object Orientation Concepts
Victer Paul
 
Java - Strings Concepts
Java - Strings ConceptsJava - Strings Concepts
Java - Strings Concepts
Victer Paul
 
Java - Packages Concepts
Java - Packages ConceptsJava - Packages Concepts
Java - Packages Concepts
Victer Paul
 
Java - OOPS and Java Basics
Java - OOPS and Java BasicsJava - OOPS and Java Basics
Java - OOPS and Java Basics
Victer Paul
 
Java - Exception Handling Concepts
Java - Exception Handling ConceptsJava - Exception Handling Concepts
Java - Exception Handling Concepts
Victer Paul
 
Java - Class Structure
Java - Class StructureJava - Class Structure
Java - Class Structure
Victer Paul
 
Java - Object Oriented Programming Concepts
Java - Object Oriented Programming ConceptsJava - Object Oriented Programming Concepts
Java - Object Oriented Programming Concepts
Victer Paul
 
Java - Basic Concepts
Java - Basic ConceptsJava - Basic Concepts
Java - Basic Concepts
Victer Paul
 
Java - File Input Output Concepts
Java - File Input Output ConceptsJava - File Input Output Concepts
Java - File Input Output Concepts
Victer Paul
 
Java - Inheritance Concepts
Java - Inheritance ConceptsJava - Inheritance Concepts
Java - Inheritance Concepts
Victer Paul
 
Java - Arrays Concepts
Java - Arrays ConceptsJava - Arrays Concepts
Java - Arrays Concepts
Victer Paul
 
Ad

Recently uploaded (20)

Shoehorning dependency injection into a FP language, what does it take?
Shoehorning dependency injection into a FP language, what does it take?Shoehorning dependency injection into a FP language, what does it take?
Shoehorning dependency injection into a FP language, what does it take?
Eric Torreborre
 
GDG Cloud Southlake #42: Suresh Mathew: Autonomous Resource Optimization: How...
GDG Cloud Southlake #42: Suresh Mathew: Autonomous Resource Optimization: How...GDG Cloud Southlake #42: Suresh Mathew: Autonomous Resource Optimization: How...
GDG Cloud Southlake #42: Suresh Mathew: Autonomous Resource Optimization: How...
James Anderson
 
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
Ivano Malavolta
 
Webinar - Top 5 Backup Mistakes MSPs and Businesses Make .pptx
Webinar - Top 5 Backup Mistakes MSPs and Businesses Make   .pptxWebinar - Top 5 Backup Mistakes MSPs and Businesses Make   .pptx
Webinar - Top 5 Backup Mistakes MSPs and Businesses Make .pptx
MSP360
 
How to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabberHow to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabber
eGrabber
 
Jignesh Shah - The Innovator and Czar of Exchanges
Jignesh Shah - The Innovator and Czar of ExchangesJignesh Shah - The Innovator and Czar of Exchanges
Jignesh Shah - The Innovator and Czar of Exchanges
Jignesh Shah Innovator
 
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptxReimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
John Moore
 
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdfKit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Wonjun Hwang
 
AsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API DesignAsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API Design
leonid54
 
UiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer OpportunitiesUiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer Opportunities
DianaGray10
 
The Changing Compliance Landscape in 2025.pdf
The Changing Compliance Landscape in 2025.pdfThe Changing Compliance Landscape in 2025.pdf
The Changing Compliance Landscape in 2025.pdf
Precisely
 
Bepents tech services - a premier cybersecurity consulting firm
Bepents tech services - a premier cybersecurity consulting firmBepents tech services - a premier cybersecurity consulting firm
Bepents tech services - a premier cybersecurity consulting firm
Benard76
 
Zilliz Cloud Monthly Technical Review: May 2025
Zilliz Cloud Monthly Technical Review: May 2025Zilliz Cloud Monthly Technical Review: May 2025
Zilliz Cloud Monthly Technical Review: May 2025
Zilliz
 
Viam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdfViam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdf
camilalamoratta
 
Transcript: Canadian book publishing: Insights from the latest salary survey ...
Transcript: Canadian book publishing: Insights from the latest salary survey ...Transcript: Canadian book publishing: Insights from the latest salary survey ...
Transcript: Canadian book publishing: Insights from the latest salary survey ...
BookNet Canada
 
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Safe Software
 
Q1 2025 Dropbox Earnings and Investor Presentation
Q1 2025 Dropbox Earnings and Investor PresentationQ1 2025 Dropbox Earnings and Investor Presentation
Q1 2025 Dropbox Earnings and Investor Presentation
Dropbox
 
machines-for-woodworking-shops-en-compressed.pdf
machines-for-woodworking-shops-en-compressed.pdfmachines-for-woodworking-shops-en-compressed.pdf
machines-for-woodworking-shops-en-compressed.pdf
AmirStern2
 
Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...
Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...
Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...
Mike Mingos
 
Smart Investments Leveraging Agentic AI for Real Estate Success.pptx
Smart Investments Leveraging Agentic AI for Real Estate Success.pptxSmart Investments Leveraging Agentic AI for Real Estate Success.pptx
Smart Investments Leveraging Agentic AI for Real Estate Success.pptx
Seasia Infotech
 
Shoehorning dependency injection into a FP language, what does it take?
Shoehorning dependency injection into a FP language, what does it take?Shoehorning dependency injection into a FP language, what does it take?
Shoehorning dependency injection into a FP language, what does it take?
Eric Torreborre
 
GDG Cloud Southlake #42: Suresh Mathew: Autonomous Resource Optimization: How...
GDG Cloud Southlake #42: Suresh Mathew: Autonomous Resource Optimization: How...GDG Cloud Southlake #42: Suresh Mathew: Autonomous Resource Optimization: How...
GDG Cloud Southlake #42: Suresh Mathew: Autonomous Resource Optimization: How...
James Anderson
 
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
Ivano Malavolta
 
Webinar - Top 5 Backup Mistakes MSPs and Businesses Make .pptx
Webinar - Top 5 Backup Mistakes MSPs and Businesses Make   .pptxWebinar - Top 5 Backup Mistakes MSPs and Businesses Make   .pptx
Webinar - Top 5 Backup Mistakes MSPs and Businesses Make .pptx
MSP360
 
How to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabberHow to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabber
eGrabber
 
Jignesh Shah - The Innovator and Czar of Exchanges
Jignesh Shah - The Innovator and Czar of ExchangesJignesh Shah - The Innovator and Czar of Exchanges
Jignesh Shah - The Innovator and Czar of Exchanges
Jignesh Shah Innovator
 
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptxReimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
John Moore
 
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdfKit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Wonjun Hwang
 
AsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API DesignAsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API Design
leonid54
 
UiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer OpportunitiesUiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer Opportunities
DianaGray10
 
The Changing Compliance Landscape in 2025.pdf
The Changing Compliance Landscape in 2025.pdfThe Changing Compliance Landscape in 2025.pdf
The Changing Compliance Landscape in 2025.pdf
Precisely
 
Bepents tech services - a premier cybersecurity consulting firm
Bepents tech services - a premier cybersecurity consulting firmBepents tech services - a premier cybersecurity consulting firm
Bepents tech services - a premier cybersecurity consulting firm
Benard76
 
Zilliz Cloud Monthly Technical Review: May 2025
Zilliz Cloud Monthly Technical Review: May 2025Zilliz Cloud Monthly Technical Review: May 2025
Zilliz Cloud Monthly Technical Review: May 2025
Zilliz
 
Viam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdfViam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdf
camilalamoratta
 
Transcript: Canadian book publishing: Insights from the latest salary survey ...
Transcript: Canadian book publishing: Insights from the latest salary survey ...Transcript: Canadian book publishing: Insights from the latest salary survey ...
Transcript: Canadian book publishing: Insights from the latest salary survey ...
BookNet Canada
 
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Safe Software
 
Q1 2025 Dropbox Earnings and Investor Presentation
Q1 2025 Dropbox Earnings and Investor PresentationQ1 2025 Dropbox Earnings and Investor Presentation
Q1 2025 Dropbox Earnings and Investor Presentation
Dropbox
 
machines-for-woodworking-shops-en-compressed.pdf
machines-for-woodworking-shops-en-compressed.pdfmachines-for-woodworking-shops-en-compressed.pdf
machines-for-woodworking-shops-en-compressed.pdf
AmirStern2
 
Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...
Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...
Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...
Mike Mingos
 
Smart Investments Leveraging Agentic AI for Real Estate Success.pptx
Smart Investments Leveraging Agentic AI for Real Estate Success.pptxSmart Investments Leveraging Agentic AI for Real Estate Success.pptx
Smart Investments Leveraging Agentic AI for Real Estate Success.pptx
Seasia Infotech
 

Java applet programming concepts

  • 1. Applets Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 2. 2 Topics  What is Applet?  Difference b/w Application and Applet  Restrictions  Life Cycle of Applet  Applet States and Methods  Applet program Structure  First Applet and Execution  Embedding Applet in Web Page  Displaying images using Applets  Passing Parameters to Applet  More Samples!!! Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 3. 3 Applets  An applet (little application) is a small program which should be run within a larger program.  An applet is a program that is typically embedded in a Web page and can be run from a browser  They can be transported over the Internet from one computer (web server) to another (client computers).  You need special HTML tag in the Web page to tell the browser about the applet  Created by subclassing from the java.applet.Applet class  Examples of Java enabled web browsers are Internet Explorer, Firefox, chrome (only older versions) Pig is to piglet as Application is to ?? Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 4. 4 Applet: Accessed over the Web Hello Hello Java <app= “Hello”> 4 APPLET Development “hello.java” AT SUN.COM The Internet hello.class AT SUN’S WEB SERVER 2 31 5 Create Applet tag in HTML document Accessing from Your Organisation The browser creates a new window and a new thread and then runs the code Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 5. Difference - Applets and Applications  An applet is basically designed for deploying on the web. An application is designed to work as a standalone program.  Applets are created by extending the java.applet.Applet class There is no such constraint for an application.  Applets run on any browser. Applications run using Java interpreter/terminal.  Execution of applets begin with the init() method. Execution of applications begins with main() method.  It is not mandatory to declare main() for an applet. In case of application, main() has to be included in a public class.  Output to an Applet’s window is done by using different AWT methods such as drawString(). In case of an application System.out.println() method is used. 5Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 6. Difference - Applets and Applications 6Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 7. 7 Restrictions  Although both the Applets and stand-alone applications are Java programs, there are certain restrictions are imposed on Applets due to security concerns:  They are embedded inside a web page and executed in browsers.  They cannot read from or write to the files on local computer.  They cannot communicate with other servers on the network.  They cannot run any programs from the local computer.  They are restricted from using libraries from other languages.  The above restrictions ensures that an Applet cannot do any damage to the local system.Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 8. Life cycle of an Applet  Life cycle of an Applet specifies stages the object has to pass right from its creation until it is destroyed.  Every applet inherits a set of default behaviours from the Applet class. As a result, when an applet is loaded, it undergoes a series of changes in its state.  For each event/state, a method is automatically called.  The applet states include:  Initialisation – invokes init()  Running – invokes start()  Display – invokes paint()  Idle – invokes stop()  Dead/Destroyed State – invokes destroy() 8Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 9. 9 Applet States  Initialisation – invokes init() – only once  Invoked when applet is first loaded.  Running – invokes start() – more than once  For the first time, it is called automatically by the system after init() method execution.  It is also invoked when applet moves from idle/stop() state to active state.  Display – invokes paint() - more than once  It happens immediately after the applet enters into the running state. It is responsible for displaying output.  Idle – invokes stop() - more than once  It is invoked when the applet is stopped from running. For example, it occurs when we leave a web page.  Dead/Destroyed State – invokes destroy() - only once  This occurs automatically by invoking destroy() method when we quite the browser. Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 10. Redraw Applet stop( ) Start state start( ) paint( ) Life cycle of an Applet Contd… Applet Working Applet Born Applet Displayed Idle State Applet Destroyed Initialization state destroy( ) Destroy Appletinit( ) 10Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 11. 11 Applet Life Cycle Diagram Born Running Idle Dead Begin init() start() paint() stop() start() destroy() End Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 12. 12 Applet Program Structure Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 13. 13 Building Applet Code: An Example //HelloWorldApplet.java import java.applet.Applet; import java.awt.*; public class HelloApplet extends Applet { public void paint(Graphics g) { g.drawString ("Hello World of Java!",25, 25); } } Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 14. 14 Embedding Applet in Web Page <HTML> <HEAD> <TITLE> Hello World Applet </TITLE> </HEAD> <body> <h1>Hi, This is My First Java Applet on the Web!</h1> <applet code="HelloApplet.class" width=500 height=400> </applet> </body> </HTML> Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 15. 15 Accessing Web page (runs Applet) Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 16. A simple applet Output import java.awt.*; import java.applet.*; public class FirstApplet extends Applet { String str; public void init() { str = "Java is interesting!"; } public void paint(Graphics g) { g.drawString(str, 70, 80); } } 16Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 17.  Create a HTML page to display the applet <html> <applet code=FirstApplet.class width=200 height=200> </applet> </html>  Then type the following at command prompt:  appletviewer abc.html where abc.html is the name of the html file. Creating an Applet  An applet is compiled using the Java compiler: javac  javac FirstApplet.java 17Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 18. Displaying images using Applets Output /* <applet code = DisplayImage width = 200 height = 200> </applet> */ import java.awt.*; import java.applet.*; public class DisplayImage extends Applet { Image img; public void init() { img = getImage(getCodeBase(),"duke.gif"); } public void paint(Graphics g) { g.drawImage(img,20,20,this); } } 18Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 19. Displaying images using Applets  To display images, we need to make use of the Image and Graphics classes.  getCodeBase() method gets the base URL of the applet  getImage() method returns an Image object which can be drawn on the screen  drawImage() takes four parameters – Image object, location in terms of x and y coordinates and an object of type ImageObserver 19Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 20. 20 Sample Graphics methods  A Graphics is something you can paint on g.drawRect(x, y, width, height); g.fillRect(x, y, width, height); g.drawOval(x, y, width, height); g.fillOval(x, y, width, height); g.setColor(Color.red); g.drawString(“Hello”, 20, 20); Hello Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 21. 21 repaint( )  Call repaint( ) when you have changed something and want your changes to show up on the screen  You do not need to call repaint() when something in Java’s own components (Buttons, TextFields, etc.)  You do need to call repaint() after drawing commands (drawRect(...), fillRect(...), drawString(...), etc.)  repaint( ) is a request--it might not happen  When you call repaint( ), Java schedules a call to update(Graphics g) Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 22. 22 Passing Parameters to Applet <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>Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 23. 23 Applet Accepting Parameters //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 paramter. Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 24. 24 HelloAppletMsg.html Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 25. 25 Displaying Numeric Values //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); } } Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 26. 26 SunNums.html <HTML> <HEAD> <TITLE> Hello World Applet </TITLE> </HEAD> <body> <h1>Sum of Numbers</h1> <APPLET CODE="SumNums.class" width=500 height=400> </APPLET> </body> </HTML> Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 27. 27 Applet – Sum Numbers Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 28. 28 The End… Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  翻译: