The applet life cycle:
We have seen how to write a very simple applet, with only an init method. This method is invoked to initialize the applet when it is first loaded, normally when you first view the web page that links to the applet. In this sense, the init method acts like a constructor, and should be used for activities that need to be performed once in the lifetime of the applet. We would not normally put all the code for an applet into the init method.
For more complex applets, it is useful to have more control over whether the applet continues to run when the user is no longer viewing the related web page. The applet life cycle methods, shown in Table, are useful in this case. It is important to note that these methods will be invoked by the browser or appletviewer - they should not be invoked explicitly by the code for your applet. For example, when a web page with a linked applet is first viewed using a browser, the init method of the applet will be invoked, followed by the start method.
The applet inherits the life cycle methods, which are shown in Table, from its superclass Applet and these methods should be overridden, if required. The superclass methods typically do nothing. Let us consider when you might need to override one or more of these methods.
The init method and the destroy method are each invoked once during the lifetime of the applet, but the start and stop methods may be invoked several times if the web page is revisited. If an applet uses significant processor resource when running, such as might occur with a complex animation, it is good practice to implement start and stop methods. This ensures that the processor is only being used when the web page is viewed. The init method should be used for one-off initialization, such as allocation of resources, like defining arrays and initializing instance variables. The destroy method is less commonly required, especially since Java garbage collection deals with releasing memory resources. However, it should be used for any final clean-up operations or releasing of resources before the applet terminates. Figure illustrates the sequence of events.
For example, an applet that uses threads would typically use the init method to create the threads and the destroy method to finally remove them. The start and stop methods might start and suspend execution of the threads respectively.
There are a few other important methods for applets, and these are listed in Table.
Among these methods, the most frequently used is the paint method, which is invoked by the system to draw the applet window and any graphical content it may have. You may need to override the paint method that is inherited from the Applet class, to define the graphical content of your applet window. This is particularly useful if the graphical content is to be dynamic. The next subsection shows an example of this.
The other methods in the table are useful for checking whether the applet is still active, for changing the size of the applet window from the size originally defined in the HTML file, and for various multimedia operations such as retrieving images and audio files. Note that you do not have complete freedom to resize the applet window, since it has to fit within a maximum space defined by the <APPLET> tag in the HTML file. See the Applet class in the API for full details of these methods.
Java Assignment Help - Java Homework Help
Struggling with java programming language? Are you not finding solution for your The applet life cycle homework and assignments? Live The applet life cycle experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer The applet life cycle homework help, java assignment help and The applet life cycle projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.
Why Expertsmind for assignment help
- Higher degree holder and experienced experts network
- Punctuality and responsibility of work
- Quality solution with 100% plagiarism free answers
- Time on Delivery
- Privacy of information and details
- Excellence in solving java programming language queries in excels and word format.
- Best tutoring assistance 24x7 hours