1 / 21

Objectives of This Session

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

kawamura
Download Presentation

Objectives of This Session

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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.

  3. Applet Hierarchy Object Component Container Panel Applet

  4. 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

  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.

  6. Applet Demo import java.awt.*; import java.applet.*; class MyApplet extends Applet { public void paint(Graphics g) { g.drawString(“Hello world”,50,50); } }

  7. Html Demo File <HTML> <HEAD> <TITLE> My first applet </TITLE> </HEAD> <BODY> <APPLET CODE = “MyApplet.class” WIDTH = 200 HEIGHT = 200> </APPLET> </BODY> </HTML>

  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.

  9. Applet Life Cycle • init() • start() • stop() • destroy() • paint(Graphics g)

  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.

  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.

  12. public void stop() • Automatically invoked whenever user moves off the page on which the applet resides. • Works in partnership with start().

  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.

  14. <applet> attributes • CODE • CODEBASE • WIDTH / HEIGHT • ALIGN • HSPACE / VSPACE • NAME • <PARAM> tag

  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

  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>

  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); }

  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.

  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.

  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.

More Related