Applet as java applications tutorial


Applet as java applications tutorial - contributed by Pradip Patil

Applet

Introduction

Applets are small java programs which are executed and displayed in java browser. Applet is a predefined class available in “java.applet” package. We can run Applets using a web browser or using appletviewer. It is mandatory to import java.awt package at the time of writing an applet program as applet requires support from windows and also applets are not executed by the runtime interpreter.

Applets do not start with the main () method. Outputs of the applets are handled by awt (abstract window toolkit) method instead of System.out.println () method.

Applets can be included in HTML file by using APPLET tag. If you want to include applet tag in a program itself then you have to include it by using a comment like

/*

<APPLET code=”classname” width=200 height=500>

</APPLET>

*/

Applet life cycle

When applets are loaded, they go through different phases or we can say different states and based on those states, we can create applet state diagram i.e. applet life cycle. The different Applet states are as follows.

  • Born
  • Running
  • Idle
  • Dead

These are the states of applet.In diagrammatic manner we can draw it as follow


Methods of the Applet class

Some of the predefined methods of the applet class are given below in the table.

Method Description
Void init() To initialize applet. It is called by browser or applet viewer
Void start() start the execution of the applet
Void destroyed() When applet is about to dispose, this method is called up.
Void play(URL url ,String name) Play the audio clip from given URL
Void resize(Dimension d) Request the applet to be resized
Void stop() It tells applet to stop execution

You can run an applet program in 2 ways

1) Create a separate HTML page.

2) Embed HTML code into a java file.

 

1) In this type we have to run the html page and call the java code(the applet program)

Sample.Html

<Html>
<Head>
<Body>
<Applet code=Demo.class width=200 height =200>
</applet>
</body>
</head>
</html>

Demo.java

import java.applet.*;
import java.awt.*;
public class Demo extends Applet
{
        public void paint(Graphics g)
       {  
              g.drawString(“Hello Applet”,100,100);
       }
}

2)In this type applet code is embedded into a java code

Demo.java

import java.applet.Applet;
import java.awt.*;

/*
<Applet code=Demo.class width=200 height =200>
</applet>
*/

public class Demo extends Applet
{
       public void paint(Graphics g)
       {
            g.drawString(“Hello Applet”,100,100);
         }
}

An Applet Skeleton

In order to control the execution of the applets, following are the methods defined by the applet

1)init()
2)start()
3)stop()
4)destroy()

One more method is defined by the awt component class which is

1)paint()

These 5 methods can be assembled into skeleton as follows

//applet skeleton
import java .awt.*;
import java.applet.*;

/*<applet code=Demo.class Height=200 weight=200>
</applet>
*/

public class Demo extends Applet
{

public void init()
{//initialization}
public void start()
{//start execution}
public void stop()
{//stop execution}
public void destroy()
{//shutdown activities}
public void paint(Graphics g)
{//redisplay the contain of window}

}

This is the skeleton format used by the applet code

The HTML APPLET Tag with all attributes.

The Applet tag is used to start the applet from html document as well as from the appletviewer.

Various attributes available with the standard applet tag are as follows:

Codebase It is an optional attribute. Specifies the base url of the applet code
Code It is a mandatory attribute used to specify the name of the file containing your applet compiled .class file
Alt Alt tag is an optional attribute used to specify short text message that is displayed in the browser.
Name It is an optional attribute used to specify name for the applet instances
Width and height Gives the size of the applet display area in pixel
Align Optional attribute specifies alignment of the applet
Vspace and hspace Optional attribute; vspace specifies the space above and below in pixel hsapce specifies the space on each side of the applet
Param name and values Param tag allows us to specify argument in html page using getParammeter() applet access attribute.

Creating an Applet

To create an applet it is compulsory to import 2 packages which are

a) applet Package and b) awt package to draw the applet in the paint method.

import java.applet.Applet;
import java.awt.*;

/*
<Applet code=Demo.class width=200 height =200>
</applet>
*/

public class Demo extends Applet
{
     public void paint(Graphics g)
      {
             g.drawString(“Hello Java Applet”,100,100);
       }
}

To display the output using appletviewer we have to write

Appletviewer Demo.java and you would be able to see the output on appletviewer

If you want to see the output on a browser then following changes are to be made.

1)create separate html file
2)embed applet tag in it
3)create separate java file
4)double click on the html file .

Sample.Html

<Html>
<Head>
<Body>
<Applet code=Demo.class width=200 height =200>
</applet>
</body>
</head>
</html>

Demo.java

import java.applet.*;
import java.awt.*;
public class Demo extends Applet
{
        public void paint(Graphics g)
        {
                 g.drawString(“Hello Applet”,100,100);
         }
}

Open the html file through a browser and the applet program should run through web browser.

Passing parameters to applet

For passing the parameter to the applet we have to use the param tag.

import java.applet.Applet;
import java.awt.*;

/*
<Applet code=Demo.class width=200 height =200>
<param name=str value=”Hello Java Applet”>
</applet>
*/

public class Demo extends Applet
{
         public void paint(Graphics g)
         {
                g.drawString(getParameter(“str”),100,100);
          }
}

Applet Vs Application

Applet Application
Applet needs explicit installation on the local machine. Application must run on local machine
Applet start its execution with init() method Application starts its execution with main()method.
Applet must run with GUI Application can run with or without GUI



Write your comment - Share Knowledge and Experience



 
Interview questions
Latest MCQs
» General awareness - Banking » ASP.NET » PL/SQL » Mechanical Engineering
» IAS Prelims GS » Java » Programming Language » Electrical Engineering
» English » C++ » Software Engineering » Electronic Engineering
» Quantitative Aptitude » Oracle » English » Finance
Home | About us | Sitemap | Contact us | We are hiring