Explain the java applets with a suitable example.


APPLET: an applet is an application designed to travel over the internet and to be executed on the client machine by a java compatible web browser like internet Explorer or Netscape. Applets are also java programs but they reside on the servers. An applet cannot be executed like standalone application. Applet can be executed only by embedding it into an HTML page like an image or sound file. To run an applet we need to access an HTML page which has applet embedded into it. When the web browser downloads such an HTML page it subsequently loads the executable file which contains an applet code and then executes it on the local machine. After an applet arrives on the client it has limited access to resources so that it can produce an arbitrary multimedia user interface and run intricate calculations with no introduction of the risk of viruses or breaching data integrity.

Applet Architectures: applets are different from normal java programs.

.  Applets are GUI based (window based) programs.

. Applets are event driven.

When a normal java program (which is not GUI based) needs input it prompts the user and then calls some input method such us read line. The interaction is initiated by the program. This is not the way in which GUI based programs behave. The user initiates interaction with the program rather than program initiating the action. For example in a world processing software user mmsoftware user initiates action by clicking on different buttons. Which generates an event and some piece of code is executed as a result and accordingly some action takes place. Applets use awt package (Abstract Window Toolkit) for providing GUI and for event handling. The awt is called so because it totally depends on the functionality of the under lying or operating system. An applet looks like a set of interrupt service routines. An applet waits and event occurs. The awt notifies the applet about an event by calling an event handler that has been provided by the applet. Ones this happens the applet must take appropriate action and then quickly return control to the AWt. This is a critical point. For the most part over applet should not enter a mode of operation in which it maintains control for an extended period. Instead it must perform specific response to events and the return control to the AWT run time system. In those situations in which our applet needs to perform a repetitive task on its own (for example displaying a scrolling message across its window) we must start an additional thread of execution.

Writing an applet:

All applets are classes of applet class. The applet class is contained in the package java applet . the hierarchy of the applet class is as follows:



Java.awt. container

Java. Awt .panel

Java. applet. applet

The applet class provides all necessary support for applet execution such as starting and stopping. It also provides methods that load and display images. It also provides necessary support for all the window based activities. Our sub class extending the applet class must always be declared as public as it is instantiated and executed by the web browser. The browser makes use of no argument constructor when instantiating the applet so it is must to provide no argument public constructor with the public visibility. It is recommended that we do not provide constructor in the applet class as in that case complier will provide the default no argument constructor. We can make use of init () method for initialization. While writing applet code we normally over ride some of the methods of the applet class which are invoked automatically during applet execution. It is every common to over ride the paint () method mainly displays the output in the applet window.

Executing an applet: applets are not executed by the console based java run time interpreter. There are two ways in which we can run applets.

1 executing the applet with in a java compatible web browser.

2 using an applet viewer such as the standard SDK tool applet viewer. An applet viewer executes our applet in a window. This is fastest and easiest way to test our applet. Executing applet in a web browser: we need to write a short HTML text file that contains the appropriate APPLET tag. The applet tag must include at least following three attributes:




The attribute code value specifies the name of the class containing applets code. The attribute width and height specify the width and height of the applets window respectively.

Here the HTML file Run App.Html with applet simple applet embedded into it.



           simple applet <\ title></p> <p style="text-align: justify;">        <\head></p> <p style="text-align: justify;">            <Body></p> <p style="text-align: justify;">         <h     </p> <p style="text-align: justify;"><strong>Executing applet using applet viewer: </strong>however a more convenient methods exists that we can use up testing. Simply include a comment in your java source file that consist that applet tag. By doing so our code is document with a proto type of the necessary HTML statements and we can test your compiled output merely by starting the applet viewer with our java source code file. If we use this method, then after compiling we can execute the applet as follows:</p> <p style="text-align: justify;"><strong>Applet viewer simple applet java</strong></p> <p style="text-align: justify;">Applet life cycle methods: All but the most trival applets over ride a set of methods that provides the basic mechanism by which the browser or applet viewer communicates to the applet and controls its execution. These methods are also called life cycle methods because the applets or browser or applet viewer calls them automatically during different stages of applet life cycle. In all there are five life cycle methods.</p> <p style="text-align: justify;">Public void init ()</p> <p style="text-align: justify;">Public void start ()</p> <p style="text-align: justify;">Public void stop ()</p> <p style="text-align: justify;">Public void destroy ()</p> <p style="text-align: justify;">Public void paint () (graphics g)</p> <p style="text-align: justify;">Four of these methods init (), start (), stop (), destroy () are defined as in the applet class. The paint (), is defined by the awt component class default implementations for all these methods they do not use. However, very simple applets will not need to define them.</p> <p style="text-align: justify;"><strong>Applet initialization and termination:</strong></p> <p style="text-align: justify;">When an applet begins, the following methods are called in sequence</p> <p style="text-align: justify;">1.      Init ()</p> <p style="text-align: justify;">2.      Start ()</p> <p style="text-align: justify;">3.      Paint ()</p> <p style="text-align: justify;">When an Applet is terminated the following sequence of methods are called and takes place :</p> <p style="text-align: justify;">4.      Stop ()</p> <p style="text-align: justify;">5.      Destroy ()</p> <p style="text-align: justify;">Init () method: this is the first method to be called. This is when we should initialize variables and write code for other initialization activities. These methods are called only one during the life cycle of our applet immediately after installation.</p> <p style="text-align: justify;">Start () method: the start () method is called after init (): it is also called restart an applet after it has been stopped. Where init () is called once the first time an Applet is loaded start () is known each time an applet HTML document is displayed on screen. So if a user leaves a web page and comes back, the applet resumes execution at start ():</p> <p style="text-align: justify;">Paint () method: the paint () is called after the init () and start () method when the applet begins execution. The paint () method is also called each time our applet output must be redrawn. This condition can takes place for various reasons. For example, the window in which applet is running may be over written by another window and then uncovered, or the applet window may be minimized and then restored.</p> <p style="text-align: justify;"> Stop () method: the stop () method is said when a web browser leave the HTML document containing the applet when it goes to another page. We should use stop () to suspend threads that do not needs to run when the applet is not visible. We can restart them when start () is called if the user returns to the page. We can also free from any costly resource and acquire it again in the start () method.</p> <p style="text-align: justify;">Destroy () method: the destroy () method is called when the environment determines that our applets needs to be removed completely from memory. At this point, we should free up any resource the applet may be using. The stop () method is always called before destroy ().</p> <p style="text-align: justify;">Example: a simple example of destroy () applet that sets the background colour of the cyan the foreground colour to red, an displays a message that illustrates the order in which the init (), and paint () methods are called when an applet starts up.</p> <p> </p></span></p></div> <div id="viewreadmore" class="link"> <a id="readmore" href="javascript:void(0);" class="read-more-trigger mar_top10" onclick="changeheight(this)">View Complete Question</a> </div> </div> <div class="row"> <div class="col-sm-12 reviewbox"> <div class="row "> <div class="panel-group review" id="accordion" role="tablist" aria-multiselectable="true"> </div> </div> </div> </div> <div class="user-comments-area"> <h4 class="text-uppercase mar_btm20"> <i class="fa fa-question-circle"></i>   Related Questions in Programming Languages</h4> <ul class="user-comments-list"> <table id="dlRelatedTags" cellspacing="0" width="100%"> <tr> <td> <li> <div class="comment-box"> <h5> <span class="mar_lft5">Q :</span> <span id="dlRelatedTags_lblTitle_0"><b><a href='https://www.tutorsglobe.com/getanswer/why-do-i-require-tc-trustcenter-publisher-id-909056.aspx' target='_balnk' >Why do I require TC Trustcenter</a></b></span></h5> <p class="answer"> <span id="dlRelatedTags_lblContent_0">Why do I require TC Trustcenter Publisher ID?</span></p> </div> <!-- /comment-box --> </li> </td> </tr><tr> <td> <li> <div class="comment-box"> <h5> <span class="mar_lft5">Q :</span> <span id="dlRelatedTags_lblTitle_1"><b><a href='https://www.tutorsglobe.com/getanswer/define-unicode-902429.aspx' target='_balnk' >Define Unicode Unicode : It is a 16-bit</a></b></span></h5> <p class="answer"> <span id="dlRelatedTags_lblContent_1">Unicode: It is a 16-bit character set designed to make it simpler to exchange and exhibit information which makes use of a broad range of dissimilar languages and symbols.</span></p> </div> <!-- /comment-box --> </li> </td> </tr><tr> <td> <li> <div class="comment-box"> <h5> <span class="mar_lft5">Q :</span> <span id="dlRelatedTags_lblTitle_2"><b><a href='https://www.tutorsglobe.com/getanswer/purpose-of-exec-functions-9023455.aspx' target='_balnk' >Purpose of exec functions Specify the</a></b></span></h5> <p class="answer"> <span id="dlRelatedTags_lblContent_2">Specify the purpose of exec functions?</span></p> </div> <!-- /comment-box --> </li> </td> </tr><tr> <td> <li> <div class="comment-box"> <h5> <span class="mar_lft5">Q :</span> <span id="dlRelatedTags_lblTitle_3"><b><a href='https://www.tutorsglobe.com/getanswer/define-the-term-address-space-902044.aspx' target='_balnk' >Define the term Address space Define</a></b></span></h5> <p class="answer"> <span id="dlRelatedTags_lblContent_3">Define the term Address space: The region of virtual memory in which a procedure is run.</span></p> </div> <!-- /comment-box --> </li> </td> </tr><tr> <td> <li> <div class="comment-box"> <h5> <span class="mar_lft5">Q :</span> <span id="dlRelatedTags_lblTitle_4"><b><a href='https://www.tutorsglobe.com/getanswer/aggregation-and-containment-in-the-programming-9027462.aspx' target='_balnk' >Aggregation and containment in the</a></b></span></h5> <p class="answer"> <span id="dlRelatedTags_lblContent_4">Illustrate the basic difference between Aggregation and containment in the Programming?</span></p> </div> <!-- /comment-box --> </li> </td> </tr><tr> <td> <li> <div class="comment-box"> <h5> <span class="mar_lft5">Q :</span> <span id="dlRelatedTags_lblTitle_5"><b><a href='https://www.tutorsglobe.com/getanswer/how-is-an-xpointer-processor-configured-909079.aspx' target='_balnk' >How is an XPointer processor configured</a></b></span></h5> <p class="answer"> <span id="dlRelatedTags_lblContent_5">How is an XPointer processor configured?</span></p> </div> <!-- /comment-box --> </li> </td> </tr><tr> <td> <li> <div class="comment-box"> <h5> <span class="mar_lft5">Q :</span> <span id="dlRelatedTags_lblTitle_6"><b><a href='https://www.tutorsglobe.com/getanswer/define-divide-and-conquer-902146.aspx' target='_balnk' >Define Divide and conquer Divide and</a></b></span></h5> <p class="answer"> <span id="dlRelatedTags_lblContent_6">Divide and conquer: An approach to trouble solving which attempts to decrease an overall single big trouble into multiple simpler troubles.</span></p> </div> <!-- /comment-box --> </li> </td> </tr><tr> <td> <li> <div class="comment-box"> <h5> <span class="mar_lft5">Q :</span> <span id="dlRelatedTags_lblTitle_7"><b><a href='https://www.tutorsglobe.com/getanswer/explain-relationship-between-xhtml-and-dtd-909123.aspx' target='_balnk' >Explain relationship between XHTML and</a></b></span></h5> <p class="answer"> <span id="dlRelatedTags_lblContent_7">Explain relationship between XHTML and DTD?</span></p> </div> <!-- /comment-box --> </li> </td> </tr><tr> <td> <li> <div class="comment-box"> <h5> <span class="mar_lft5">Q :</span> <span id="dlRelatedTags_lblTitle_8"><b><a href='https://www.tutorsglobe.com/getanswer/define-the-term-monitor-902267.aspx' target='_balnk' >Define the term Monitor Define the term</a></b></span></h5> <p class="answer"> <span id="dlRelatedTags_lblContent_8">Define the term Monitor: It is an object with one or more synchronized techniques.</span></p> </div> <!-- /comment-box --> </li> </td> </tr><tr> <td> <li> <div class="comment-box"> <h5> <span class="mar_lft5">Q :</span> <span id="dlRelatedTags_lblTitle_9"><b><a href='https://www.tutorsglobe.com/getanswer/define-static-nested-class-902393.aspx' target='_balnk' >Define Static nested class Static</a></b></span></h5> <p class="answer"> <span id="dlRelatedTags_lblContent_9">Static nested class: It is a nested class with static reserved word in its header. Dissimilar inner classes, objects of static nested classes contain no enclosing object. They are as well termed as nested top-level classes.</span></p> </div> <!-- /comment-box --> </li> </td> </tr> </table> </ul> <!-- /user-comments-list --> </div> </div> <div class="col-md-4 col-xs-12 login-area innerpage"> <div id="DivSolution"> <h4> Discover Q & A</h4> <div class="seprator"> </div> <div style="text-align: justify"></div> <div class="downloadfiles"> <h5> Leading Solution Library</h5> <h6> Avail More Than 1427307 Solved problems, classrooms assignments, textbook's solutions, for quick Downloads</h6> <h5> No hassle, Instant Access </h5> <a href="https://www.tutorsglobe.com/library/" class="btn btn-success btn-lg btn-block-sm mar_btm20"> Start Discovering </a> </div> <div class="seprator"> </div> </div> <div class="row"> <div class="details col-md-12"> <div class="col-md-4"> <div class="circle orange"> <i class="fa fa-question"></i> </div> <p> 18,76,764 </p> <p> 1947674<br /> Asked</p> </div> <div class="col-md-4"> <div class="circle yellow"> <i class="fa fa-user-secret"></i> </div> <p> 3,689</p> <p> Active Tutors</p> </div> <div class="col-md-4"> <div class="circle green"> <i class="fa fa-thumbs-o-up"></i> </div> <p> 1427307</p> <p> Questions<br /> Answered</p> </div> <p> Start Excelling in your courses, Ask an Expert and get answers for your homework and assignments!!</p> <a href="https://www.tutorsglobe.com/post-your-job-for-free.aspx" class="btn btn-primary btn-lg mar_top10">Submit Assignment </a> </div> </div> </div> </div> </div> </div> <footer class="site-footer"> <div class="container"> <div class="footerlinks"> <a href="https://www.tutorsglobe.com/">Home</a> | <a href="https://www.tutorsglobe.com/about-us.aspx">Company Overview</a> | <a href="https://www.tutorsglobe.com/services.aspx">Services</a> | <a href="https://www.tutorsglobe.com/library/">Discover Q&A</a> | <a href="https://www.tutorsglobe.com/sitemap.aspx">Sitemap</a> | <a href="https://www.tutorsglobe.com/contact-us.aspx">Contact Us</a> | <a href="https://www.tutorsglobe.com/terms-and-conditions.aspx">T & C</a> | <a href="https://www.tutorsglobe.com/refundcancelpolicy.aspx">Refund Policy</a> | <a href="https://www.tutorsglobe.com/copyright-infringement-policy.aspx">Copyright Policy</a> | <a href="https://www.tutorsglobe.com/blog/archive/">Blog</a> | <a href="https://www.tutorsglobe.com/library/archive.aspx">Q&A</a> | <a href="https://www.tutorsglobe.com/education-directory.aspx">Directory</a> </div> <p>©TutorsGlobe</a> All rights reserved 2022-2023. </p> <script type="application/ld+json"> { "@context": "http://schema.org/", "@type": "product", "name": "Tutorsglobe", "image": "https://www.tutorsglobe.com/IncludeLib/Images/logo.png", "description": "elearning Platform - Tutor Service", "brand": { "@type": "elearning", "name": "Tutorsglobe" }, "aggregateRating": { "@type": "AggregateRating", "ratingValue": "4.9", "ratingCount": "37128" } } </script> <a href="#" class="settings"><i class="fa fa-angle-up"></i></a> <ul class="social-icons"> <li><a href="https://www.facebook.com/TutorsGlobe" rel="nofollow" target="_blank"><i class="fa fa-facebook-square"></i></a></li> <li><a href="https://twitter.com/Tutorsglobe" rel="nofollow" target="_blank"><i class="fa fa-twitter-square"></i></a></li> <li><a href="#" rel="nofollow"><i class="fa fa-youtube-square"></i></a></li> <li><a href="https://www.linkedin.com/company/tutorsglobe" target="_blank" rel="nofollow"><i class="fa fa-linkedin-square"></i></a></li> </ul> </div> <script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-32333066-1']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.tutorsglobe.com/IncludeLib/js/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })(); </script> <script async src="https://www.googletagmanager.com/gtag/js?id=G-5E9QFMFDJR"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-5E9QFMFDJR'); </script> </footer> </div> <!-- /pageWrap --> <div class="overlay"> </div> <!-- JavaScript Files ================================================== --> <script type="text/javascript" src="../IncludeLib/js/jquery-1.11.2.min.js"></script> <script type="text/javascript" src="../IncludeLib/js/bootstrap.min.js"></script> <script type="text/javascript" src="../IncludeLib/js/jquery.mCustomScrollbar.concat.min.js"></script> <script type="text/javascript" src="../IncludeLib/js/script.js"></script> <script type="text/javascript" src="../IncludeLib/js/ie10-viewport-bug-workaround.js"></script> </form> </body> </html>