Creating an Applet User Interface - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

Creating an Applet User Interface

Description:

This is a natural consequence of the fact that each applet appears within an ... PARAM NAME=buttonText VALUE='Click here to see a BorderLayout in action' /APPLET ... – PowerPoint PPT presentation

Number of Views:55
Avg rating:3.0/5.0
Slides: 31
Provided by: tzone
Category:

less

Transcript and Presenter's Notes

Title: Creating an Applet User Interface


1
Creating an Applet User Interface
  • Most applets have a graphical user interface
    (GUI).
  • This is a natural consequence of the fact that
    each applet appears within an HTML page that's
    displayed by a browser.
  • Applet class is a subclass of the AWT (Abstract
    Window Toolkit) Panel class, and thus
    participates in the AWT event and drawing model.
  • Creating an applet's GUI is just as easy as
    creating an application's GUI.

2
Creating an Applet User Interface
  • An applet can mix and match several UI types.
  • Some applets play sounds, either to give the user
    feedback or to provide ambiance.
  • Applets can get configuration Information from
    the user through parameters that the applet
    defines.

3
Creating an Applet User Interface
  • Applets can get system information by reading
    system properties.
  • To give text information to the user, an applet
    can use its GUI or (for text that's not crucial)
    display a short status string (for debugging
    text) to the standard output or standard error
    stream.

4
An Applet is a Panel.
  • Because Applet is a subclass of the AWT Panel
    class, applets can contain other Components, just
    as any Panel can.
  • Applets inherit Panel's default layout manager
    FlowLayout.
  • As Panels (and thus Components), Applets
    participate in the AWT drawing and event
    hierarchy.

5
Applets appear in pre-existing browser windows.
  • Unlike GUI-based applications, applets don't have
    to create a window to display themselves in.
  • Depending on the browser implementation, your
    applet's components might not be shown unless
    your applet calls validate() after adding
    components to itself.
  • Fortunately, calling validate() can't hurt.

6
The applet background color might not match the
page color.
  • By default, applets have a light gray background
    color, which might disturb HTML pages background
    colors or patterns.
  • One solution is to define an applet parameter
    that specifies the applet's background color.
  • The Applet subclass can use Component's
    setBackground() method to set its background to
    the user-specified color.

7
Each applet has a user-specified, pre-determined
size.
  • Because the ltAPPLETgt tag requires that the
    applet's width and height be specified.
  • The platform-specific parts of the applet (such
    as buttons) might require a different amount of
    space on another platform.
  • Using flexible layouts, such as the AWT-provided
    GridBagLayout and BorderLayout classes to adapt
    to extra space.

8
Applets load images using the Applet getImage()
methods.
  • The Applet class provides a convenient form of
    getImage() that lets you specify the image file
    location.
  • The Applet getCodeBase() and getDocumentBase()
    methods provide the base URLs that most applets
    use.
  • Images are usually specified relative to where
    the applet's code was loaded from (the code base)
    or to the page that includes the applet (the
    document base).

9
Applet classes are loaded over the network, which
can be slow.
  • Applets can do several things to decrease the
    perceived startup time.
  • The Applet subclass can be a small one that
    immediately displays a status message.
  • And, if some of the applet's classes or data
    aren't used right away, the applet can preload
    the classes or data in a background thread.

Here is an example
10
Playing Sounds
  • In the Java Applet package (java.applet), the
    Applet class and AudioClip interface provide
    basic support for playing sounds.
  • Currently, the Java API supports only one sound
    format Sun ".au" files.
  • You can convert files from other sound formats
    using an audio format conversion program.

11
Sound-Related Methods
  • The two-argument form of each method takes a base
    URL and the location of the sound file relative
    to the base URL.
  • getAudioClip(URL), getAudioClip(URL, String)
  • Return an object that implements the AudioClip
    interface.
  • play(URL), play(URL, String)
  • Play the AudioClip corresponding to the specified
    URL.

Here is an example
12
Sound-Related Methods
  • The AudioClip interface defines the following
    methods
  • loop()
  • Starts playing the clip repeatedly.
  • play()
  • Plays the clip once.
  • stop()
  • Stops the clip. Works with both looping and
    one-time sounds.

13
Defining and Using Applet Parameters
  • Parameters are to applets what command-line
    arguments are to applications.
  • They allow the user to customize the applet's
    operation.
  • By defining parameters, you can increase your
    applet's flexibility, making your applet work in
    multiple situations without recoding and
    recompiling it.

14
Deciding Which Parameters to Support
  • When implementing parameters, you must answer
    four questions
  • What should the applet let the user configure?
  • What should the parameters be named?
  • What kind of value should each parameter take?
  • What should the default value of each parameter
    be?

15
What Should the Applet Let the User Configure?
  • The parameters an applet should support depend on
    what the applet does and on how flexible it to
    be.
  • Applets that display images might have parameters
    to specify the image locations.
  • Similarly, applets that play sounds might have
    parameters to specify the sounds.

16
What Should the Applet Let the User Configure?
  • Besides parameters that specify resource
    locations (such as image and sound files),
    applets sometimes provide parameters for
    specifying details of the applet's appearance or
    operation.
  • For example, an animation applet might let the
    user specify the number of images shown per
    second.
  • Or an applet might let the user change the
    strings the applet displays.

17
What Should the Parameters Be Named?
  • SOURCE or SRC
  • For a data file such as an image file.
  • XXXSOURCE (for example, IMAGESOURCE)
  • Used in applets that let the user specify more
    than one type of data file.
  • XXXS
  • For a parameter that takes a list of XXXs (where
    XXX might be IMAGE, again).
  • NAME
  • Used only for an applet's name. Applet names are
    used for interapplet communication

18
What Should the Parameters Be Named?
  • Parameter names are case insensitive. For
    example, IMAGESOURCE and imageSource both refer
    to the same parameter.
  • Parameter values, on the other hand, are case
    sensitive unless you take steps interpret them
    otherwise (such as by using the String
    toLowerCase() method before interpreting the
    parameter's value).

19
What Kind of Value Should Each Parameter Take?
  • Parameter values are all strings.
  • Applets typically interpret a parameter value as
    one of the following types
  • A URL
  • An integer
  • A floating-point number
  • A boolean value -- typically "true"/"false" or
    "yes"/"no"
  • A string -- for example, the string to use as a
    window title
  • A list of any of the above

20
What Should the Default Values of Parameters Be?
  • Applets should attempt to provide useful default
    values for each parameter.
  • So that the applet will execute even if the user
    doesn't specify a parameter or specifies it
    incorrectly.
  • For example, an animation applet should provide a
    reasonable setting for the number of images it
    displays per second.

21
An Example AppletButton
  • The AppletButton class is so flexible because it
    defines parameters to let an user specify any or
    all of the following
  • The type of window to bring up
  • The window's title
  • The window's height
  • The window's width
  • The label of the button that brings up the window
  • The source code is here.

22
Writing the Code to Support Parameters
  • Applets use the Applet getParameter() method to
    get user-specified values for applet parameters.
  • The getParameter() method is defined as follows
  • public String getParameter(String name)
  • The java.lang package provides classes such as
    Integer that you can use to help with converting
    strings to primitive types.

23
An Example AppletButton
  • Here's what a typical ltAPPLETgt tag for
    AppletButton looks like
  • 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
  • When the user doesn't specify a value for a
    parameter, AppletButton uses a reasonable default
    value.

24
Giving Information about Parameters
  • The applet's documentation should describe each
    parameter and give the user examples and hints of
    setting them
  • One should also implement the getParameterInfo()
    method so that it returns information about your
    applet's parameters.
  • Below is an example of implementing the
    getParameterInfo() method

25
Reading System Properties
  • To find out about the current working
    environment, applets can read system properties.
  • System properties are key/value pairs.
  • Applets can read some, but not all, system
    properties.
  • An applet uses System class method getProperty()
    to read system properties.
  • String s System.getProperty("os.name")

26
Reading System Properties
  • Applets can read the following system properties
  • Key Meaning
  • ----------------------- --------------------
    ----------
  • "file.separator" File separator (e.g.,
    "/")
  • "java.class.version" Java class version
    number
  • "java.vendor" Java vendor-specific
    string
  • "java.vendor.url" Java vendor URL
  • "java.version" Java version number
  • "line.separator" Line separator
  • "os.arch" Operating system
    architecture
  • "os.name" Operating system name
  • "path.separator" Path separator (e.g.,
    "")

27
Forbidden System Properties
  • For security reasons, no existing browsers or
    applet viewers let applets read the following
    system properties.
  • Key Meaning
  • ------------------------ --------------------
    ------------------
  • "java.class.path Java classpath
  • "java.home" Java installation
    directory
  • "user.dir" User's current working
    directory
  • "user.home" User home directory
  • "user.name" User account name

28
Displaying Short Status Strings
  • Applets can display short status strings on the
    status line of the application in which they're
    running.
  • Applets display status lines with the
    showStatus() method.
  • Here's an example of its use
  • showStatus("MyApplet Loading image file "
    file)

29
Displaying Diagnostics to the Standard Output and
Error Streams
  • Displaying diagnostics to the standard output and
    error streams can be an invaluable tool when
    you're debugging an applet.
  • Applets display to the standard output stream
    using System.out.print(String) and
    System.out.println(String).
  • Displaying to the standard error stream is
    similar just specify System.err instead of
    System.out.

30
Displaying Diagnostics to the Standard Output and
Error Streams
  • Here's an example of displaying to the standard
    output
  • //Where instance variables are declared
  • boolean DEBUG true
  • . . .
  • //Later, when we want to print some status
  • if (DEBUG)
  • System.out.println("Called someMethod(" x
    "," y ")")
Write a Comment
User Comments (0)
About PowerShow.com