Well,
The Life Cycle of An Applet
U often feel uncomfortable with applets because they don't really understand the various methods that they need to include, why these are necessary, and what happens when the VM starts to run an applet. The following describes the events that take place when we launch an applet. If you have difficulty understanding any of these points, make sure that you talk to your TA or instructor.
1. The web browser or applet viewer receives a command (from the user) to run a particular applet.
2. The VM that is built-into the browser or viewer starts up, and the compiled version of the applet (the bytecode, which is stored in a .class file somewhere) gets located.
3. The VM starts to interpret the bytecode into the machine language of the computer that we're trying to run the applet on.
4. An Applet object is created. This is done automatically for us, behind the scenes. You don’t need to invoke an Applet constructor in your code, the system takes care of creating the Applet object. This Applet object knows how to respond to all of the messages specified in the Applet class.
5. A series of important Applet methods now gets executed, each method plays a specific role in the set-up and execution of the applet. If your applet code does not contain its own customized versions of these methods, the system will use its own "invisible", default versions instead. If you, the programmer, have supplied your own, more detailed versions of these methods, they will "override" the default versions. (You will understand this issue a lot more when we get to the "Inheritance" lecture later on in the course).
These important methods include:
a) the init() method. The init() method is executed only once, when the applet is first launched, just before it starts running. After creating the Applet object, the init message is sent to the Applet object to initialise the state of the Applet object. For example, suppose we want to have clickable buttons attached to our applet when it is first displayed – these buttons have to be created and then attached to the Applet as part of its state.
b) the start() method. This method performs the task of activating the method, making it actually run. Unlike the init() method, it can be invoked more than once. It is invoked after the init() method when the applet is first launched, and then re-invoked each time the applet needs to be started up again. (For example, the user paused the applet in order to do some other task, but when (s)he returns the applet needs to be started up again. To do this, the system invokes the start() method). In CMPUT 114, you won't usually have worry about including a start() method of your own in your applet code, the default version is usually sufficient.
c) the paint() method is also called after the init() method. The paint() method is responsible for drawing (or "painting") the applet onto the screen for the user to see. The paint() method takes one argument of declared type Graphics:
public void paint(Graphics g) {
When the applet is first launched a Graphics object is automatically created by the system, and a reference to this Graphics object is supplied as an argument for the invokation of the paint() method. You, the programmer, do not have to use a constructor to create the Graphics object, or declare a Graphics instance variable or anything like that. The run-time system takes care of that automatically for us. The Graphics object models the portion of the screen that your applet is going to be displayed on. So when the applet is launched and the init() method has finished executing, the paint() method is given a reference to this automatically-created Graphics object and uses it whenever the paint() method is invoked. The paint method can be called many times - each time we need to re-draw the applet onto the screen,
Answered by
Ajay Pal
, an ibibo Master,
at
2:00 PM on May 23, 2008