17.7. Rethrowing Exceptions

 
[Page 536 ( continued )]

16.2. The Applet Class

The Applet class provides the essential framework that enables applets to be run from a Web browser. While every Java application has a main method that is executed, when the application starts, applets do not have a main method. Instead they depend on the browser to call the methods in the Applet class. Every applet is a subclass of java.applet.Applet , as outlined below:

   public class   MyApplet    extends   java.applet.Applet  {   ...  /** The no-arg constructor is called by the browser when the Web   *  page containing this applet is initially loaded, or reloaded   */     public   MyApplet()  {     ...   }  /** Called by the browser after the applet is loaded   */     public void   init()  {     ...   }  /** Called by the browser after the init() method, or   *  every time the Web page is visited   */     public   void start()  {     ...   }  /** Called by the browser when the page containing this   *  applet becomes inactive   */   pu   blic void   stop()  {      ...   }  /** Called by the browser when the Web browser exits */     public   void destroy()  {     ...   }  /** Other methods if necessary... */  } 


[Page 537]

When the applet is loaded, the Web browser creates an instance of the applet by invoking the applet's no-arg constructor . So the applet must have a no-arg constructor declared either explicitly or implicitly. The browser uses the init , start , stop , and destroy methods to control the applet. By default, these methods do nothing. To perform specific functions, they need to be modified in the user 's applet so that the browser can call your code properly. Figure 16.1(a) shows how the browser calls these methods, and Figure 16.1(b) illustrates the flow of control of an applet using a statechart diagram.

Figure 16.1. The Web browser uses the init , start , stop , and destroy methods to control the applet.

16.2.1. The init Method

The init method is invoked after the applet is created. A subclass of Applet should override this method if the subclass has an initialization to perform. The functions usually implemented in this method include setting up user-interface components , loading resources such as images and audio, and getting string parameter values from the <applet> tag in the HTML page.

16.2.2. The start Method

The start method is invoked after the init method. It is also called when the user returns to the Web page containing the applet after surfing other pages.

A subclass of Applet overrides this method if it has any operation that needs to be performed whenever the Web page containing the applet is visited. An applet with animation, for example, might start the timer to resume animation.

16.2.3. The stop Method

The stop method is the opposite of the start method. The start method is called when the user moves back to the page that contains the applet. The stop method is invoked when the user leaves the page.

A subclass of Applet overrides this method if it has any operation that needs to be performed each time the Web page containing the applet is no longer visible. An applet with animation, for example, might stop the timer to pause animation.


[Page 538]

16.2.4. The destroy Method

The destroy method is invoked when the browser exits normally to inform the applet that it is no longer needed and should release any resources it has allocated. The stop method is always called before the destroy method.

A subclass of Applet overrides this method if it has any operation that needs to be performed before it is destroyed . Usually, you won't need to override this method unless you wish to release specific resources that the applet created.

 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net