Presentation is loading. Please wait.

Presentation is loading. Please wait.

Applet in Java.

Similar presentations


Presentation on theme: "Applet in Java."— Presentation transcript:

1 Applet in Java

2 Overview An applet is a Java program that runs in a Web browser. An applet can be a fully functional Java application because it has the entire Java API at its disposal. There are some important differences between an applet and a standalone Java application, including the following − An applet is a Java class that extends the java.applet.Applet class. A main() method is not invoked on an applet, and an applet class will not define main().

3 Overview Applets are designed to be embedded within an HTML page.
When a user views an HTML page that contains an applet, the code for the applet is downloaded to the user's machine. A JVM is required to view an applet. The JVM can be either a plug-in of the Web browser or a separate runtime environment.

4 Overview The JVM on the user's machine creates an instance of the applet class and invokes various methods during the applet's lifetime. Applets have strict security rules that are enforced by the Web browser. The security of an applet is often referred to as sandbox security, comparing the applet to a child playing in a sandbox with various rules that must be followed. Other classes that the applet needs can be downloaded in a single Java Archive (JAR) file.

5 Life Cycle of an Applet Four methods in the Applet class gives you the framework on which you build any serious applet − init − This method is intended for whatever initialization is needed for your applet. It is called after the param tags inside the applet tag have been processed. start − This method is automatically called after the browser calls the init method. It is also called whenever the user returns to the page containing the applet after having gone off to other pages.

6 Life Cycle of an Applet stop − This method is automatically called when the user moves off the page on which the applet sits. It can, therefore, be called repeatedly in the same applet. destroy − This method is only called when the browser shuts down normally. Because applets are meant to live on an HTML page, you should not normally leave resources behind after a user leaves the page that contains the applet. paint − Invoked immediately after the start() method, and also any time the applet needs to repaint itself in the browser. The paint() method is actually inherited from the java.awt.

7 Hierarchy of Applet JApplet Applet Panel Container Component Object As displayed in the above diagram, Applet class extends Panel. Panel class extends Container which is the subclass of Component.

8 java.applet.Applet class
For creating any applet java.applet.Applet class must be inherited. It provides 4 life cycle methods of applet. public void init(): is used to initialized the Applet. It is invoked only once. public void start(): is invoked after the init() method or browser is maximized. It is used to start the Applet. public void stop(): is used to stop the Applet. It is invoked when Applet is stop or browser is minimized.

9 java.applet.Applet class
public void destroy(): is used to destroy the Applet. It is invoked only once. There are two ways to run an applet By html file. By appletViewer tool (for testing purpose).

10 Simple example of Applet by html file
To execute the applet by html file, create an applet and compile it. After that create an html file and place the applet code in html file. Now click the html file.

11 import java.applet.Applet; import java.awt.Graphics;
//First.java   import java.applet.Applet;   import java.awt.Graphics;   public class First extends Applet{      public void paint(Graphics g){   g.drawString("welcome",150,150);   }    }  

12 Simple example of Applet by html file
myapplet.html <html>   <body>   <applet code="First.class" width="300" height="300">   </applet>   </body>   </html>  

13 Applet by appletviewer tool
Simple example of Applet by appletviewer tool To execute the applet by appletviewer tool, create an applet that contains applet tag in comment and compile it. After that run it by: appletviewer First.java. Now Html file is not required but it is for testing purpose only.

14 Applet by appletviewer tool
Simple example of Applet by appletviewer tool //First.java   import java.applet.Applet;   import java.awt.Graphics;   public class First extends Applet{    public void paint(Graphics g){   g.drawString("welcome to applet",150,150);  }  }   /*  <applet code="First.class" width="300" height="300">  </applet>  */  

15 Output To execute the applet by appletviewer tool, write in command prompt: c:\>javac First.java c:\>appletviewer First.java

16 Example of Graphics in applet
Following example demonstrates how to add first n natural numbers by using the concept of stack .

17 Example of Graphics in applet import java.applet.Applet;
import java.awt.*;   public class GraphicsDemo extends Applet{   public void paint(Graphics g){   g.setColor(Color.red);   g.drawString("Welcome",50, 50);   g.drawLine(20,30,20,300);   g.drawRect(70,100,30,30);   g.fillRect(170,100,30,30);   g.drawOval(70,200,30,30);   g.setColor(Color.pink);   g.fillOval(170,200,30,30);   g.drawArc(90,150,30,30,30,270);   g.fillArc(270,150,30,30,0,180);   }  } 

18 Example of Graphics in applet myapplet.html <html> <body>
<applet code="GraphicsDemo.class" width="300" height="300">   </applet>   </body>   </html>  


Download ppt "Applet in Java."

Similar presentations


Ads by Google