Thursday 8 March 2012

Creating and executing applets

•Before learning about how to create applets, let us see the basic methods that help us to control the execution of an applet.
•Most of the applets override a set of methods which provide the basic functionality for the applets. Those methods are:
1) init()
2) start()
3) stop()
4) destroy()
5) paint()
•The first four methods are available in the “Applet” class.
•The paint() method is available in the “Component” class.


•When an applet begins, the following methods are executed in sequence:
1) init()
2) start()
3) paint()
•When an applet terminates, the following methods are executed in sequence:
1) stop()
2) destroy()



•init() – The init() method is the first method to be executed when an applet is loaded into the web browser or applet viewer. This method is used to initialize the variables. This method is called only once during the execution of an applet.
•start() – This method is called immediately after the init() method. This method is also called when the applet stops and restarts its execution.
•stop() – This method is called when the web browser leaves the document containing the applet i.e., when the execution of the applet stops. This method is also executed before the destroy() method is invoked.
•destroy() – This method is called when the JVM decides to remove the applet completely from the memory. stop() method is always called before destroy() method.

•paint() – This method is called after the execution of the start() method. paint() is also called whenever the output needs to be redrawn, i.e., whenever the window is minimized and maximized etc… The paint() method contains only one parameter of the type “Graphics”. This parameter represents the current graphics context. We can use this to output data or draw objects like circle, square, rectangle etc…




•In the previous program, the first import statement is required to import the “Applet” class.
•The second import is required as we are using the “paint()” method which is present in the “Component” class available in “java.awt” package. Also for using the “Graphics” class.
•Every applet class must extend the predefined class “Applet”.
•The class must be declared as “public” as it will be accessed from outside the program.
•The drawString() method is available in “Graphics” class which is used to print a string to the GUI. Its syntax is:
void drawString(String message, int x-coordinate, int y-coordinate);

No comments:

Post a Comment