Java Tutorial

Applet's

In JAVA we write two types of programs or applications. They are standalone applications and distributed applications.

A standalone application is one which runs in the context of local disk and whose result is not sharable. Every standalone application runs from command prompt and it contains main method along with System.out.println statements.

A distributed application is one which runs in the context of browser or World Wide Web and it can be accessed across the globe. Any technology which runs in the browser will have 'n' number of life cycle methods and it does not contain main methods and System.out.println statements.

In JAVA, SUN micro initially has developed a concept called applets which runs in the context of browser. "An applet is a JAVA program which runs in the context of browser or World Wide Web".

In order to deal with applets we must import a package called java.applet.*. This package contains only one class Applet whose fully qualified name is java.applet.Applet.

Since applets are running in the browser, the class Applet contains the life cycle methods.

Life cycle methods are also called as loop back methods.

In java.applet.Applet we have four life cycle methods. They are public void init (), public void start (), public void stop () and public void destroy ().

  1. Public void init (): This is the method which is called by the browser only one time after loading the applet. In this method we write some block of statements which will perform one time operations, such as, obtaining the resources like opening the files, obtaining the database connection, initializing the parameters, etc.
  2. Public void start (): After calling the init method, the next method which is from second request to sub-sequent requests the start method only will be called i.e., short method will be called each and every time. In this method we write the block of statement which provides business logic.
  3. Public void stop (): This id the method which is called by the browser when we minimize the window. In this method we write the block of statements which will temporarily releases the resources which are obtained in init method.
  4. Public void destroy (): This is the method which will be called by the browser when we close the window button or when we terminate the applet application. In this method we write same block of statements which will releases the resources permanently which are obtained in init method.

Another method which is not a life cycle method is public void paint (). This is the method which will be called by the browser after completion of start method. This method is used for displaying the data on to the browser. Paint method is internally calling the method called drawString whose prototype is given below.

java.awt.Graphics

(Graphics => public void drawString (String, int row position, int column position))

An object of Graphics class will be created automatically after loading the applet into the browser.

Steps for Developing Applet Program:

  1. Import java.applet.Applet package.
  2. Choose the user defined class that must extends java.applet. Applet class and ensure the modifier of the class must be public.
  3. Overwrite the life cycle methods of the applet if require.
  4. Save the program and compile.
  5. Run the applet: To run the applet we have two ways. They are using HTML program and using applet viewer tool.

Using HTML program: In order to run the applet through HTML program we must use the following tag.

Syntax:

<applet code =".class file of the applet" height = height value width = width value>
</applet>

For example:

File name: MyApp.html

<HTML>
    <HEAD>
        <TITLE> My applet example </TITLE>
    </HEAD>

    <BODY>
    <APPLET code="MyApp" height=100 width=150>
    </APPLET>
    </BODY>
</HTML>

Using appletviewer:

Appletviewer is a tool supplied by SUN micro system to run the applet programs from the command prompt (in the case of browser is not supporting).

Syntax:

appletviewer filename.java

For example:

appletviewer MyApp.java

When we use appletviewer to run the above applet, MyApp.java program must contain the following tag within the multi line comment.

/*<applet code= "MyApp" height=300 width=300> </applet>*/

Write an applet program which displays "AshaKrishna My Love"?

Answer:

/*<applet code= "MyApp" height=300 width=300> </applet>*/ 
import java.applet.Applet;
public class MyApp extends Applet
{
    public void paint(java.awt.Graphics g)
    {
        g.drawString ("AshaKrishna MyLove", 20, 15);
    }
};

Note: To set the font we must use a class called Font.

Syntax:

 Font f=new Font ("arial", Font.BOLD, 40);

In the Graphics class we have the following method which will set the font.

java.awt.Graphics
public void setFont (Font fobj)
For example:
g.setFont (f);

Write a JAVA program which illustrates the life cycle methods of applet?

Answer:

import java.applet.*;
import java.awt.*;
/*<applet code="MyApp1" height=300 width=300> </applet>*/

public class MyApp1 extends Applet
{
    String s=" ";
    public void init ()
    {
        setBackground (Color.green); setForeground (Color.red); s=s+" INIT ";
    }
    public void start ()
    {
        s=s+" START ";
    }
    public void stop ()
    {
        s=s+" STOP ";
    }
    public void destroy ()
    {
        s=s+" DESTROY ";
    }
    public void paint (Graphics g)
    {
        Font f=new Font ("arial", Font.BOLD, 40);
        g.setFont (f);
        g.drawString (s,100,100);
    }
};