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" style="width:100%;border-collapse:collapse;"> <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/explain-command-line-argument-902111.aspx' target='_balnk' >Explain Command-line argument</a></b></span></h5> <p class="answer"> <span id="dlRelatedTags_lblContent_0">Command-line argument: The arguments passed to a program whenever it is run. The Java program receives such in the single formal argument to its major method: public static void main(String[] args)</stro</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-singleton-pattern-902381.aspx' target='_balnk' >Explain Singleton pattern Singleton</a></b></span></h5> <p class="answer"> <span id="dlRelatedTags_lblContent_1">Singleton pattern: It is a pattern which permits us to make sure that only a single instance of a specific class exists at any one time. Such an instance is termed as singleton. The pattern can as well be employed whenever instances would have no excl</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/describe-limitations-of-ajax-9017737.aspx' target='_balnk' >Describe limitations of AJAX Briefly</a></b></span></h5> <p class="answer"> <span id="dlRelatedTags_lblContent_2">Briefly describe the limitations of AJAX.</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/advantages-of-object-oriented-programming-language-90213.aspx' target='_balnk' >Advantages of Object oriented</a></b></span></h5> <p class="answer"> <span id="dlRelatedTags_lblContent_3">Q. What are the advantages of Object Oriented Programming languages? Explain them. Ans. : Object oriented programming language has following advantages- <p style="text-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_4"><b><a href='https://www.tutorsglobe.com/getanswer/edit-things-in-moodle-907843.aspx' target='_balnk' >Edit things in Moodle Normal 0 false</a></b></span></h5> <p class="answer"> <span id="dlRelatedTags_lblContent_4"> Normal 0 false false <w:AlwaysShowPlacehol</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-authorities-save-their-private-keys-906427.aspx' target='_balnk' >How authorities save their Private Keys</a></b></span></h5> <p class="answer"> <span id="dlRelatedTags_lblContent_5">How do certifying authorities save their Private Keys?</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/how-java-client-access-corba-902936.aspx' target='_balnk' >How Java client access Corba A Corba</a></b></span></h5> <p class="answer"> <span id="dlRelatedTags_lblContent_6">A Corba remote object exists. How could you get a Java client to access this object?</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/state-machine-to-identify-three-consecutive-heads-909365.aspx' target='_balnk' >State machine to identify three</a></b></span></h5> <p class="answer"> <span id="dlRelatedTags_lblContent_7">Explain a finite state machine which will detect three consecutive coins tosses of one coin which results in heads?</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/state-the-term-gpo-links-909183.aspx' target='_balnk' >State the term GPO links State the term</a></b></span></h5> <p class="answer"> <span id="dlRelatedTags_lblContent_8">State the term GPO links? Explain.</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/what-is-abstract-method-902040.aspx' target='_balnk' >What is Abstract method Abstract method</a></b></span></h5> <p class="answer"> <span id="dlRelatedTags_lblContent_9">Abstract method: This is a method with the abstract reserved word in its header. The abstract method has no method body. Methods stated in an interface are for all time abstract. The body of an abstract method should be stated in a su</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 1428281 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> 1934071<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> 1428281</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> <script type="text/javascript"> //<![CDATA[ var theForm = document.forms['frm1']; if (!theForm) { theForm = document.frm1; } function __doPostBack(eventTarget, eventArgument) { if (!theForm.onsubmit || (theForm.onsubmit() != false)) { theForm.__EVENTTARGET.value = eventTarget; theForm.__EVENTARGUMENT.value = eventArgument; theForm.submit(); } } //]]> </script> <script src="/WebResource.axd?d=bWLPZmaowK3J7BdJG1GXmtSQMrxDIE_f9rI857uy9F_Ro2q0Wetm1K9TpLWVoixen8of0H-aJESemEOYROUviAtiZhk1&t=637814372020000000" type="text/javascript"></script> </form> </body> </html>