210 likes | 224 Views
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
E N D
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
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.
Applet Hierarchy Object Component Container Panel Applet
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 Applet Vs Applications
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.
Applet Demo import java.awt.*; import java.applet.*; class MyApplet extends Applet { public void paint(Graphics g) { g.drawString(“Hello world”,50,50); } }
Html Demo File <HTML> <HEAD> <TITLE> My first applet </TITLE> </HEAD> <BODY> <APPLET CODE = “MyApplet.class” WIDTH = 200 HEIGHT = 200> </APPLET> </BODY> </HTML>
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.
Applet Life Cycle • init() • start() • stop() • destroy() • paint(Graphics g)
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.
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.
public void stop() • Automatically invoked whenever user moves off the page on which the applet resides. • Works in partnership with start().
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.
<applet> attributes • CODE • CODEBASE • WIDTH / HEIGHT • ALIGN • HSPACE / VSPACE • NAME • <PARAM> tag
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
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>
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); }
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.
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.
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.