Introduction to Applets - PowerPoint PPT Presentation

About This Presentation
Title:

Introduction to Applets

Description:

Applets are loaded from remote computer and executed locally. ... FONT SIZE=5 STYLE='font-size: 20pt' CS II Web-page to Display an Applet /FONT /P ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 11
Provided by: markf91
Learn more at: http://www.cs.uni.edu
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Applets


1
Introduction to Applets
  • Chapter 21

2
Applets
  • An applet is a Java application that is intended
    to be invoked and executed through a Web browser.
  • Click Here To View Cannon World Applet

3
Security Issues
  • Applets are loaded from remote computer and
    executed locally. Since an untrusted stranger
    wrote the applet, applets are restricted.
    Applets CANNOT
  • run any local executable program
  • read or write to local computers file system
  • Communicate with any computer except originating
    server
  • Access many facts about the local computer,
    except name and version of OS

4
Applets vs. Applications
  • Much of the code for Applets is similar to
    Applications
  • Differences
  • Applets are created by subclassing Applet,
    instead of Frame
  • Applets begin execution with the method init,
    instead of main
  • Applets are halted and restarted as the Web
    browser moves to a new page and returns

5
Applet class
  • Applet is a subclass of Panel, thus it inherits
    all of Panels graphical component attributes.
  • Applets have four methods available for
    overriding by clients
  • init() called only once when the applet is first
    loaded (use like the constructor)
  • start() called to begin execution each time the
    Web-page containing the applet is exposed
  • stop() called each time the Web-page containing
    the applet is hidden
  • Destroy() called when the applet is about to be
    terminated (should free any resource being used)

6
CannonWorld as an Applet
  • import java.applet. import java.awt.
    import java.awt.event. public class
    CannonWorld extends Applet // was Frame
    public static final int FrameWidth 600
    public static final int FrameHeight 400
    private int angle private
    String message private CannonBall
    cannonBall private Scrollbar slider
    // public CannonWorld() public
    void init() // changed the layout
    manager setLayout(new BorderLayout())
    setSize ( FrameWidth, FrameHeight
    ) // setTitle( "Cannon Game" )
    angle 45 message
    "Angle " angle

7
Http and Applets
  • ltHTMLgtltHEADgt ltMETA HTTP-EQUIV"CONTENT-TYPE"
    CONTENT"text/html charsetwindows-1252"gt ltTITLE
    gtCannon World Applet Demolt/TITLEgtlt/HEADgtltBODY
    LANG"en-US" DIR"LTR"gtltP ALIGNCENTERgtltFONT
    SIZE5 STYLE"font-size 20pt"gtCS II Web-page to
    Display an Appletlt/FONTgtlt/PgtltP
    ALIGNCENTERgtltBRgtltBRgtlt/PgtltP ALIGNCENTERgtltAPPLET
    CODEBASE"http//www.cs.uni.edu/fienup/cs062s06/
    sessions/session24/CannonWorld_Applet/"
    CODE"CannonWorldApplet"
    ALIGNLEFT WIDTH600 HEIGHT400gtlt/APPLETgtltBR
    CLEARLEFTgtltBRgtltBRgtlt/BODYgtlt/HTMLgt

8
Parameters in ltappletgt tag
  • ltAPPLET CODEBASE"http//www.cs.uni.edu"
    CODE"CannonWorldApplet"
  • ALIGNLEFT WIDTH600 HEIGHT400gt ltPARAM
    namename1 valuevalue1gt
  • You do not have a Java enabled browser
  • lt/APPLETgt
  • In the Java Applet, getParameter(name1) can be
    used to access the parameter string.

9
Loading Resources from the Server
  • getImage(URL) retrieve the image specified by
    the URL (jpeg and gif)
  • getAudioClip(URL) return audioClip object, then
    play it (or play(URL) short cut)
  • getCodeBase() returns URL for codebase
  • appletContext.showDocument(URL) instructs the Web
    browser to display the a new page from the URL

10
URL objects
  • Constructed from a string or previous URL and a
    string
  • URL have an openStream() method which returns an
    inputStream object, so you can read from a URL
    just like reading from a file.
Write a Comment
User Comments (0)
About PowerShow.com