Creating an applet user interface
This presentation is the property of its rightful owner.
Sponsored Links
1 / 30

Creating an Applet User Interface PowerPoint PPT Presentation


  • 67 Views
  • Uploaded on
  • Presentation posted in: General

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.

Download Presentation

Creating an Applet User Interface

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Creating an applet user interface

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.


Creating an applet user interface1

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.


Creating an applet user interface2

Creating an Applet User Interface

  • Applets can get system information by reading systemproperties.

  • 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 standardoutput or standarderrorstream.


An applet is a panel

An Applet is a Panel.

  • Because Applet is a subclass of the AWTPanel 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.


Applets appear in pre existing browser windows

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.


The applet background color might not match the page color

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.


Each applet has a user specified pre determined size

Each applet has a user-specified, pre-determined size.

  • Because the <APPLET> 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.


Applets load images using the applet getimage methods

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


Applet classes are loaded over the network which can be slow

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


Playing sounds

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.


Sound related methods

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


Sound related methods1

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.


Defining and using applet parameters

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.


Deciding which parameters to support

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?


What should the applet let the user configure

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

  • Similarly, applets that play sounds might have parameters to specify the sounds.


What should the applet let the user configure1

What Should the Applet Let the User Configure?

  • Besides parameters that specify resourcelocations (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.


What should the parameters be named

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


What should the parameters be named1

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


What kind of value should each parameter take

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


What should the default values of parameters be

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.


An example appletbutton

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 labelofthebutton that brings up the window

  • The source code is here.


Writing the code to support parameters

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


An example appletbutton1

An Example: AppletButton

  • Here's what a typical <APPLET> tag for AppletButton looks like:

    <APPLET CODE=AppletButton.class CODEBASE=example WIDTH=350 HEIGHT=60>

    <PARAM NAME=windowClass VALUE=BorderWindow>

    <PARAM NAME=windowTitle VALUE="BorderLayout">

    <PARAM NAME=buttonText VALUE="Click here to see a BorderLayout in action">

    </APPLET>

  • When the user doesn't specify a value for a parameter, AppletButton uses a reasonable default value.


Giving information about parameters

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


Reading system properties

Reading System Properties

  • To find out about the current working environment, applets can read systemproperties.

  • Systemproperties are key/valuepairs.

  • Applets can read some, but not all, system properties.

  • An applet uses System class method getProperty() to read systemproperties.

    String s = System.getProperty("os.name");


Reading system properties1

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., ":")


Forbidden system properties

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


Displaying short status strings

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


Displaying diagnostics to the standard output and error streams

Displaying Diagnostics to the Standard Output and Error Streams

  • Displaying diagnostics to the standardoutput and errorstreams 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.


Displaying diagnostics to the standard output and error streams1

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 + ")");

    }


  • Login