Applet

Introduction Applet is java program that can be embedded into HTML pages. Java applets runs on the java enables web browsers such as mozila and internet explorer. Applet is designed to run remotel on the client browser! so there are some restrictions on it. Applet can"t access s stem resources on the local #omputer. Applets are used to ma$e the web site more d namic and entertaining. Advantages of Applet: • Applets are cross plat%orm and can run on &indows! Mac '( and Linux plat%orm • Applets can wor$ all the version o% Java )lugin • Applets runs in a sandbox! so the user does not need to trust the code! so it can wor$ without securit approval • Applets are supported b most web browsers • Applets are cached in most web browsers! so will be *uic$ to load when returning to a web page • +ser can also have %ull access to the machine i% user allows Disadvantages of Java Applet: • Java plug,in is re*uired to run applet • Java applet re*uires J-M so %irst time it ta$es signi%icant startup time • I% applet is not alread cached in the machine! it will be downloaded %rom internet and will ta$e time • Its di%%icult to desing and build good user inter%ace in applets compared to HTML technolog Life Cycle of Applet: Applet runs in the browser and its li%ec cle method are called b J-M when it is loaded and destro ed. Here are the li%ec cle methods o% an Applet. init(): This method is called to initialized an applet start(): This method is called a%ter the initialization o% the applet. stop(): This method can be called multiple times in the li%e c cle o% an Applet. destroy(): This method is called onl once in the li%e c cle o% the applet when applet is destro ed. init () method: The li%e c cle o% an applet begins when the applet is %irst loaded into the browser and called the init/0 method. The init/0 method is called onl one time in the li%e c cle on an applet. The init/0 method is basicall called to read the )A1AM tag in the html %ile. The init /0 method retrieve the passed parameter through the )A1AM tag o% html %ile using get )arameter/0 method All the initialization such as initialization o% variables and the objects li$e image! sound %ile are loaded in the init /0 method .A%ter the initialization o% the init/0 method user can interact with the Applet and mostl applet contains the init/0 method. Start () method: The start method o% an applet is called a%ter the initialization method init/0. This method ma be called multiple times when the Applet needs to be started or restarted. 2or 3xample i% the user wants to return to the Applet! in this situation the start Method/0 o% an Applet will be called b the web browser and the user will be bac$ on the applet. In the start method user can interact within the applet.

2irst o% all we will $now about the applet. The param tag/>parma name899 value899?>Dparam?0 is used to .56 import java.raphics g07 g. #. (o! ou can pass the parameters %rom our html page to the applet embedded in our page. The applet vie#er command connects to the documents or resources designated b urls. !ere is the "ava code of program : import java. It displa s each applet re%erenced b the documents in its own window. 2or example the stop/0 method is called b the web browser on that time &hen the user leaves one applet to go another applet and the start/0 method is called on that time when the user wants to go bac$ into the %irst program or Applet.draw(tring/str! <=!<=06 : : Here is the HTML code o% the program>HTML? >@'AB? >applet code89M applet9!height89C==9 width89C==9? >Dapplet? >D@'AB? >DHTML? 3xecution Method %or Applet . E. There is onl miner di%%erence between the start/0 method and stop /0 method. This method is called onl on that time when the browser needs to (hut down.Fjavac? javac M applet.html $assing $arameter in Java Applet. 'r should be called at least one time.java #.applet. It runs on the java enabled web browser such as 4etscape navigator or Internet 3xplorer. Java source o% applet is then compiled into java class %ile and we speci% the name o% class in the applet tag o% html page.56 public class 4ew#lass extends Applet7 (tring str6 public void init/07 str 8 9This is m %irst applet96 : public void paint/. Java applet has the %eature o% retrieving the parameter values passed %rom the html page. In this example ou will see! how to write an applet program. An applet is a program written in java programming language and embedded within HTML page. Creating First Applet E ample.Fjavac?appletviewer M applet. destroy() method: The destro /0 method is called onl one time in the li%e c cle o% Applet li$e init/0 method.Stop () method: The stop/0 method can be called multiple times in the li%e c cle o% applet li$e the start /0 method. The java enabled browser loads class %ile o% applet and run in its sandbox.awt.

2or the illustration about the concept o% applet and passing parameter in applet! a example is given below. . javac applet)arameter. (tring str)arameter 8 this.9 message. -alue o% a parameter passed to an applet can be retrieved using get)arameter/0 %unction.java 'utput a%ter running the program . #ompile the program .pass the parameters to an applet.get)arameter/9Message906 i% /str)arameter 88 null0 str)arameter 8 strAe%ault6 g. To run the program using appletviewer! go to command prompt and t pe appletviewer applet)arameter.draw(tring/str)arameter! <=! C<06 : : Here is the code %or the html program . Here is the code %or the Java )rogram .9? >DA))L3T? >D@'AB? >DHTML? There is the advantage that i% need to change the output then ou will have to change onl the value o% the param tag in html %ile not in java code.56 import java. import java. In our case applet should displa 9&elcome in )assing parameter in java applet example.html Appletviewer will run the applet %or ou and and it should show output li$e &elcome in )assing parameter in java applet example. In this example! we will see what has to be done in the applet code to retrieve the value %rom parameters.g. 3.raphics g0! we prints the parameter value to test the value passed %rom html page. Applet will displa 9HelloG Java Applet9 i% no parameter is passed to the applet else it will displa the value passed as parameter.get)arameter/9Message906 $rinting the value: Then in the %unction paint /.96 public void paint/. code. Alternativel ou can also run this example %rom our %avorite java enabled browser.56 public class applet)arameter extends Applet 7 private (tring strAe%ault 8 9HelloG Java Applet.awt.>)? >A))L3T code89applet)arameter.raphics g0 7 (tring str)arameter 8 this. >HTML? >H3AA? >TITL3?)assing )arameter in Java Applet>DTITL3? >DH3AA? >@'AB? This is the applet.class9 width89H==9 height89E==9? >)A1AM name89message9 value89&elcome in )assing parameter in java applet example.applet.