The Structure and Life Cycle of an Applet



The Java Applet class includes the methods required to initialize and run an applet. Your job is to use inheritance to override the Applet class and fill in these methods as required. When an applet is loaded into its applet context, certain applet methods are called in a specific order. At the scope of the Applet class, these methods don’t actually do anything. However, they are not declared as abstract either, so you don’t have to override them. However, they are given as empty skeleton methods for your applets to follow.

After an applet has been loaded into its context its init method is called. Within the init method you should give the code to initialize your applet. This contains initializing your game objects as well as loading any images or sound your applet may use.

The next method to be called is the start method. It simply informs the applet that it is ready to start execution. You can use the start method to do things such as start animation sequences and threads.

After the start method has completed, the paint method is called. This is where the visible applet content is rendered to the window. It passes to it the applet’s Graphics context, which is used to present graphic material to the window.

Next is where you will want to add interactivity between the user and the applet. When the user is finished with the applet and either goes to another Web page or closes the browser altogether, the stop and destroy methods are called. The stop method stops the execution of the applet. So if you started an animation within the start method, this is the time to end it. The final method called within an applet is the destroy method. It is here that you should terminate live objects, such as threads, created during the life of the applet.