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/difference-between-collection-and-arrays-9018464.aspx' target='_balnk' >Difference between collection and arrays</a></b></span></h5> <p class="answer"> <span id="dlRelatedTags_lblContent_0">Write the difference between collection and arrays?</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/non-linear-dynamic-model-equation-to-linear-equation-901442.aspx' target='_balnk' >Non linear dynamic model equation to</a></b></span></h5> <p class="answer"> <span id="dlRelatedTags_lblContent_1">Could you please show the steps from non linear dynamic model equation to linear equation?</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/explain-a-quality-and-metrics-reporting-program-of-a-company-909112.aspx' target='_balnk' >Explain a quality and metrics reporting</a></b></span></h5> <p class="answer"> <span id="dlRelatedTags_lblContent_2">What opinion would you provide to someone who asked you where to begin to introduce to their company a quality and metrics reporting program?</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-peer-902302.aspx' target='_balnk' >Define Peer Peer : It is a term</a></b></span></h5> <p class="answer"> <span id="dlRelatedTags_lblContent_3">Peer: It is a term employed of the Abstract Windowing Toolkit (AWT) to refer to the underlying classes which give the platform-specific implementation of the component classes.</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/state-the-term-urn-909110.aspx' target='_balnk' >State the term URN State the term URN?</a></b></span></h5> <p class="answer"> <span id="dlRelatedTags_lblContent_4">State the term URN?</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/define-the-term-cpu-scheduler-909208.aspx' target='_balnk' >Define the term CPU Scheduler Define</a></b></span></h5> <p class="answer"> <span id="dlRelatedTags_lblContent_5">Define the term CPU Scheduler?</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/explain-copy-constructor-902126.aspx' target='_balnk' >Explain Copy constructor Copy</a></b></span></h5> <p class="answer"> <span id="dlRelatedTags_lblContent_6">Copy constructor: It is a constructor which takes a single argument of similar class. For illustration:     public class Point {        </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/compute-days-between-ordering-and-shipping-in-sql-902855.aspx' target='_balnk' >Compute Days Between Ordering and</a></b></span></h5> <p class="answer"> <span id="dlRelatedTags_lblContent_7">An analyst in the quality assurance office reviews the time lapse between receiving an order and shipping an order. Any orders which have not been shipped in a day of the order being positioned are investigated. Build a function named ORD_SHIP_SF which computes the nu</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/which-is-the-tool-having-demand-within-the-market-909047.aspx' target='_balnk' >Which is the tool having demand within</a></b></span></h5> <p class="answer"> <span id="dlRelatedTags_lblContent_8">Which is the excellent tool one should learn, which is the tool having demand within the market?</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/differences-between-primary-storage-and-secondary-storage-909213.aspx' target='_balnk' >Differences between primary storage and</a></b></span></h5> <p class="answer"> <span id="dlRelatedTags_lblContent_9">What are the differences between primary storage and secondary storage?</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 1420214 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> 1955178<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> 1420214</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> <script> var url = 'https://www.tutorsglobe.com/include/javascript/watiWidget.js'; var s = document.createElement('script'); s.type = 'text/javascript'; s.async = true; s.src = url; var options = { "enabled":true, "chatButtonSetting":{ "backgroundColor":"#00e785", "ctaText":"Whatsapp Support!!", "borderRadius":"25", "marginLeft": "0", "marginRight": "20", "marginBottom": "20", "ctaIconWATI":false, "position":"left" }, "brandSetting":{ "brandName":"Tutorsglobe", "brandSubTitle":"Trusted Since 2005", "brandImg":"https://www.tutorsglobe.com/include/images/chat-logo.svg", "welcomeText":"Hi there!\nDo you Need help?", "messageText":"Hello, Tutorsglobe !! I have a question!", "backgroundColor":"#00e785", "ctaText":"Chat with Whatsapp", "borderRadius":"25", "autoShow":false, "phoneNumber":"441416286080" } }; s.onload = function() { CreateWhatsappChatWidget(options); }; var x = document.getElementsByTagName('script')[0]; x.parentNode.insertBefore(s, x); </script> <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>