pravin yannawar, docs, nmu jalgaon basic java : applets 2 objectives of this session identify the...
TRANSCRIPT
Pravin Yannawar, DOCS, NMU Jalgaon
Basic Java : Applets2
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 : Applets3
Applets
• Is an application designed to be transmitted over the internet & executed by a Java-compatible 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 : Applets4
Applet Hierarchy
Object
Component
Container
Panel
Applet
Basic Java : Applets5
Applet Vs Applications
• HTML file• Applet Context• No main()• Extend Applet class• init()• Flow Layout• No titles• Access Restrictions
• No HTML file• Operating System• main()• Not necessary• Constructor• Border Layout• Titles• No Restrictions
Basic Java : Applets6
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 : Applets7
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 : Applets8
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 : Applets9
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 : Applets10
Applet Life Cycle
• init()• start()• stop()• destroy()• paint(Graphics g)
Basic Java : Applets11
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 : Applets12
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 : Applets13
public void stop()
• Automatically invoked whenever user moves off the page on which the applet resides.
• Works in partnership with start().
Basic Java : Applets14
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 : Applets15
<applet> attributes
• CODE• CODEBASE• WIDTH / HEIGHT• ALIGN• HSPACE / VSPACE• NAME• <PARAM> tag
Basic Java : Applets16
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 : Applets17
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 : Applets18
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 : Applets19
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 : Applets20
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 : Applets21
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.