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.lang.object

Java.awt.component

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:

       Code

        Width

       Height

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/function-prototypes-905264.aspx' target='_balnk' >Function prototypes Function prototypes</a></b></span></h5> <p class="answer"> <span id="dlRelatedTags_lblContent_0">Function prototypes: Function declaration which specifies the function name, return type and parameter list of the function. Syntax: return_type function_name(type var1, type var2,…</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/explain-the-term-xforms-909151.aspx' target='_balnk' >Explain the term XForms Explain the</a></b></span></h5> <p class="answer"> <span id="dlRelatedTags_lblContent_1">Explain the term XForms.</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/what-is-pattern-902301.aspx' target='_balnk' >What is Pattern Pattern : It is a</a></b></span></h5> <p class="answer"> <span id="dlRelatedTags_lblContent_2">Pattern: It is a recurring theme in class usage or design. Interfaces like Iterator encapsulate a pattern of admission to the items in a collection, whereas freeing the client from the requirement to know details of the way in which the collection is</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/template-class-and-class-template-in-the-programming-9027464.aspx' target='_balnk' >Template class and class template in</a></b></span></h5> <p class="answer"> <span id="dlRelatedTags_lblContent_3">Illustrate the difference between a template class and class template 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_4"><b><a href='https://www.tutorsglobe.com/getanswer/what-is-mime-902262.aspx' target='_balnk' >What is MIME MIME : Multipurpose</a></b></span></h5> <p class="answer"> <span id="dlRelatedTags_lblContent_4">MIME: Multipurpose Internet Mail Extensions (abbreviated as MIME) are rules which make it possible to utilize electronic mail to send content other than the simple text.</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-graphical-user-interface-902178.aspx' target='_balnk' >Define the term Graphical User Interface</a></b></span></h5> <p class="answer"> <span id="dlRelatedTags_lblContent_5">Graphical User Interface: A Graphical User Interface (abbreviated as GUI) is a part of a program which permits user interaction through graphical components, like menus, buttons, text areas, and so forth. Interaction frequently includes the usage of a</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/asynchronous-and-synchronous-postback-9017717.aspx' target='_balnk' >Asynchronous and synchronous postback</a></b></span></h5> <p class="answer"> <span id="dlRelatedTags_lblContent_6">Differeniate asynchronous postback and synchronous postback?</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/what-is-an-assembly-language-902061.aspx' target='_balnk' >What is an Assembly language Assembly</a></b></span></h5> <p class="answer"> <span id="dlRelatedTags_lblContent_7">Assembly language: This is a symbolic language closely analogous to the instruction set of a Central Processing Unit. The program employed to translate a program written in assembly language is termed an assembler.</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/risks-associated-while-porting-a-c-pre-processor-in-java-901146.aspx' target='_balnk' >Risks associated while porting a c pre</a></b></span></h5> <p class="answer"> <span id="dlRelatedTags_lblContent_8">Q. 1 Explain number of issues which are faced while porting a c pre processor in java. These issues always have the probability that the code will show errors while trying to execute the code in java.  Q.2 : Software Requirement Specification or Requirements for the studen</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/restrictions-implied-on-api-functions-9019011.aspx' target='_balnk' >Restrictions implied on API functions</a></b></span></h5> <p class="answer"> <span id="dlRelatedTags_lblContent_9">What are the restrictions implied on API functions?</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 1433813 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> 1931254<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> 1433813</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>