Before moving on the applet life cycle lets know about what is Java applet. Java applet is a program that runs within a web browser. Basically Applet is a Java class that is run within the web browser. Here the scenario is that Java provides the code for the applet.
The life cycle of the applet in Java:
init() – init() is the function that runs the first time When the web browser comes across an applet. But before running its first security settings also verified by it. All this piece of code is written in Java.
start() – After a load of Applet, now are able to see your page and you’re still on the page. After init() the browser will now initiate the start() function.
stop() – Start will execute until you won’t move on another page. Once you move to another page, the stop() function is called and run. If you go again back to the old page then start() function will run again.
destroy() – And at last when you’re completely done with browsing and close the browser then destroy() function will call and run.
Let’s see in the brief description of the Life Cycle methods.
public void init(): The only first method that is called at the time of execution start. init() function will only be called once in the whole applet life cycle. Basically this method initializes the Applet. This Initialization helps to initialize the basic variables and instantiate the objects which are required initially and then load the GUI of the applet that will be visible on the web browser. inti() function is invoked when the applet containing page is loaded. It is the state where the applet born.
public void start(): After the calling of init() function applet object is created. But still, the applet is in an inactive state. The init() method calls start() method to starts the execution of Applet. After the calling of start function now the applet becomes active.
public void stop(): This is the state when the stop method is called to send the current applet in a temporarily inactive state. stop() method gets invoked when someone moves on another page or someone minimized the browser. This is a very common state where Applet frequently comes in its life cycle and can go back again inactive state. We can say that it is very similar to the blocked state.
public void paint() : paint() method takes a java.awt.Graphics object as a parameter. Basically paint() method helps to create Applet’s GUI. GUI Like anything it may be colored background, drawing, and writing. start() method calls the paint() method. This is a runnable state which calls much time by the start() method to show the GUI on the browser.
public void destroys (): This is the last method that gets invoked when we close our browser.
Some Important note:
The init() and destroy() methods are called only once in the whole applet life cycle.
The start(), paint() and stop() methods are many times in the applet life cycle.