Chapter: Java The Complete Reference - The Java Library - The Applet Class

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail

Applet Basics

AWT-based applets are subclasses of Applet.

Applet Basics

 

Chapter 13 introduced the general form of an applet and the steps necessary to compile and run one. Let’s begin by reviewing this information.

 

AWT-based applets are subclasses of Applet. Applets are not stand-alone programs. Instead, they run within either a web browser or an applet viewer. The illustrations shown in this chapter were created with the standard applet viewer, called appletviewer, provided by the JDK.

 

Execution of an applet does not begin at main( ). Actually, few applets even have main( ) 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 System.out.println( ). Rather, in an AWT-based applet, output is handled with various AWT methods, such as drawString( ), which outputs a string to a specified X,Y location. Input is also handled differently than in a console application.

 

Before an applet can be used, a deployment strategy must be chosen. There are two basic approaches. The first is to use the Java Network Launch Protocol (JNLP). This approach offers the most flexibility, especially as it relates to rich Internet applications. For real-world applets that you create, JNLP will often be the best choice. However, a detailed discussion

of JNLP is beyond the scope of this book. (See the JDK documentation for the latest details on JNLP.) Fortunately, JNLP is not required for the example applets shown here.

The second basic approach to deploying an applet is to specify the applet directly in an HTML file, without the use of JNLP. This is the original way that applets were launched when Java was created, and it is still used today—especially for simple applets. Furthermore, because of its inherent simplicity, it is the appropriate method for the applet examples described in this book. At the time of this writing, Oracle recommends the APPLET tag for this purpose. Therefore, the APPLET tag is used in this book. (Be aware that the APPLET tag is currently deprecated by the HTML specification. The alternative is the OBJECT tag. You should check the JDK documentation in this regard for the latest recommendations.) When an APPLET tag is encountered in the HTML file, the specified applet will be executed by a Java-enabled web browser.

 

The use of the APPLET tag offers a secondary advantage when developing applets because it enables you to easily view and test the applet. To do so, simply include a comment at the head of your Java source code file that contains the APPLET tag. This way, your code is documented with the necessary HTML 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:

 

/*

 

<applet code="MyApplet" width=200 height=60> </applet>

 

*/

 

This comment contains an APPLET tag that will run an applet called MyApplet in a window that is 200 pixels wide and 60 pixels high. Because the inclusion of an APPLET command makes testing applets easier, all of the applets shown in this book will contain the appropriate APPLET tag embedded in a comment.

The Applet Class

 

The Applet class defines the methods shown in Table 23-1. Applet provides all necessary support for applet execution, such as starting and stopping. It also provides methods that load and display images, and methods that load and play audio clips. Applet extends the AWT class Panel. In turn, Panel extends Container, which extends Component. These classes provide support for Java’s window-based, graphical interface. Thus, Applet provides all of the necessary support for window-based activities. (An overview of the AWT is presented in subsequent chapters.)

 

Method : Description

 

void destroy( ) : Called by the browser just before an applet is terminated. Your applet will override this method if it needs to perform any cleanup prior to its destruction.

 

AccessibleContext getAccessibleContext( ) : Returns the accessibility context for the invoking object.

 

AppletContext getAppletContext( ) : Returns the context associated with the applet.

 

String getAppletInfo( ) : Overrides of this method should return a string that describes the applet. The default implementation returns null.

 

AudioClip getAudioClip(URL url) : Returns an AudioClip object that encapsulates the audio clip found at the location specified by url.

 

AudioClip getAudioClip(URL url,String clipName) : Returns an AudioClip object that encapsulates the audio clip found at the location specified by url and having the name specified by clipName.

 

URL getCodeBase( ) : Returns the URL associated with the invoking applet.

 

URL getDocumentBase( ) : Returns the URL of the HTML document that invokes the applet.

 

Image getImage(URL url) : Returns an Image object that encapsulates the image found at the location specified by url.

 

Image getImage(URL url, String imageName) : Returns an Image object that encapsulates the image found at the location specified by url and having the name specified by imageName.

 

Locale getLocale( ) : Returns a Locale object that is used by various locale-sensitive classes and methods.

 

String getParameter(String paramName) : Returns the parameter associated with paramName. null is returned if the specified parameter is not found.

 

 

String[ ] [ ] getParameterInfo( ) : Overrides of this method should return a String table that describes the parameters recognized by the applet. Each entry in the table must consist of three strings that contain the name of the parameter, a description of its type and/or range, and an explanation of its purpose. The default implementation returns null.

 

void init( ) : Called when an applet begins execution. It is the first method called for any applet.

 

boolean isActive( ) : Returns true if the applet has been started. It returns false if the applet has been stopped.

 

boolean isValidateRoot( ) : Returns true, which indicates that an applet is a validate root.

 

static final AudioClip newAudioClip(URL url) : Returns an AudioClip object that encapsulates the audio clip found at the location specified by url. This method is similar to getAudioClip( ) except that it is static and can be executed without the need for an Applet object.

 

void play(URL url) : If an audio clip is found at the location specified by url, the clip is played.

 

void play(URL url, String clipName) : If an audio clip is found at the location specified by url with the name specified by clipName, the clip is played.

 

void resize(Dimension dim) : Resizes the applet according to the dimensions specified by dim. Dimension is a class stored inside java.awt. It contains two integer fields: width and height.

 

void resize(int width, int height) : Resizes the applet according to the dimensions specified by width and height.

 

final void setStub(AppletStub stubObj) : Makes stubObj the stub for the applet. This method is used by the run-time system and is not usually called by your applet. A stub is a small piece of code that provides the linkage between your applet and the browser.

 

void showStatus(String str) : Displays str in the status window of the browser or applet viewer. If the browser does not support a status window, then no action takes place.

 

void start( ) : Called by the browser when an applet should start (or resume) execution. It is automatically called after init( ) when an applet first begins.

 

void stop( ) : Called by the browser to suspend execution of the applet. Once stopped, an applet is restarted when the browser calls start( ).







Table 23-1   The Methods Defined by Applet


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.