{"id":90,"date":"2018-07-11T04:59:22","date_gmt":"2018-07-11T04:59:22","guid":{"rendered":"http:\/\/itp16.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=90"},"modified":"2019-05-15T07:15:39","modified_gmt":"2019-05-15T07:15:39","slug":"applet-api","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/chapter\/applet-api\/","title":{"rendered":"Applet API"},"content":{"raw":"<div><span style=\"float: right;\"><a href=\"https:\/\/youtu.be\/tyHn7i0vnKo\" target=\"_blank\" rel=\"noopener\"><img src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a>\r\n<\/span><\/div>\r\n<div>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><strong>Applet Context<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">An instance of \u201cAppletContext\u201d is used for interaction with the browser. The instance of \u201cAppletContext\u201d is available to the Applet from the \u201cAppletStub\u201d instance. The\u00a0 methods in the \u201cAppletContext\u201d can be used to locate other applets in the Web page, load image and audio resources from any specified URL, show any document in the browser by specifying its URL, set messages in the status bar of the browser, etc.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">The \u201cAppletContext\u201d has methods to get access to the applet instances in the corresponding web page as:-<\/p>\r\n\r\n<ul style=\"text-align: justify;\">\r\n \t<li>Enumeration \u201cgetApplets()\u201d\u00a0\u00a0\u00a0 \/\/ returns an Enumeration containing all the applet instances on the corresponding web page<\/li>\r\n \t<li>Applet getApplet(String name) \u00a0\/\/ return the applet instance with the name as its id.<\/li>\r\n<\/ul>\r\n<p style=\"text-align: justify;\">The method \u201cgetApplets()\u201d returns an Enumeration containing all the applets which are loaded in the current Web page. The method \u201cgetApplet()\u201d can be used to fetch an instance of the Applet by its name, which has been specified in the Web page.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">The \u201cshowMessage()\u201d method can be used to set a message on the status bar of the browser. The \u201cshowMessage()\u201d method has a string parameter to specify the message to be set on the status bar as given in the listing below:-<\/p>\r\n\r\n<ul style=\"text-align: justify;\">\r\n \t<li>void showStatus(String msg)<\/li>\r\n<\/ul>\r\n<p style=\"text-align: left;\"><strong>What is a URL class?\u00a0<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">The URL class(defined in the \u201cjava.net package\u201d) encapsulates the information within a URL. what are the different parts of a typical url. Lets take a typical url.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><a href=\"http:\/\/forum.java.sun.com\/forum.jspa?forumID=24\">http:\/\/forum.java.sun.com\/forum.jspa?forumID=24<\/a><\/p>\r\n<p style=\"text-align: justify;\">&lt;protocol&gt;:\/\/&lt;host&gt;\/&lt;file&gt;?&lt;querystring&gt;<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">so a url has protocol, host, port, file, query string or reference, a reference is mentioned as \u201c#\u201d followed by a reference within the file.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">An instance of URL could be used to parse all the information in a URL string you can create an instance of URL by calling the constructor with string as parameter<\/p>\r\n\r\n<ul>\r\n \t<li style=\"text-align: justify;\">URL(Sring urlstring)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ url string can be any kind of url.<\/li>\r\n \t<li style=\"text-align: justify;\">URL(URL location, String s)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ url created relative to an existing URL.<\/li>\r\n<\/ul>\r\n<\/div>\r\n<div style=\"text-align: justify;\">\r\n\r\nThere are other constructor\u2019s where you can create instance of URL by specifying all the parts of the URL.\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">Now coming to the AppletContext instance, if the applet wants to interact with the browser, it can invoke methods on the AppletContext instance available to it thru the AppletStub, which was given to it in the setStub method. The methods in the AppletContext are:<\/p>\r\n&nbsp;\r\n\r\npublic void showStatus(String msg)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\/\/ show a message on the status bar of the browser.\r\n\r\npublic void showDocument(URL webpage, String target)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\/\/ load the webpage and show on \u00a0the\r\n\r\ntarget. as mentioned in the API.\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">The main activity of a browser is to load resources from a give URL. These resources could be webpage or some other content type like images and audio. In \u201cjava.awt.\u201d there is a class called Image. which can be used for rendering on any Component by using the \u201cdrawImage\u201d method of Graphics class. The \u201cAppletContext\u201d has methods to create an instance of Image from a give URL location on the web. It has methods like:<\/p>\r\n&nbsp;\r\n\r\nImage getImage(URL imagefile)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/downloads the content of image file from the URL\r\n\r\n\/\/ and creates an instance of Image from the content.\r\n\r\n&nbsp;\r\n\r\nAudioClip getAudioClip(URL audiofile)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/downloads the content of audio file from\r\n\r\n\/\/the URL and creates an instance of Audioclip\r\n\r\n\/\/from the content.\r\n\r\n&nbsp;\r\n<p class=\"hanging-indent\" style=\"text-align: left;\"><strong>Applet Class\u00a0<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">Now if we look at the methods of the Applet class. It has inherited methods from the \u201cPanel\u201d, and additional methods are the life cycle methods plus the methods which would in turn interact with the \u201cAppletStub\u201d or \u201cAppletContext\u201d. Most of these methods are the same as methods in either \u201cAppletStub\u201d or \u201dAppletContext\u201d.<\/p>\r\n&nbsp;\r\n<p class=\"hanging-indent\" style=\"text-align: left;\"><strong>Methods from the stub\u00a0<\/strong><\/p>\r\n&nbsp;\r\n\r\nString getParameter(String name) URL getCodeBase()\r\n\r\nURL getDocumentBase()\r\n\r\nvoid resize(int w, int h)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ this would in turn invoke appletResized on the\r\n\r\n\/\/corresponding stub\r\n\r\nvoid resize(Dimension d) AppletContext getAppletContext() boolean isActive()\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">Invoking any of the above methods on an instance of Applet results in the invocation of the corresponding method on the stub. The \u201cresize()\u201d method is not available in the \u201cAppletStub\u201d. The invocation of \u201cresize()\u201d method on the Applet results in the invocation of \u201cappletResize()\u201d method on the stub.<\/p>\r\n\r\n<\/div>\r\n<div style=\"text-align: justify;\">\r\n<p class=\"hanging-indent\" style=\"text-align: left;\"><strong>Methods from the AppletContext\u00a0<\/strong><\/p>\r\n&nbsp;\r\n\r\nvoid showStatus(String msg) Image getImage(URL iamgefile)\r\n\r\nImage getImage(URL location, String imagefile)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ imagefile relative to the location\r\n\r\nAudioClip getAudioClip(URL audioFile)\r\n\r\nAudioClip getAudioClip(URL location, String audioFile)\r\n\r\nvoid play(URL u) \/\/gets the AudioClip from the url and calls play on the AudioClip\r\n\r\nvoid play(URL u, String audiofile)\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">Invoking any of the above methods on an instance of Applet results in the invocation of the corresponding method on the \u201cAppletContext\u201d instance which is available within the stub. The invocation of \u201cplay()\u201d method on an instance of Applet results in the invocation of the \u201cgetAudioClip()\u201d method on the \u201cAppletContext\u201d and then the invocation of \u201cplay()\u201d method on the \u201cAudioClip\u201d instance.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">Additionally, the Applet has methods which are informative, ie. they may be overridden to provide into about the applet and its parameters.<\/p>\r\n&nbsp;\r\n\r\nString getAppletInfo()\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\/\/ u may return author name, purpose etc.\r\n\r\nString[][] getParametersInfo()\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\/\/ you may return a 2-dimension string array\r\n\r\n\/\/ describing info about the parameters which this applet may look from\r\n\r\n\/\/ the web page in the param tag\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">The \u201cgetAppletInfo()\u201d method is used to return information about the applet, usually overridden to return a copyright message for the applet, and the method \u201cgetParameterInfo()\u201d is used to return information about the parameters which this applet can understand, when specified in the applet tag. The return type for this method is a two- dimensional array of string, where we have an array for each parameter for each parameter, with three elements. The first element is the parameter name, second is the parameter type and the third is used for information about the parameter.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">These is another method which may give information about the Locale of the client, ie. the machine on which the browser is running. The instance of Locale typically encapsulates information about the Language and\u00a0 country settings. so you also have\u00a0 a method in the Applet class called Locale \u201cgetLocale()\u201d.<\/p>\r\n&nbsp;\r\n<p class=\"hanging-indent\" style=\"text-align: left;\"><strong>Audio Clip\u00a0<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">The AudioClip interface whose instance would be created when an audiofile is downloaded by the browser has following methods:<\/p>\r\nvoid play()\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">void loop()<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">void stop()<\/span>\r\n\r\n<\/div>\r\n<div style=\"text-align: justify;\">\r\n<p style=\"text-align: justify;\">The \u201cplay()\u201d method will play the audio only once, whereas the method loop() results in a repeatedly playing of the audio. The \u201cstop()\u201d method will stop the audio which is currently being played using the AudioClip instance.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">So Applet is a GUI container with capability to interact with the web browser in which it has been enbedded.<\/p>\r\n&nbsp;\r\n<p class=\"hanging-indent\" style=\"text-align: left;\"><strong>Security Issue\u00a0<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">Now if we look at the way an applet is loaded, we find that the applet can be loaded whenever a user loads a webpage in his browser. The user when loading the webpage may not have the idea whether the page contains an applet or not applet is a java code which runs inside the users browser, without the knowledge of the user. ie. The user may not specifically invoke the applet code. This could lead to some security issues. The security from applets is the responsibility of the browser. The java application which is started initially uses a \u201decurityManager\u201d to ensure that the applet does not get access to critical resources on the local machine. It would ensure that the applet is not allowed to:<\/p>\r\n&nbsp;\r\n\r\nread, write, delete or create files on the local file system.\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">It cannot open a socket connection to any host other than the host from where it was loaded. it cannot load libraries, cannot terminate the jvm (System.exit()).<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">As a Container the Applet extends from Panel and so by default it has the \u201cFlowLayout\u201d as the default \u201cLayoutManager\u201d.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">The GUI components which are used in an Applet should be the components from the \u201cjava.awt\u201d package. In order to use Swing components in an applet, we can use the \u201cJApplet\u201d class, which is a sub-class of the Applet class. The \u201cJApplet\u201d in the \u201cjavax.swing\u201d has one typical feature. ie. it can have Menus. The \u201cJApplet\u201d has most of the capabilities of \u201cJFrame\u201d.<\/p>\r\n&nbsp;\r\n<p class=\"hanging-indent\" style=\"text-align: left;\"><strong>In Summary:\u00a0<\/strong><\/p>\r\n\r\n<ul>\r\n \t<li style=\"text-align: justify;\">Most of the methods in the Applet use the instance of \u201cAppletStub\u201d and \u201cAppletContext\u201d available from the \u201cAppletStub\u201d. These methods can be used for fetching parameter values from the Web page associated with the applet. Fetching Image or \u201cAudioClip\u201d instances for a given URL, etc.<\/li>\r\n \t<li style=\"text-align: justify;\">The applets are normally restricted by the browser from accessing the local file system and network; it is also restricted from terminating the JVM. This is to protect the applet from being used to breach the security of the data on the client\u2019s machine. The security is the responsibility of the browser, which uses a security manager to manage the restrictions for the applet.<\/li>\r\n \t<li style=\"text-align: justify;\"><span style=\"text-align: justify; font-size: 1em;\">The \u201cJApplet\u201d is a Swing version of the applet and has the capability of using menus.<\/span><\/li>\r\n<\/ul>\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><strong>you can view video on Applet API<\/strong><\/td>\r\n<td><a href=\"https:\/\/youtu.be\/tyHn7i0vnKo\" target=\"_blank\" rel=\"noopener\"><img class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n\r\n<\/div>\r\n<div><strong>Suggested Reading:<\/strong><\/div>\r\n<div><\/div>\r\n<div>1. Core Java Volume 2 by Cay Horstmann &amp; Gary Cornell, Ninth Edition, Pearson Education.<\/div>\r\n<div>2. The class of JAVA by Pravin Jain, Pearson Education.<\/div>\r\n<div>3. An Introduction to Programming with Java Applets by Elizabeth S. Boese<\/div>\r\n<div>4. https:\/\/docs.oracle.com\/javase\/tutorial\/deployment\/applet\/index.html<\/div>","rendered":"<div><span style=\"float: right;\"><a href=\"https:\/\/youtu.be\/tyHn7i0vnKo\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a><br \/>\n<\/span><\/div>\n<div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><strong>Applet Context<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">An instance of \u201cAppletContext\u201d is used for interaction with the browser. The instance of \u201cAppletContext\u201d is available to the Applet from the \u201cAppletStub\u201d instance. The\u00a0 methods in the \u201cAppletContext\u201d can be used to locate other applets in the Web page, load image and audio resources from any specified URL, show any document in the browser by specifying its URL, set messages in the status bar of the browser, etc.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">The \u201cAppletContext\u201d has methods to get access to the applet instances in the corresponding web page as:-<\/p>\n<ul style=\"text-align: justify;\">\n<li>Enumeration \u201cgetApplets()\u201d\u00a0\u00a0\u00a0 \/\/ returns an Enumeration containing all the applet instances on the corresponding web page<\/li>\n<li>Applet getApplet(String name) \u00a0\/\/ return the applet instance with the name as its id.<\/li>\n<\/ul>\n<p style=\"text-align: justify;\">The method \u201cgetApplets()\u201d returns an Enumeration containing all the applets which are loaded in the current Web page. The method \u201cgetApplet()\u201d can be used to fetch an instance of the Applet by its name, which has been specified in the Web page.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">The \u201cshowMessage()\u201d method can be used to set a message on the status bar of the browser. The \u201cshowMessage()\u201d method has a string parameter to specify the message to be set on the status bar as given in the listing below:-<\/p>\n<ul style=\"text-align: justify;\">\n<li>void showStatus(String msg)<\/li>\n<\/ul>\n<p style=\"text-align: left;\"><strong>What is a URL class?\u00a0<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">The URL class(defined in the \u201cjava.net package\u201d) encapsulates the information within a URL. what are the different parts of a typical url. Lets take a typical url.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><a href=\"http:\/\/forum.java.sun.com\/forum.jspa?forumID=24\">http:\/\/forum.java.sun.com\/forum.jspa?forumID=24<\/a><\/p>\n<p style=\"text-align: justify;\">&lt;protocol&gt;:\/\/&lt;host&gt;\/&lt;file&gt;?&lt;querystring&gt;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">so a url has protocol, host, port, file, query string or reference, a reference is mentioned as \u201c#\u201d followed by a reference within the file.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">An instance of URL could be used to parse all the information in a URL string you can create an instance of URL by calling the constructor with string as parameter<\/p>\n<ul>\n<li style=\"text-align: justify;\">URL(Sring urlstring)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ url string can be any kind of url.<\/li>\n<li style=\"text-align: justify;\">URL(URL location, String s)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ url created relative to an existing URL.<\/li>\n<\/ul>\n<\/div>\n<div style=\"text-align: justify;\">\n<p>There are other constructor\u2019s where you can create instance of URL by specifying all the parts of the URL.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">Now coming to the AppletContext instance, if the applet wants to interact with the browser, it can invoke methods on the AppletContext instance available to it thru the AppletStub, which was given to it in the setStub method. The methods in the AppletContext are:<\/p>\n<p>&nbsp;<\/p>\n<p>public void showStatus(String msg)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\/\/ show a message on the status bar of the browser.<\/p>\n<p>public void showDocument(URL webpage, String target)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\/\/ load the webpage and show on \u00a0the<\/p>\n<p>target. as mentioned in the API.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">The main activity of a browser is to load resources from a give URL. These resources could be webpage or some other content type like images and audio. In \u201cjava.awt.\u201d there is a class called Image. which can be used for rendering on any Component by using the \u201cdrawImage\u201d method of Graphics class. The \u201cAppletContext\u201d has methods to create an instance of Image from a give URL location on the web. It has methods like:<\/p>\n<p>&nbsp;<\/p>\n<p>Image getImage(URL imagefile)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/downloads the content of image file from the URL<\/p>\n<p>\/\/ and creates an instance of Image from the content.<\/p>\n<p>&nbsp;<\/p>\n<p>AudioClip getAudioClip(URL audiofile)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/downloads the content of audio file from<\/p>\n<p>\/\/the URL and creates an instance of Audioclip<\/p>\n<p>\/\/from the content.<\/p>\n<p>&nbsp;<\/p>\n<p class=\"hanging-indent\" style=\"text-align: left;\"><strong>Applet Class\u00a0<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">Now if we look at the methods of the Applet class. It has inherited methods from the \u201cPanel\u201d, and additional methods are the life cycle methods plus the methods which would in turn interact with the \u201cAppletStub\u201d or \u201cAppletContext\u201d. Most of these methods are the same as methods in either \u201cAppletStub\u201d or \u201dAppletContext\u201d.<\/p>\n<p>&nbsp;<\/p>\n<p class=\"hanging-indent\" style=\"text-align: left;\"><strong>Methods from the stub\u00a0<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>String getParameter(String name) URL getCodeBase()<\/p>\n<p>URL getDocumentBase()<\/p>\n<p>void resize(int w, int h)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ this would in turn invoke appletResized on the<\/p>\n<p>\/\/corresponding stub<\/p>\n<p>void resize(Dimension d) AppletContext getAppletContext() boolean isActive()<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">Invoking any of the above methods on an instance of Applet results in the invocation of the corresponding method on the stub. The \u201cresize()\u201d method is not available in the \u201cAppletStub\u201d. The invocation of \u201cresize()\u201d method on the Applet results in the invocation of \u201cappletResize()\u201d method on the stub.<\/p>\n<\/div>\n<div style=\"text-align: justify;\">\n<p class=\"hanging-indent\" style=\"text-align: left;\"><strong>Methods from the AppletContext\u00a0<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>void showStatus(String msg) Image getImage(URL iamgefile)<\/p>\n<p>Image getImage(URL location, String imagefile)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ imagefile relative to the location<\/p>\n<p>AudioClip getAudioClip(URL audioFile)<\/p>\n<p>AudioClip getAudioClip(URL location, String audioFile)<\/p>\n<p>void play(URL u) \/\/gets the AudioClip from the url and calls play on the AudioClip<\/p>\n<p>void play(URL u, String audiofile)<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">Invoking any of the above methods on an instance of Applet results in the invocation of the corresponding method on the \u201cAppletContext\u201d instance which is available within the stub. The invocation of \u201cplay()\u201d method on an instance of Applet results in the invocation of the \u201cgetAudioClip()\u201d method on the \u201cAppletContext\u201d and then the invocation of \u201cplay()\u201d method on the \u201cAudioClip\u201d instance.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">Additionally, the Applet has methods which are informative, ie. they may be overridden to provide into about the applet and its parameters.<\/p>\n<p>&nbsp;<\/p>\n<p>String getAppletInfo()\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\/\/ u may return author name, purpose etc.<\/p>\n<p>String[][] getParametersInfo()\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\/\/ you may return a 2-dimension string array<\/p>\n<p>\/\/ describing info about the parameters which this applet may look from<\/p>\n<p>\/\/ the web page in the param tag<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">The \u201cgetAppletInfo()\u201d method is used to return information about the applet, usually overridden to return a copyright message for the applet, and the method \u201cgetParameterInfo()\u201d is used to return information about the parameters which this applet can understand, when specified in the applet tag. The return type for this method is a two- dimensional array of string, where we have an array for each parameter for each parameter, with three elements. The first element is the parameter name, second is the parameter type and the third is used for information about the parameter.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">These is another method which may give information about the Locale of the client, ie. the machine on which the browser is running. The instance of Locale typically encapsulates information about the Language and\u00a0 country settings. so you also have\u00a0 a method in the Applet class called Locale \u201cgetLocale()\u201d.<\/p>\n<p>&nbsp;<\/p>\n<p class=\"hanging-indent\" style=\"text-align: left;\"><strong>Audio Clip\u00a0<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">The AudioClip interface whose instance would be created when an audiofile is downloaded by the browser has following methods:<\/p>\n<p>void play()<\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">void loop()<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">void stop()<\/span><\/p>\n<\/div>\n<div style=\"text-align: justify;\">\n<p style=\"text-align: justify;\">The \u201cplay()\u201d method will play the audio only once, whereas the method loop() results in a repeatedly playing of the audio. The \u201cstop()\u201d method will stop the audio which is currently being played using the AudioClip instance.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">So Applet is a GUI container with capability to interact with the web browser in which it has been enbedded.<\/p>\n<p>&nbsp;<\/p>\n<p class=\"hanging-indent\" style=\"text-align: left;\"><strong>Security Issue\u00a0<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">Now if we look at the way an applet is loaded, we find that the applet can be loaded whenever a user loads a webpage in his browser. The user when loading the webpage may not have the idea whether the page contains an applet or not applet is a java code which runs inside the users browser, without the knowledge of the user. ie. The user may not specifically invoke the applet code. This could lead to some security issues. The security from applets is the responsibility of the browser. The java application which is started initially uses a \u201decurityManager\u201d to ensure that the applet does not get access to critical resources on the local machine. It would ensure that the applet is not allowed to:<\/p>\n<p>&nbsp;<\/p>\n<p>read, write, delete or create files on the local file system.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">It cannot open a socket connection to any host other than the host from where it was loaded. it cannot load libraries, cannot terminate the jvm (System.exit()).<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">As a Container the Applet extends from Panel and so by default it has the \u201cFlowLayout\u201d as the default \u201cLayoutManager\u201d.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">The GUI components which are used in an Applet should be the components from the \u201cjava.awt\u201d package. In order to use Swing components in an applet, we can use the \u201cJApplet\u201d class, which is a sub-class of the Applet class. The \u201cJApplet\u201d in the \u201cjavax.swing\u201d has one typical feature. ie. it can have Menus. The \u201cJApplet\u201d has most of the capabilities of \u201cJFrame\u201d.<\/p>\n<p>&nbsp;<\/p>\n<p class=\"hanging-indent\" style=\"text-align: left;\"><strong>In Summary:\u00a0<\/strong><\/p>\n<ul>\n<li style=\"text-align: justify;\">Most of the methods in the Applet use the instance of \u201cAppletStub\u201d and \u201cAppletContext\u201d available from the \u201cAppletStub\u201d. These methods can be used for fetching parameter values from the Web page associated with the applet. Fetching Image or \u201cAudioClip\u201d instances for a given URL, etc.<\/li>\n<li style=\"text-align: justify;\">The applets are normally restricted by the browser from accessing the local file system and network; it is also restricted from terminating the JVM. This is to protect the applet from being used to breach the security of the data on the client\u2019s machine. The security is the responsibility of the browser, which uses a security manager to manage the restrictions for the applet.<\/li>\n<li style=\"text-align: justify;\"><span style=\"text-align: justify; font-size: 1em;\">The \u201cJApplet\u201d is a Swing version of the applet and has the capability of using menus.<\/span><\/li>\n<\/ul>\n<table>\n<tbody>\n<tr>\n<td><strong>you can view video on Applet API<\/strong><\/td>\n<td><a href=\"https:\/\/youtu.be\/tyHn7i0vnKo\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<div><strong>Suggested Reading:<\/strong><\/div>\n<div><\/div>\n<div>1. Core Java Volume 2 by Cay Horstmann &amp; Gary Cornell, Ninth Edition, Pearson Education.<\/div>\n<div>2. The class of JAVA by Pravin Jain, Pearson Education.<\/div>\n<div>3. An Introduction to Programming with Java Applets by Elizabeth S. Boese<\/div>\n<div>4. https:\/\/docs.oracle.com\/javase\/tutorial\/deployment\/applet\/index.html<\/div>\n","protected":false},"author":4,"menu_order":13,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["mr-pravin-jain"],"pb_section_license":""},"chapter-type":[],"contributor":[58],"license":[],"class_list":["post-90","chapter","type-chapter","status-publish","hentry","contributor-mr-pravin-jain"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/chapters\/90","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/wp\/v2\/users\/4"}],"version-history":[{"count":8,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/chapters\/90\/revisions"}],"predecessor-version":[{"id":401,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/chapters\/90\/revisions\/401"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/chapters\/90\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/wp\/v2\/media?parent=90"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/chapter-type?post=90"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/wp\/v2\/contributor?post=90"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/wp\/v2\/license?post=90"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}