Java Applets - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Java Applets

Description:

There are security implications when running applets. Specifically, they can't access ... JLabel('Greetings', SwingConstants.CENTER); contentPane.add(label) ... – PowerPoint PPT presentation

Number of Views:50
Avg rating:3.0/5.0
Slides: 23
Provided by: michaelv153
Category:

less

Transcript and Presenter's Notes

Title: Java Applets


1
Java Applets
2
What is an Applet?
  • Its a Java application that is designed to be
    downloaded from the Internet and run in a browser
  • There are security implications when running
    applets
  • Specifically, they cant access protected
    resources
  • More about applet security later in the course

3
How an Applet Works
  • Embed code in an HTML document to tell the
    browser
  • Where to get the Java class files
  • How the applet should appear on a Web page
  • An HTML document may have multiple applets
  • Install the Java Plug-In to ensure maximum
    compatibility
  • This Plug-In is supported by Sun rather than the
    browser vendor

4
Applet Essentials
  • An Applet is just a class that derives from
    JApplet
  • Create a method named init() which contains any
    initialization code
  • init() is of type void
  • Init accepts no arguments
  • From there, create visual components as you would
    with any Swing application
  • As always, class name must match the file name
  • Thats really all there is to it

5
Creating a First Applet (Example)
  • import java.awt.
  • import javax.swing.
  • public class FirstApplet extends JApplet
  • public void init()
  • Container contentPane getContentPane()
  • JLabel label new
  • JLabel("Greetings",
  • SwingConstants.CENTER)
  • contentPane.add(label)

6
Compiling and Deploying an Applet
  • Compile the Java Applet into a class
  • Call javac or use Netbeans to compile the source
    file
  • Copy the class file(s) to the Web server as
    necessary

7
Viewing an Applet
  • Embed tags into an HTML document
  • Older ltappletgt tag has attributes to define an
    applet
  • Newer ltobjectgt tag is part of HTML 4.0 and
    defined by the W3C
  • Use the appletviewer program for testing
  • Example
  • appletviewer FirstApplet.html

8
The ltappletgt Tag
  • code attribute supplies the named of the applets
    class file
  • Standard package directory conventions apply
  • codebase attribute is used to supply relative
    directory references
  • width and height attributes define the applets
    size when rendered in the browser
  • name attribute is used in scripting to reference
    the applet programmatically
  • align attribute controls how the applet is
    aligned about the surrounding text
  • See Figure 10-6 on page 503
  • Plain text is displayed when the browser cannot
    display an applet

9
Relative Directories and the codebase attribute
  • codebase attribute specifies relative directory
    containing class files
  • Use to keep applets organized
  • Name corresponds to directory name containing the
    class file(s)

10
The ltobjectgt Tag
  • Similar semantics to the ltappletgt tag
  • classid attribute contains the name of the class
    in the form
  • ClassidjavaFirstApplet.class
  • codetype attribute contains application/java

11
Sample HTML Applet Code (1)
  • lthtmlgt
  • ltbodygt
  • ltapplet code"FirstApplet.class"
  • width"300" height"300"gt
  • Browser cannot display applets
  • lt/appletgt
  • lt/bodygt
  • lt/htmlgt

12
Sample HTML Applet Code (2)
  • lthtmlgt
  • ltbodygt
  • ltobject
  • codetypeapplication.java
  • classidjavaCalculatorApplet.class
  • width"300" height"300"gt
  • lt/bodygt
  • lt/htmlgt

13
Passing Information to an Applet (1)
  • Embed the ltparamgt tag inside of the applet
  • Use one ltparamgt tag for each parameter
  • name attribute contains the parameter name
  • value attribute contains the parameters value
  • Note that parameters are returned as a String
    data type
  • If a parameter value is not found, null is
    returned

14
Passing Information to an Applet (2)
  • Call getParameter(name) in init() to retrieve a
    parameters value
  • Call GetParameterInfo() to return a 2 dimensional
    array of parameters
  • Values for each row are
  • Parameter name
  • Parameter type
  • Parameter description

15
Passing information to an Applet (HTML Example)
  • lthtmlgt
  • ltbodygt
  • ltapplet code"FirstApplet.class"
  • width"300" height"300"gt
  • ltparam name"version" value"1.0.0"/gt
  • lt/appletgt
  • lt/bodygt
  • lt/htmlgt

16
Getting a Parameter Value (Example)
  • public class FirstApplet extends JApplet
  • public void init()
  • String version getParameter("version")

17
Applet Life Cycle
  • Four methods of the Applet class control applet
    initialization and destruction
  • init
  • start
  • stop
  • destroy

18
Applet (init)
  • Conceptually similar to a constructor
  • Init runs when the JVM executes the applet the
    first time
  • Parameter values are typically passed here
  • Note that applets to have constructors but we
    seldom use them

19
Applet (start)
  • Start is called after init
  • Start is called when the user navigates back to a
    page containing the applet
  • Restart an animation for example
  • Think of start as a GotFocus or Enter event

20
Applet (stop)
  • Stop is called when the user navigates to another
    page
  • Create code to suspend visual animations or other
    resource intensive activity

21
Applet (destroy)
  • Destroy is called when the browser closes the
    window containing the applet

22
Communication with the Browser
  • An applet runs inside of a browser
  • Call getAppletContext() to get information from
    the ambient browser
Write a Comment
User Comments (0)
About PowerShow.com