You are on page 1of 15

APPLETS

When a html page wants to communicate with the user on Internet, it can use a special java program called as applet to communicate and respond to the user. The user can interact by typing some details or by clicking the components available in the applet program. The program then processes and then displays results. APPLET = JAVA BYTE CODE + HTML PAGE That is, we can understand an applet as a java byte code embedded in a html page. Therefore, applet can be defined as java byte code embedded in a web page. The java.applet package provides an API ( application programming interface) that gives applets some capabilities that applications dont have. Ex: applets can play sounds which other programs cant do yet. When you use a Java technology-enabled browser to view a page that contains an applet, the applet's code is transferred to your system and executed by the browser's Java Virtual Machine (JVM). Applets are classified as: LOCAL APPLET & REMOTE APPLET Local applet is developed by user and it is present in our local machine only. Remote applet is developed by others and is present in internet. It can be downloaded to our local machine and it can run in our machine also.
Syntax for writing html code: <html> <head> <title>. . . . . <\title> <body>

. . . . . . . . . . . . <\body> <\head> <\html>

Creating an APPLET:
To create an applet we need to write a java program and compile it to get byte code.

Then we should include it into a HTML page on a particular location wherever we want it to be displayed. This page is the stored in web server. A client machine communicates with web server, the server then sends the HTML page that contains the applet. The page is then transmitted to the client where the applet is executed on the clients web browser. Thus, applets are executed at client side by the web browser. Thus, applets travel thousands of kilometers of distance on internet and reach the client machines before their execution on the client. To create an applet we have Applet class of java.applet package and JApplet class of javax.swing package. The classes use the following methods which are automatically run by any applet program. So these methods should be overridden in applet program. 1.public void init(): This method is the first method to be called by the browser and it is executed only once.

So the programmer can use this method to initialize any variables, creating components and creating threads etc., When execution of this method is completed browser looks for the next method start(). 2.Public void start():
This method is called after init() method and each time the applet is revisited by the user. For example, the user has minimized the web page that contains the applet and moved to another page then this methods execution is stopped. When the user comes back to view the webpage again start() method execution will resume. Any calculations and processing of data should be done in this method and the results are also displayed. 3. Public void stop(): This method is called by the browser when the applet is to be stopped. If the user minimizes the web page, then this method is called and when the user again comes back to this page, then start() method is called. In this way, start() and stop() methods can be called repeatedly. For example, an applet with some animation might want to use the start() method to resume animation, and the stop() method to suspend animation. 4. public void destroy(): This method is called when applet is being terminated from memory. The stop() method will always be called before destroy(). The code related to releasing the memory allocated to the applet and stopping any running threads should be written in this method.

EXECUTING init(),start(),stop() AND destroy() METHODS IN THE ABOVE SEQUENCE IS CALLED AS LIFE CYCLE OF APPLET. Note: writing all these methods is not compulsory while writing an applet. Applet dosent have main() method. That is, public static void main(String args[]) is not available in applet. So applet code can be compiled but cannot be executed by JVM.

Now the question is where does this applets run? Once the applet is created we compile and obtain its byte code. This byte code is embedded in HTML page and the page is sent to client computer. The client machine contains a browser like HTML page and the page in sent to client computer. The client machine contains a browser like internet explorer, netscape navigator or Mozilla firefox where HTML page is viewed by user. The same b rowser will execute the applet of the HTML page. The browser contains a small virtual machine called applet engine which understands and run the applet.

APPLET LIFE CYCLE: Begin (load Applet) Born initialization

Start() Stop() Running

Display
Paint()

Idle
Start()

stopped

Destroy()

Destroyed

Dead

Exit of Browser

Applet state transition diagram


Initialization state:

Applet enters the initialization state when it is first loaded. This is achieved by calling the init() method of Applet class. Create objects needed by the applet. Set up initial values. Load images or fonts. Set up colors

Running state: Applet enters the running state when the system calls the start() method of Applet class. This occurs automatically after the applet is initialized. Starting can also occur if the applet is already in stopped (idle) state.

Idle or Stopped State: An applet becomes idle when it is stopped from running. Stopping occurs automatically when we leave the page containing the currently running applet. We can calling the stop () method explicitly.

Dead State: An applet is said to be dead when it is removed from memory. This occurs automatically by invoking the destroy() method when we quit the browser.

Display State: Applet moves to the display state whenever it has to perform some output operation on the screen. This happens immediately

after the applet enters into the running state. To display a message we can take the help of paint() method of comoponent class of java.awt package.

An applet life cycle is born with init() method and starts functioning with start() method. To stop the applet the stop() method is called and to terminate the applet completely from memory the destroy() method is called. Once the applet is terminated, we should reload the HTML page again to get the applet start once again from init() method. This cyclic way of executing the methods is called as applet life cyle.

CREATING A SIMPLE APPLET Java program to create an applet with pink color background and a message import java.awt.*; import java.applet.*; public class myapplet extends Applet { public void init() { setBackground(Color.pink); } public void paint(Graphics g) { g.drawString("this is my first applet",50,100); } }
Save the above program as : myapplet.java Compile the above program as: myapplet.java Now myapplet.class file is created. This byte code should be embedded inti a html page using <applet> tag as shown below:

<html> <applet code = "myapplet.class" </applet> </html>

height=200 width=400>

Save the above code as myapplet.html This html page contains the applet which can be opened in the browser or an applet viewer supplied by sun micro systems can be used to test the applet. For this purpose, open browser and write the path of .html file Along with directory path. The applet will be opened in the browser or give the following command in command prompt : appletviewer myapplet.html

output:

Java program to create applet with some background color and font color with a message. This message string is stored in msg and is displayed in paint() method. import java.awt.*; import java.applet.*; public class myapplet1 extends Applet { String msg=" "; public void init() { setBackground(Color.yellow); setForeground(Color.red); Font f = new Font("Arial",Font.BOLD,20); setFont(f); msg +=" this is init"; } public void start() { msg += " this is start"; } public void stop() { msg += " this is stop"; } public void destroy() { msg +=" this is destroy"; } public void paint(Graphics g) { g.drawString(msg,50,100); } }

<html> <applet code = "myapplet1.class" </applet> </html>

height=200 width=400>

Output:

Program to draw lines import java.awt.*; import java.applet.*;

public class {

lines extends Applet

public void paint(Graphics g) { setBackground(Color.cyan); g.drawLine(10,20,170,40); g.setColor(Color.pink); g.drawLine(170,40,80,140); g.setColor(Color.green); g.drawLine(80,140,10,20); } } <html> <applet code = "lines.class" </applet> </html> height=200 width=400>

Program to create rectangle import java.awt.*; import java.applet.*; public class { rectangle extends Applet

public void paint(Graphics g) { setBackground(Color.pink); g.drawRect(30,30,60,60); } } <html> <applet code = "rectangle.class" </applet> </html> height=150 width=300>

Program to fill rectangle with a color import java.awt.*; import java.applet.*; public class { public void paint(Graphics g) { setBackground(Color.pink); rectangle extends Applet

g.drawRect(30,30,60,60); g.fillRect(30,30,60,60); setForeground(Color.yellow); } } <html> <applet code = "rectangle.class" </applet> </html> height=150 width=300>

Program to draw polygons import java.awt.*; import java.applet.*; public class { int x1[]={100,10,100,200,100}; int y1[]={10,100,200,100,10}; int n1=5; int x2[]={200,300,400,300,200}; int y2[]={100,200,100,10,100}; int n2=5; public void paint(Graphics g) polygon extends Applet

{ g.drawPolygon(x1,y1,n1); g.fillPolygon(x2,y2,n2); } } <html> <applet code = "polygon.class" </applet> </html> height=250 width=450>

Program to display a banner which is moving import java.awt.*; import java.applet.*; /* <applet code = "Banner" width = 200 height = 80> </applet> */ public class Banner extends Applet implements Runnable { Thread t; String s; public void init() { t = new Thread(this);

s = "This is a moving banner"; t.start(); } public void run() { char ch; while(true) { try{ repaint(); Thread.sleep(250); ch = s.charAt(0); s = s.substring(1, s.length()); s += ch; } catch(InterruptedException e) { System.out.println("Exception occured: " + e); } } } public void paint(Graphics g) { g.drawString(s, 20, 20); } }

You might also like