Java applet programming concepts

victerpaul 512 views 28 slides May 10, 2020
Slide 1
Slide 1 of 28
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28

About This Presentation

Java Applet Programming Concepts


Slide Content

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 appletis 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 subclassingfrom the
java.applet.Applet class
Examples of Java enabled web browsers are Internet
Explorer, Firefox, chrome (only older versions)
Pigis to piglet asApplication 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.Appletclass
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.
5
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam

Difference -Applets and Applications
6
Dr. 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
LifecycleofanAppletspecifiesstagestheobjecthas
topassrightfromitscreationuntilitisdestroyed.
Everyappletinheritsasetofdefaultbehavioursfrom
theAppletclass.Asaresult,whenanappletis
loaded,itundergoesaseriesofchangesinitsstate.
Foreachevent/state,amethodisautomatically
called.
The applet states include:
Initialisation–invokes init()
Running–invokes start()
Display–invokes paint()
Idle–invokes stop()
Dead/DestroyedState –invokes destroy()
8
Dr. 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
Applet
init( )
10
Dr. 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 HelloAppletextends 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 FirstAppletextends Applet
{
String str;
public void init()
{
str= "Java is interesting!";
}
public void paint(Graphics g)
{
g.drawString(str, 70, 80);
}
}
16
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam

Create a HTML page to display the applet
<html>
<applet code=FirstApplet.classwidth=200 height=200>
</applet>
</html>
Then type the following at command prompt:
appletviewerabc.htmlwhere abc.html is the name of
the html file.
Creating an Applet
An applet is compiled using the Java compiler: javac
javacFirstApplet.java
17
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam

Displaying images using Applets
Output
/*
<applet code = DisplayImagewidth = 200 height = 200>
</applet>
*/
import java.awt.*;
import java.applet.*;
public class DisplayImageextends Applet
{
Image img;
public void init()
{
img= getImage(getCodeBase(),"duke.gif");
}
public void paint(Graphics g)
{
g.drawImage(img,20,20,this);
}
}
18
Dr. 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
19
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam

20
Sample Graphicsmethods
A Graphicsis 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 notneed to call repaint()when something in
Java’s own components (Buttons, TextFields, etc.)
You doneed to call repaint()after drawing commands
(drawRect(...), fillRect(...), drawString(...), etc.)
repaint( ) is a request--it might not happen
When you callrepaint( ),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 HelloAppletMsgextends 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