Applets - Dr. Ramesh R. Manza

Download Report

Transcript Applets - Dr. Ramesh R. Manza

Pravin Yannawar, DOCS, NMU Jalgaon
Objectives of This Session
•
•
•
•
•
•
•
Identify the need for Applets
Distinguish between Applets and Applications
State the restrictions on Applets
State Applet Life Cycle
Explain the Applet tag & its various attributes .
Explain process of passing parameters to Applet
State the concept of applet container and
AppletContext class
• Explain InterApplet Communication
Basic Java : Applets
2
Applets
• Is an application designed to be transmitted
over the internet & executed by a Javacompatible web browser.
• Are small applications that are accessed on a
Internet server, transported over the Internet,
automatically installed & run as part of a web
document.
Basic Java : Applets
3
Applet Hierarchy
Object
Component
Container
Panel
Applet
Basic Java : Applets
4
Applet Vs Applications
•
•
•
•
•
•
•
•
HTML file
Applet Context
No main()
Extend Applet class
init()
Flow Layout
No titles
Access Restrictions
Basic Java : Applets
•
•
•
•
•
•
•
•
No HTML file
Operating System
main()
Not necessary
Constructor
Border Layout
Titles
No Restrictions
5
Security Restrictions
• Applets can’t read / write files on user’s file
system.
• Can’t communicate with an internet site other
than the one that served the web page that
included the applet.
• Can never run any executable program.
• All windows popped by an applet carry a
warning message.
• Can never find any info about the local
computer.
Basic Java : Applets
6
Applet Demo
import java.awt.*;
import java.applet.*;
class MyApplet extends Applet
{
public void paint(Graphics g)
{
g.drawString(“Hello world”,50,50);
}
}
Basic Java : Applets
7
Html Demo File
<HTML>
<HEAD>
<TITLE> My first applet </TITLE>
</HEAD>
<BODY>
<APPLET CODE = “MyApplet.class” WIDTH = 200
HEIGHT = 200>
</APPLET>
</BODY>
</HTML>
Basic Java : Applets
8
Converting Applications to Applets
• Create web page with appropriate tags to load
applet code.
• Eliminate main() method.
• Replace Frame class with Applet class.
• Remove call to setSize() and setTitle().
• Remove call to addWindowListener.
• Replace constructor with init() method.
Basic Java : Applets
9
Applet Life Cycle
•
•
•
•
•
init()
start()
stop()
destroy()
paint(Graphics g)
Basic Java : Applets
10
public void init()
• Automatically invoked when java launches
applet for the first time.
• Called only once.
• Functionality performed commonly includes:



Creating the applet’s GUI.
Reading values from <param> tags.
Loading off-screen images & audio clips from
external files.
Basic Java : Applets
11
public void start()
• Automatically invoked after init().
• Also invoked when browser browses back to
applet after following a link to a diff page.
• Thus, code that needs to be executed
repeatedly must be put in start().

E.g. Restart thread to resume an animation.
Basic Java : Applets
12
public void stop()
• Automatically invoked whenever user moves off
the page on which the applet resides.
• Works in partnership with start().
Basic Java : Applets
13
public void destroy()
• Automatically invoked when browser no longer
needs the applet.
• Is an applets opportunity to release any non
memory resources it may have, such as
threads & network connections & graphic
contexts.
Basic Java : Applets
14
<applet> attributes
•
•
•
•
•
•
•
CODE
CODEBASE
WIDTH / HEIGHT
ALIGN
HSPACE / VSPACE
NAME
<PARAM> tag
Basic Java : Applets
15
CODEBASE attribute
• Tells browser to search for applet class files in
the directory specified by the URL..
• If this attribute isn’t there, the codebase dir is
assumed to be in the same dir that the HTML
file resides
Basic Java : Applets
16
Applet(Parameter Passing)
HTML FILE
<APPLET CODE = “test.class ” WIDTH = 100 HEIGHT =100>
<PARAM
NAME = font VALUE=“Times New Roman” >
<PARAM
NAME = size VALUE=“24” >
</APPLET>
Basic Java : Applets
17
Applet
public void paint(Graphics g)
{
String fontName = getParameter(“font”);
int fontSize =
Integer.parseInt(getParameter(“size”));
g.setFont(fontName,Font.BOLD,fontSize);
g.drawString(“Hey SEED !!!”,50,50);
}
Basic Java : Applets
18
The Applet Context
• Is the context (browser/ applet Viewer) in
which the applet runs.
• The getAppletContext() methods returns an
object that implements an interface of the type
AppletContext.
• This enables your applet to access features of
the browser that contains it.
Basic Java : Applets
19
Methods of AppletContext
• Applet getApplet(String name): returns a
handle to a named applet.
• AudioClip getAudioClip(URL soundFileURL):
returns an audio clip object.
• Image getImage(URL imageFile) : returns an
image.
• void showDocument(URL docURL) : requests
the browser to display the HTML page specified
in parameter.
Basic Java : Applets
20
InterApplet Communication
• Get the reference of the AppletContext
Interface using the getAppletContext() method
of Applet.
• Call the getApplet(String name) on the
AppletContext which returns a reference to a
named Applet.
• Call any method of Applet using this interface.
Basic Java : Applets
21