300 likes | 561 Views
Applet:. An applet is a java program that is transmitted over the network from the server to client & executed within clients browser. Applets are used to provides dynamic content & interactivity at client side. hierarchy. Java.lang.Object java.awt.Component java.awt.panel
E N D
Applet: An applet is a java program that is transmitted over the network from the server to client & executed within clients browser. Applets are used to provides dynamic content & interactivity at client side.
hierarchy • Java.lang.Object • java.awt.Component java.awt.panel java.applet.Applet
Difference between java Application & java Applet • 1) is a standalone program • 1) is executed by the browser • 2) executed using standalone JVM • 2) browser provides JVM • 3) entry point is the main() method • 3)Several entry point called by browser like init() • 4) can be graphical • 4) almost graphical.
1.0 Webserver client HTML 1.3 1.1 Applet Applet Classes 1.2 1.4 HTML contents Applet component
1.0 send request for a page • 1.1 HTML page with an embedded Applet is Returned. • 1.2 shows the HTML contents & Applet component. • 1.3 Applet object is created & life cycle method are invoked • 1.4 content of Applets are displayed in the panel in browser.
An applet is a managed object & has life cycle methods. • 1) A managed object is an object that is created & managed by run time environment . In case of Applet objects are created & managed by the browser. • Each managed object has life cycle method that determines sequence of operations that can be performed by a managed object. • Each Applet is represented by a class that extends • Java.applet.Applet class. • This class provides life cycle methods of an applet .
Init(): this method is invoked only once on an applet object just after its created this method is provided to executed initialization code. • Syntax: public void init(); • Start(): this method is invoke initially after init method & after that each time execution on Applet is resumed. This method denotes the starting point of execution of an Applet. • stop(): this method is invoke when the execution of Applet is suspended. • Public void stop(); • Paint(): this method is used to draw the content of applet . • It is invoke initially after start & after that each time repaint method is invoked.
Or contents of the Applet are redrawn. • Public void paint(Graphics g); • destroy():-it is invoked when Applet object is unloaded from the memory . • public void destroy();
Client Browserr Graphics Applet WebBrowser 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8
2.0 2.1 3.0 3.1 3.2 4.0 4.1
1.0 types the URL of a web page • 1.1 send the request for the specified web page • 1.2 web page within an applet is returned • 1.3 Applet object is created • 1.4 init(); • 1.5 start(); • 1.6 getGraphics(); • 1.7 Graphics Object is returned & pass into paint(). • 1.8 call paint method. • 2.0 request other page • 2.1 stop() • 3.0 client request previous page • 3.1 start(); • 3.2 paint(); • 4.0 client ends communication with the server • 4.1 destroy();
For each GUI component in an interface a graphics object is created when the component is make visible . This graphics object can be used to drawing content on the component . • Ex if we have the following interface Graphics Graphics Name Text Frame/graphics Submit Button Label
For this interface ,graphics object shall be created when this interface make be visible. • Graphics class provides following methods to draw component on a component. • drawString():- is used to display a string on a component. Public void drawString(String text,intleft,int top); top String left
drawLine(): is used to draw a line on a Component. • Public void drawLine(int x1,int y1,int x2,int y2); • drawRect():- public void drawRect (int left, int top, int width, int height); • drawOval():- it is used to draw oval & circle. • Note: use same signature for draw circle & pass the same coordinate for square . • getGraphics():- method of component class is used to obtain the reference of graphics obj. for a component . • Public graphics getGraphics(); • Note: if getGraphics() method is invoke on a invisible component then null is returned.
An Applet has the following limitation :- • 1) it does not have main method. • 2) an applet can not access contact of local file system.(for security point of view). • 3) An applet can only initiate socket communication with the host from where it was downloaded. Client Server Applet Applet For security reason Host A
Note :1) Applet is the subclass of panel so give the color. • Note 2)main purpose of applet of interactivity . Number Guessing Game To play the game ,type a number between 0 to 100. Enter number here Guess
INTER APPLET COMMUNICATION : • Inter applet communication is facilitate with the help of applet context. Applet context represent run time environment in which Applet are executed . Each time applet object is created run time environment store its reference into the applet object.
Reference of Applet context provided to each Applet object RUN TIME ENVIROMENT Applet context Applet1 Applet2 200 300 100 Applet2 Ap[plet1 100 100 300 200 Reference of applets that are currently executed in applet context.
getAppletContext():- method of applet class is used to obtain the reference of applet context in an applet . • Public AppletContextgetAppletContext(); • appletcontext provides following methods:- • GetApplet():- it is used to obtain the reference of an applet from the appletcontext. Public Applet getApplet(String name);
PASSING PARAMETER TO APPLET • Parameters can be passed to an applet from the HTML page . • <param> subtag of applet is use to define a parameter for an applet in the HTML page . • <applet > • <param name=“parameter name” • value =“parameter value”/> • </applet>
getParameter():- it is a method ,of applet class is used to read the value of a parameter in an applet. • Public String getParameter(String parameter name); • Following Sequence of event occurs: • When an applet object is created by run time environment .
HTML PAGE Applet Applet Context RunTimeEnvironment 1.0 1.1 1.2 1.3 1.4
1.0 Read applet Tag & Identified Applet class • 1.1 class is loaded & its instance is created • 1.2 reference of appletcontext is stored • 1.3 name & value of all parameters defined for applet are read. • 1.4 all parameters(Name/ value) are stored in applet object. It is an object, getParameters method call on its & it provides all details with this reason given in figure. APPLet
LIFE CYCLE OF AN APPLET OBJECT CREATED INITILIZATION NEWINTANSE(); INIT(); START(); STOP(); Destroyed() Destroyed RUNNING Paint();