BidVertiser

Tuesday, January 6, 2009

Applets in JAVA

Applet class can be used to enhance the way your applet works. Recall that the Applet class provides the foundation for creating applets-Java applications that run in a browser environment. Besides launching your applet, the Applet class provides many useful services. It can be used to load image and audio files, work with URLs, and access the native browser environment. Since the Applet class is also a component of the AWT package .Applet objects provide many of the visual features that are part of the standard AWT repertoire, especially using the Graphics class for painting text, shapes, and images. Since The Applet class is a subclass of the AWT Component class, it can handle events such as mouse events and keystrokes.
Four often misunderstood Applet methods are overridden to manage the life cycle of an applet. None of these methods are required to be overridden, although their use will generally give you a more stable applet. These are the four methods:


init() This is used to initialize an applet whenever it is loaded. You typically override this method to set up resources that will be used throughout an applet, such as fonts, or to initialize variables. This method is called once and only once during the lifetime of your applet. However, if the applet is reloaded for some reason or another, the init() method will be called again. Some Java literature may lead you to believe that you have to always override this method. This is not true! You need to override init() only when your applet's circumstances dictate that you should. A good example of this is initializing resources, such as AWT components.


start() This is called whenever the HTML document on which an applet resides becomes the current page of a browser. When an applet is first run, the start() method is called after init(). Unlike the latter, however, start() will be called whenever the user visits the applet's page. Two very important types of activities should be located in the start() method. The show() method of instances of the Frame class are best called in the start() method. Since Frames occur outside the confines of an applet page, they will stay onscreen even after you have left the page. Consequently, they should be shown when you enter the page and hidden when you leave (see the stop() description method that follows). There will be an example in the upcoming listings. The start() method is also a good place to begin threads since their existence is also not confined to the page where they began.


stop() This method is called whenever the user leaves a page-it is the converse of the start() method. Therefore, it's a good place to hide frames and terminate threads.


destroy() The destroy() method is called whenever the applet is being shut down. Typically, this will occur when the browser is being closed, although there could be other circumstances that could lead to destroy() being invoked. This method is a good place to do some cleanup. However, since it's unpredictable when destroy() will be called, it should be used with some discretion.

No comments: