The Applet’ Class Java Help

The Applet’ Class

This chapter examines the Applet class, which .provides the necessary support for applets. In Chapter 12, you were Introduced to the general form’ ofan applet and
the steps necessary to compile and run one. In this chapter, we will look at  applets in detail The Applet class is contained in the ava.applet package, Applet contains several methods that give you detailed control over the execution of your applet. In addition, java.applet also defines three interfaces: Applet Context, Audio Clip, and Applet Stub. Let’s begin by reviewing the basic elements of an appIet and the steps necessary to create and test one.

Applet Basics

All applets are sub classes of Applet. Thus, all applets must import java.applet.Applets . must also import  Recall that AWT stands (or the Abstract Window toolkit Since all applets run-in a window, it is necessary to include support for that win Applets are not executed by the console-based Java run-time interpreter. Rather, they are executed by either a Web browser or an applet viewer. The figures shown in this chapter were, created with the standard applet viewer, called apple tv provided by the . But you can use any applet viewer or browser you like.  Execution of an applet does not begin at main] ). Actually, few applets even ha  methods. Instead, execution of an applet is started and controlled with an . entirely different mechanism, which will be explained shortly. Output to your applet’s window is not performed by S Rather, it is handled with methods, such as  which outputs a string to a specified location. Input is also handled differently than in an application.  an applet has been compiled, it is’ included in an HTML file using. the  The applet will be executed by a web browser when it encounters the  tag within.the  To view and test an applet more, conveniently, simply comment at the head of your Java source code file that contains the This way, your code is documented with the necessary  statements needed-by your applet, and you can test the compiled applet by . starting the applet viewer with your Java source code ‘file specified as the target. Here is an example of such a comment.

 The Applet Class

The Applet class defines the methods shown in Table 19-1. Applet provides all necessary support for applet execution, such as starting and slopping. It also provides methods that load and display images, and methods that load and play audio dips. . Apple extends the AWT class Panel. In turn, Panel extends Container, which extends· Component. The sec lasses provide support for Java’s window-based, graphical interface, Thus, Applet provides all of the necessary support for window-based activities. (The  described in detail in following chapters.)


An applet is a window-based program. As such, its architecture is different from the
so-called normal, console-based programs shown in the first part of this book. If you
“are familiar with Windows programming, you will be right at home writing applets.
not, then there are a few key concepts you must understand. First, applets are event driven. Although we won’t examine event handling until , the following chapter, it is important to understand in a general way how the event-driven architecture impacts the design of an applet. An applet resembles a set of  interrupt service routines. Here is how the process’ works. An applet waits until an event occurs. The AWT notifies the applet about an event by calling an event handler that has been provided by the applet. Once this happens, the applet must take appropriate action and then quickly return control to the AWT. This is a crucial point. . For the most part, your applet should not enter a “mode” of operation in which it maintains control for an extended period. Instead, it must perform specific actions in response to events and then return control to the AWT run-time system. In those situations in which your applet needs to perform a repetitive task on its own (for example, displaying a scrolling message across its window), you must start an additional thread of execution. {You will see an example later in this chapter.Second, the user initiates interaction with an not the other way around. As . you know, in a program, when the program needs input, it will prompt the user and then call some input method, ‘such as This is not the way it  works in an applet. Instead, the user interacts with the applet as he or she wants, when he or she wants. These interactions are sent to the applet as events to which the applet must respond. For example, when the user clicks a  the applet’s window, a mouse event Is generated. If  user presses a key while the  window has input focus, a  event is generated. As you will see in Inter chapters, applets can contain various controls, such as push buttons and check boxes. When the user’ interacts with one of these controls, an event is generated, While the architecture of an is not us easy to understand as that of a console-based how intimidating that environment can be. Fortunately provides a much cleaner approach that is more quickly mastered.

An Applet Skeleton

All but the most trivial applets override a set of methods that provides, the basic
mechanism by which the browser  applet viewer interfaces to the applet and controls, its execution. Four of these  start(), stop(), and destroy(  defined by Applet, Another,  is defined by the AWT Component class. Default implementations 1’01’ all of these methods <Ire provided. Applets do need to override those methods they do not use, However, only very simple applets will not .

.Applet Initialization “and Termination

It is important to understand the order in which the various methods shown in the
skeleton are called. When an applet begins, the Awr calls the following methods, in
this sequence:

1. init( )
2. start( )
3. paint()
When an applet is terminated, the following sequence of method calls takes place;
1. stop
2. destory
Let’s look more closely at these methods.
Inlt( )
The init method is the first method to be called. This is where you should initialize
variables. This  is called only once during the run time of your applct,
start( )
The start() method is called after inH( ). It is also called to restart an applet after it has
been stopped. Whereas init( ) is called once-the first time an applet is loaded-start( )
is called each time an applet’s HTML document is displayed onscreen. So, if a user
leaves a web page and comes .back, the applet resumes execution at start( I.
palnt( ) .
The paint( ) method is called each time your applet’s output must be redrawn. This
situation can occur for several reasons. For example, the window in which the applet is running may be overwritten by another window and then uncovered. Or the applet window may be minimized then restored. paint( ) is also called when the applet begins execution. Whatever the cause, whenever the applet must redraw its output, paint( ) is called. The paint( ) method has one parameter of type Graphics. This . parameter will contain the graphics context, which describes the graphics environment in which the applet is running. This context is used whenever output to the applet is required,

The stope method-is called when a web browser leaves the HTML document containing the applet-when it goes to another page, for example. When stope ) is called, the applet is probably running. You should use stop() to suspend threads that don’t need to run when the applet is not visible. You can restart them when  is called if the user returns to the page.

Overriding update( }

In some situations, your applet may need to override another method defined by the
AWf, called update( ). This method is called when your applet has requested that a
portion of its window be redrawn. The default version of update( ) first fills an applet with the default background color and then calls paint( ). If you fill the background using a different color in paint(), the user will experience a flash of the default , background each time update! ) is called-that is, whenever the window is repainted. One way ‘to avoid this problem is to override the update( ) method so that it performs all necessary display activities Then have paint( ) simply call update( ). Thus, for some applications, the applet skeleton will override paint( ) and update ted .

  • Feel free to send us an inquiry, we will reply back in hours.


Posted on September 17, 2014 in Networking

Share the Story

About the Author

Back to Top