Practical Considerations when Writing Applets |
This page discusses the few issues that are particular to applet GUIs. Some of the information in this section might not make sense until you've read the Creating a User Interface trail. That trail discusses all the GUI concepts referred to on this page and gives many examples of applet GUIs.
- An
Applet
is aPanel
.- Because
Applet
is a subclass of the AWTPanel
class, applets can contain otherComponents
, just as anyPanel
can.Applet
s inheritPanel
's default layout manager:FlowLayout
. AsPanel
s (and thusComponent
s),Applet
s participate in the AWT drawing and event hierarchy.
- Applets appear in pre-existing browser windows.
- This has two implications. First, unlike GUI-based applications, applets don't have to create a window to display themselves in. They can, if they have a good reason, but they often just display themselves within the browser window. Second, depending on the browser implementation, your applet's components might not be shown unless your applet calls
validate
after adding components to itself. Fortunately, callingvalidate
can't hurt.
- The applet background color might not match the page color.
- By default, applets have a light gray background color. HTML pages, however, can have other background colors and can use background patterns. If the applet designer and page designer aren't careful, the applet's different background color can cause it to stick out on the page or cause noticeable flashing when the applet is drawn. One solution is to define an applet parameter that specifies the applet's background color. The
Applet
subclass can useComponent
'ssetBackground
method to set its background to the user-specified color. Using the background color parameter, the page designer can choose an applet color that works well with the page colors. You'll learn about parameters in Defining and Using Applet Parameters, and about theComponent
class and drawing in the Creating a User Interface trail.
- Each applet has a user-specified, pre-determined size.
- Because the
<APPLET>
tag requires that the applet's width and height be specified, and because browsers don't necessarily allow applets to resize themselves, applets must make do with a fixed amount of space that might not be ideal. Even if the amount of space is ideal for one platform, the platform-specific parts of the applet (such as buttons) might require a different amount of space on another platform. You can compensate by recommending that pages that include your applet specify a little more space than might be necessary, and by using flexible layouts, such as the AWT-providedGridBagLayout
andBorderLayout
classes, that adapt well to extra space.
- Applets load images using the
Applet getImage
methods.- The
Applet
class provides a convenient form ofgetImage
that lets you specify a base URL as one argument, followed by a second argument that specifies the image file location, relative to the base URL. TheApplet getCodeBase
andgetDocumentBase
methods provide the base URLs that most applets use. Images that an applet always needs, or needs to rely on as a backup, are usually specified relative to where the applet's code was loaded from (the code base). Images that are specified by the applet user (often with parameters in the HTML file) are usually relative to the page that includes the applet (the document base).
- Applet classes (and often the data files they use) 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. 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.For example, the
AppletButton
classstart
method launches a thread that gets theClass
object for the window the button brings up. The applet's main purpose in doing so is to make sure the class name the user specified is valid. An added benefit is that getting theClass
object forces the class file to be loaded before the class is instantiated. When the user requests that the window be created, the applet instantiates the window class much quicker than if the applet still had to load the window class file.
Practical Considerations when Writing Applets |