SlideShare a Scribd company logo
1 of 28
Download to read offline
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

More Related Content

What's hot

Tortuga-A simulation software
Tortuga-A simulation softwareTortuga-A simulation software
Tortuga-A simulation softwareSyeda Nyma
 
Eo gaddis java_chapter_13_5e
Eo gaddis java_chapter_13_5eEo gaddis java_chapter_13_5e
Eo gaddis java_chapter_13_5eGina Bullock
 
Eo gaddis java_chapter_14_5e
Eo gaddis java_chapter_14_5eEo gaddis java_chapter_14_5e
Eo gaddis java_chapter_14_5eGina Bullock
 
Eo gaddis java_chapter_11_5e
Eo gaddis java_chapter_11_5eEo gaddis java_chapter_11_5e
Eo gaddis java_chapter_11_5eGina Bullock
 
Challenges in mobile test automation - 2011
Challenges in mobile test automation - 2011Challenges in mobile test automation - 2011
Challenges in mobile test automation - 2011Daniel Knott
 

What's hot (9)

Tortuga-A simulation software
Tortuga-A simulation softwareTortuga-A simulation software
Tortuga-A simulation software
 
Best practices android_2010
Best practices android_2010Best practices android_2010
Best practices android_2010
 
Android Lab
Android LabAndroid Lab
Android Lab
 
Chapter 5
Chapter 5Chapter 5
Chapter 5
 
Eo gaddis java_chapter_13_5e
Eo gaddis java_chapter_13_5eEo gaddis java_chapter_13_5e
Eo gaddis java_chapter_13_5e
 
Eo gaddis java_chapter_14_5e
Eo gaddis java_chapter_14_5eEo gaddis java_chapter_14_5e
Eo gaddis java_chapter_14_5e
 
Eo gaddis java_chapter_11_5e
Eo gaddis java_chapter_11_5eEo gaddis java_chapter_11_5e
Eo gaddis java_chapter_11_5e
 
Easymock tutorial
Easymock tutorialEasymock tutorial
Easymock tutorial
 
Challenges in mobile test automation - 2011
Challenges in mobile test automation - 2011Challenges in mobile test automation - 2011
Challenges in mobile test automation - 2011
 

Similar to Java applet programming concepts

Similar to Java applet programming concepts (20)

Applets in Java
Applets in JavaApplets in Java
Applets in Java
 
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
 
Unit 7 Java
Unit 7 JavaUnit 7 Java
Unit 7 Java
 
Appletjava
AppletjavaAppletjava
Appletjava
 
Applet in java new
Applet in java newApplet in java new
Applet in java new
 
3. applets
3. applets3. applets
3. applets
 
Java applet
Java appletJava applet
Java applet
 
Java: Java Applets
Java: Java AppletsJava: Java Applets
Java: Java Applets
 
Applet progming
Applet progmingApplet progming
Applet progming
 
Applet Architecture - Introducing Java Applets
Applet Architecture - Introducing Java AppletsApplet Architecture - Introducing Java Applets
Applet Architecture - Introducing Java Applets
 
Applet
AppletApplet
Applet
 
Smart material - Unit 3 (2).pdf
Smart material - Unit 3 (2).pdfSmart material - Unit 3 (2).pdf
Smart material - Unit 3 (2).pdf
 
Smart material - Unit 3 (1).pdf
Smart material - Unit 3 (1).pdfSmart material - Unit 3 (1).pdf
Smart material - Unit 3 (1).pdf
 
applet.pptx
applet.pptxapplet.pptx
applet.pptx
 
Applet
AppletApplet
Applet
 
Applet and graphics programming
Applet and graphics programmingApplet and graphics programming
Applet and graphics programming
 
Java applet basics
Java applet basicsJava applet basics
Java applet basics
 
Lecture 22
Lecture 22Lecture 22
Lecture 22
 
Slide8appletv2 091028110313-phpapp01
Slide8appletv2 091028110313-phpapp01Slide8appletv2 091028110313-phpapp01
Slide8appletv2 091028110313-phpapp01
 
Applets
AppletsApplets
Applets
 

More from 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 - LabVicter 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 - LabVicter Paul
 
OOAD - Systems and Object Orientation Concepts
OOAD - Systems and Object Orientation ConceptsOOAD - Systems and Object Orientation Concepts
OOAD - Systems and Object Orientation ConceptsVicter Paul
 
Java - Strings Concepts
Java - Strings ConceptsJava - Strings Concepts
Java - Strings ConceptsVicter Paul
 
Java - Packages Concepts
Java - Packages ConceptsJava - Packages Concepts
Java - Packages ConceptsVicter Paul
 
Java - OOPS and Java Basics
Java - OOPS and Java BasicsJava - OOPS and Java Basics
Java - OOPS and Java BasicsVicter Paul
 
Java - Exception Handling Concepts
Java - Exception Handling ConceptsJava - Exception Handling Concepts
Java - Exception Handling ConceptsVicter Paul
 
Java - Class Structure
Java - Class StructureJava - Class Structure
Java - Class StructureVicter Paul
 
Java - Object Oriented Programming Concepts
Java - Object Oriented Programming ConceptsJava - Object Oriented Programming Concepts
Java - Object Oriented Programming ConceptsVicter Paul
 
Java - Basic Concepts
Java - Basic ConceptsJava - Basic Concepts
Java - Basic ConceptsVicter Paul
 
Java - File Input Output Concepts
Java - File Input Output ConceptsJava - File Input Output Concepts
Java - File Input Output ConceptsVicter Paul
 
Java - Inheritance Concepts
Java - Inheritance ConceptsJava - Inheritance Concepts
Java - Inheritance ConceptsVicter Paul
 
Java - Arrays Concepts
Java - Arrays ConceptsJava - Arrays Concepts
Java - Arrays ConceptsVicter Paul
 

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
 
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
 
OOAD - Systems and Object Orientation Concepts
OOAD - Systems and Object Orientation ConceptsOOAD - Systems and Object Orientation Concepts
OOAD - Systems and Object Orientation Concepts
 
Java - Strings Concepts
Java - Strings ConceptsJava - Strings Concepts
Java - Strings Concepts
 
Java - Packages Concepts
Java - Packages ConceptsJava - Packages Concepts
Java - Packages Concepts
 
Java - OOPS and Java Basics
Java - OOPS and Java BasicsJava - OOPS and Java Basics
Java - OOPS and Java Basics
 
Java - Exception Handling Concepts
Java - Exception Handling ConceptsJava - Exception Handling Concepts
Java - Exception Handling Concepts
 
Java - Class Structure
Java - Class StructureJava - Class Structure
Java - Class Structure
 
Java - Object Oriented Programming Concepts
Java - Object Oriented Programming ConceptsJava - Object Oriented Programming Concepts
Java - Object Oriented Programming Concepts
 
Java - Basic Concepts
Java - Basic ConceptsJava - Basic Concepts
Java - Basic Concepts
 
Java - File Input Output Concepts
Java - File Input Output ConceptsJava - File Input Output Concepts
Java - File Input Output Concepts
 
Java - Inheritance Concepts
Java - Inheritance ConceptsJava - Inheritance Concepts
Java - Inheritance Concepts
 
Java - Arrays Concepts
Java - Arrays ConceptsJava - Arrays Concepts
Java - Arrays Concepts
 

Recently uploaded

Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfjimielynbastida
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfngoud9212
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 

Recently uploaded (20)

The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdf
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdf
 
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 

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