Pages

java.applet package

What is Applet ?

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. It does not contain main methods and System.out.println statements.

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.Aleppt.




        1. java.lang.Object
        2. java.awt.Component
        3. java.awt.Container
        4. java.awt.Panel
        5. java.awt.Applet
        6. javax.swing.JApplet
     
As displayed in the above diagram. Applet class extends Panel. Panel class extends Container which is the subclass of Component.


Lifecycle of an Applet

  1. Applet is initialized.
  2. Applet is started.
  3. Applet is painted.
  4. Applet is stopped.
  5. Applet is destroyed.


Lifecycle methods for Applet

The java.applet.Applet class provides 4 life cycle methods and java.awt.Component class provides 1 life cycle method for an applet. For creating any applet java.applet.Applet class must be inherited. It provides 4 life cycle methods of applet.

public void init()

It is used to initialize the Applet. It is invoked only once.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.

public void start()

After calling the init method, this method invoked. It is used to start the Applet. start method will be called multiple times.

public void stop()

It is used to stop the Applet. It is invoked when Applet is stop ro browser is minimized. stop method will be called multiple times.

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.

public void paint(Graphics g)

It is used to paint the Applet. It provides Graphics class object that can be used for drawing oval, rectangle, arc etc.


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, because its object is created by java Plug-in software that resides on the browser.
  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.
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.

Example of Applet by html file

//Save your file as First.java

import java.applet.Applet;
import java.awt.Graphics;

public class First extends Applet
{
public void paint(Graphics g)
{
g.drawString("Welcome to hungry4java.blogspot.com",50,50);
}
}

//Save it as myapplet.html

<html>
<body>
<applet code="First.class" width="300" height="300"></applet>
</body>
</html>
-------------------------------------------------------------------------------------
Now open command prompt goto the directory where your program is saved and write:
  1. javac First.java 
  2. appletViewer myapplet.html or you can double click on myapplet.html file then it will open in a browser.


No comments:

Post a Comment