Applets - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

Applets

Description:

Applets Life Cycle Drawing and Event Handling Methods for UI Components Applet Capabilities Example – PowerPoint PPT presentation

Number of Views:75
Avg rating:3.0/5.0
Slides: 29
Provided by: Russell175
Category:
Tags: applets | controls

less

Transcript and Presenter's Notes

Title: Applets


1
Applets
  • Life Cycle
  • Drawing and Event Handling
  • Methods for UI Components
  • Applet Capabilities
  • Example

2
Life Cycle
  • Loading the applet
  • instance of applet subclass is created
  • applet initializes itself init() (like
    constructor)
  • applet starts running start() (execution,
    except user input)
  • Leave and Come Back
  • stopping stop()
  • restarting start()
  • iconification minimize

3
  • Reloading the Applet
  • Quitting the Browser
  • stop()
  • final cleanup destroy()
  • init, start, stop, destroy methods

4
(No Transcript)
5
Drawing and Event Handling
  • paint() Basic display method.
  • update() method used with paint() to improve
    performance
  • Objects notified of registered events (listen for
    them)
  • Register event by implementing appropriate
    interface (mouseListener)

6
(No Transcript)
7
Pre-Made AWT UI Components
  • Buttons (java.awt.Button)
  • Checkboxes (java.awt.Checkbox)
  • Single-line text fields (java.awt.TextField)
  • Larger text display and editing areas
    (java.awt.TextArea)
  • Labels (java.awt.Label)
  • Lists (java.awt.List)
  • Pop-up lists of choices (java.awt.Choice)
  • Sliders and scrollbars (java.awt.Scrollbar)
  • Drawing areas (java.awt.Canvas)
  • Menus (java.awt.Menu, java.awt.MenuItem,
    java.awt.CheckboxMenuItem)
  • Containers (java.awt.Panel, java.awt.Window and
    its subclasses)

8
Adding UI Components
  • add adds the component to the applet
  • remove removes the component
  • setLayout sets the applets layout manager which
    controls the position and size of the applet
    components

9
(No Transcript)
10
Working with Applets
  • Cannot load libraries or define native methods
  • Cannot read or write files on local host
  • Cannot make network connections
  • Cannot start a program on local host
  • Cannot read system properties on local host
  • Implemented with SecurityManager which throws
    SecurityExceptions

11
Applets Can...
  • Display html
  • If local, restrictions off
  • make network connections back to host they came
    from
  • Dont have to stop when you leave

12
ltAPPLET CODE AppletSubclass.class WIDTHanInt
HEIGHTanIntgt lt/APPLETgt
13
Finding and Loading Data Files
14
  • Applet.getCodeBase() returns URL of directory
    from which applet classes load
  • Applet.getDocumentBase() returns URL of directory
    from which applets html came
  • Image imagegetImage(getCodeBase(),"imgDir/a.gif")

15
Showing Status
  • Applet.showStatus(String s)
  • showStatus("MyApplet Loading image file "
    file)

16
Displaying Documents
  • AppletContext class allows applet to interact
    with context (i.e. browser) in which it runs.
  • Display in browser (wont work with appletviewer)
  • public void showDocument(java.net.URL url)
  • public void showDocument(java.net.URL url, String
    targetWindow)

17
"_blank" Display the document in a
new, nameless window. "windowName"
Display the document in a window named
windowName. This window is created if
necessary. "_self" Display the
document in the window and frame that contain
the applet. "_parent" Display
the document in the applet's window but in the
parent frame of the applet's frame. If the applet
frame has no parent frame, this acts the
same as "_self". "_top"
Display the document in the applet's window but
in the top-level frame. If the applet's frame
is the top-level frame, this acts the same as
"_self".
18
...//In an Applet subclass
urlWindow new URLWindow(getApplet
Context()) . . .
class URLWindow extends Frame . .
. public URLWindow(AppletContext
appletContext) . . .
this.appletContext appletContext
. . .
. . . public boolean action(Event
event, Object o) . . .
String urlString / user-entered
string / URL url null
try
url new URL(urlString)
catch (MalformedURLException e)
...//Inform the user and return...
if
(url ! null) if (/
user doesn't want to specify the window /)
appletContext.showDocume
nt(url) else
appletContext.showDocument
(url,
/ user-specified window /)

. . .
19
Sending Messages to Other Applets
  • Same host, same directory, same page
  • Find by name on same page
  • Two ways to name applet

20
1) By specifying a NAME attribute within the
applet's ltAPPLETgt tag. For example
ltAPPLET CODEBASEexample/ CODESender.class
WIDTH450
HEIGHT200 NAME"buddy"gt
. . . lt/appletgt 2) By
specifying a NAME parameter with a ltPARAMgt tag.
For example ltAPPLET
CODEBASEexample/ CODEReceiver.class
WIDTH450 HEIGHT35gt
ltPARAM NAME"name" value"old pal"gt
. . . lt/appletgt
21
Applet receiver null String receiverName
nameField.getText() //Get name to search
for. receiver getAppletContext().getApplet(recei
verName)
if (receiver ! null) //Use the
instanceof operator to make sure the applet
//we found is a Receiver object.
if (!(receiver instanceof Receiver))
status.appendText("Found applet named
"
receiverName ", "
"but it's not a Receiver object.\n")
else
status.appendText("Found applet named "
receiverName ".\n"
" Sending
message to it.\n") //Cast the
receiver to be a Receiver object
//(instead of just an Applet object) so that
the //compiler will let us call
a Receiver method.
((Receiver)receiver).processRequestFrom(myName)
. . .
22
Playing Sound
AudioClip onceClip, loopClip onceClip
applet.getAudioClip(getCodeBase(), "bark.au")
loopClip applet.getAudioClip(getCodeBas
e(), "train.au") onceClip.play()
//Play it once. loopClip.loop()
//Start the sound loop.
loopClip.stop() //Stop the sound loop.
23
Using the Applet Tag
  • Like command line arguments for applets
  • Customize applet operation
  • User Configures Names
  • Provide Default Values
  • Parameter values are all strings
  • Let applet interpret string

24
ltAPPLET CODEAppletButton.class CODEBASEexample
WIDTH350 HEIGHT60gt
ltPARAM NAMEwindowClass VALUEBorderWindowgt
ltPARAM NAMEwindowTitle VALUE"BorderLayout"
gt ltPARAM NAMEbuttonText
VALUE"Click here to see a BorderLayout in
action"gt lt/APPLETgt
25
int requestedWidth 0 . . .
String windowWidthString getParameter("WINDOWWID
TH") if (windowWidthString ! null)
try
requestedWidth Integer.parseInt(windowWidthStrin
g) catch (NumberFormatException
e) //Use default width.

26
String windowClass String buttonText
String windowTitle int
requestedWidth 0 int requestedHeight
0 . . . public void init()
windowClass getParameter("WINDOW
CLASS") if (windowClass null)
windowClass "TestWindow"
buttonText
getParameter("BUTTONTEXT") if
(buttonText null)
buttonText "Click here to bring up a "
windowClass
windowTitle getParameter("WINDOWTITLE")
if (windowTitle null)
windowTitle windowClass
String windowWidthString
getParameter("WINDOWWIDTH") if
(windowWidthString ! null)
try requestedWidth
Integer.parseInt(windowWidthString)
catch (NumberFormatException e)
//Use default width.
...
27
Combining Applet Files
  • If applet has more than one file, combine into
    zip archive
  • jar tool and JAR format from java
  • jar cvf file.zip com/mycompany/myproject/.class
    .gif (Solaris)
  • jar cvf file.zip com\mycompany\myproject\.class
    .gif (Windows 95/NT)

28
ltAPPLET CODE"AppletSubclass.class"
ARCHIVE"file1, file2"
WIDTHanInt HEIGHTanIntgt lt/APPLETgt
Write a Comment
User Comments (0)
About PowerShow.com