Vous êtes sur la page 1sur 6

APPLETS

What is an Applet? An applet is a program written in java that can be included in an html page. When we use a javaenabled browser to view a page that contains applet, the applet's code is downloaded to our webbrowser and executed by browser's JVM. Difference between applets and text based application programs 1. Applets do not use main() for initializing the execution of the code. 2. Applets when loaded automatically call certain methods of applet class to start and execute the applet code. 3. Applets cannot run independently. They are run from inside a browser. 4. Applets cannot read from or write to the file in local computer. A sample applet program An applet program can be written only as a subclass of the superclass called Applet. This class is contained in a package called applet. Example: importjava.awt.*; importjava.applet.*; /*<applet code="sampleapplet" width=600 height=600> </applet>*/ public class sampleapplet extends Applet { public void paint(Graphics g) { g.drawString("A Simple Applet", 200, 200); }
Dept Of CSE,VKCET Page 1 MAS

} Running an Applet program There are two ways in which an applet program can be executed. 1. Executing the applet within a java compatible web browser. 2. Using an applet viewer. The applet viewer executes the applet in a default window in Java. To execute in a web browser To execute an applet in a web browser we need to write a short HTML text file that contains the appropriate applet tag. <HTML> <HEAD> <TITLE>Applet</TITLE> <HEAD> <BODY> <Applet code=Sample.class width=200 height=70></Applet> </BODY> </HTML>

Here the width and height statement specifies the dimensions of display. After creating the HTML document save the file using a name with .html extension. Eg: applet.html Inorder to view the embedded applet in the HTML just open the HTML document we have created.

Dept Of CSE,VKCET Page 2

MAS

Executing using applet viewer For executing using applet viewer include the applet code in the java source code. Example: importjava.awt.*; importjava.applet.*; //<applet code=sample width=200 height=70></applet> publicclass sample extends Applet { void paint(Graphics g) { g.drawString(This is a sample applet); } } Executing this program To compile the java program write the following command in the command prompt. C:\javac sample.java To execute in an applet viewer specify the name of the applet source file. C:\appletviewer sample.java The applet viewer will encounter the applet tag within the command and execute.

Dept Of CSE,VKCET Page 3

MAS

Applet Life Cycle Java applets inherit a set of default behavior from the applet class. When an applet is loaded it undergoes a series of changes in its state. When an applet is started the following sequence of methods calls takes place. 1. init() 2. start() 3. paint() When an applet is terminated the following sequence of method calls takes place. 1. stop() 2. destroy()

init() The init() is the first method to be called. This is called only once during the run time of the applet. Here we can initialize variables and add components like buttons, check boxes etc to appear on applet.

Dept Of CSE,VKCET Page 4

MAS

start() Applet enters the running state when the system calls the start() method of applet class. This occurs automatically after the applet is initialized. The start() method may be called more than once. It is also called to restart an applet after it has been stopped. paint() This method is called each time the applets output must be redrawn. The paint method is called several times. The paint() is also called when the applet window may be minimized and then restored. stop() The stop() is called when a web browser leaves the html document containing the applet. ie. When it goes to another page.An applet become idle when it is stopped from running. destroy() An applet is said to be dead when it is removed from memory. The destroy() is called when the environment determines that the applets need to be removed from the memory of exited. Example: import java.awt.*; importjava.applet.*; /*<applet code=lifecycle width=200 height=70></applet>*/ public class lifecycle extends Applet { int i=0, j=0, k=0; public void init() { i++; } public void start()
Dept Of CSE,VKCET Page 5 MAS

{ j++; } public void stop() { k++; } public void paint(Graphics g) { g.drawString(init=+i+start=+j+stop=+k,10,10); } } Output

Methods used in applet viewer 1. setBackground(color.green); 2. setForeground(color.blue) These methods are used for changing the background as well as the foreground color in the applet viewer screen.
Dept Of CSE,VKCET Page 6 MAS

Vous aimerez peut-être aussi