Quantcast
Viewing all articles
Browse latest Browse all 3

Difference between Applets and Applications Program in Java with Examples


(adsbygoogle = window.adsbygoogle || []).push({});

Introduction to Applets and Applications in Java

Image may be NSFW.
Clik here to view.
Venn diagram explaining features of Java Applets and Application

Venn diagram explaining features of Java Applets and Application

In java an applet is a special kind of Java program that a browser enabled with Java technology can download from the internet and run. An applet is typically embedded inside a Web page and runs in the context of the browser. In order to be provided with the standard interface between the applet and the browser environment, an applet must be a subclass of the java.applet.Applet class or javax.swing.JApplet class. Java applets are compiled using javac command, but are run either with a browser or with the appletviewer command.

Applications in java are thought to be larger than applets which runs in a desktop mode and is used as productive software application. They can be created by writing packages other than java.applet.Applet. Java application programs run in a standalone environment with the support of virtual machine(JVM). Java applications are also compiled using the javac command and run using java command. A java application has a full network and local file system access, and its potential is limited only by the creativity of its developers.

Methods used in Applets(Life cycle of an Applet)

Basically, there are five methods in the Applet class on which any applet is built.

Methods Purpose
init() This method is intended for whatever initialization is needed for an applet.
start() This method is automatically called after init() method. It is also called whenever user returns to the page containing the applet after visiting other pages. This method holds the main code of the applet.
stop() The stop() method temporarily holds the execution. This method is automatically called whenever the user moves away from the page containing applets. You can use this method to stop an animation.
destroy() This method is only called when the browser shuts down normally. This method deallocates the memory used by the applets. The stop() method is always closed before destroy().
paint() The paint() method is used to display something on the applet. It is guranteed to be called automatically when any applet begins execution. It is called when an applet must display its output such as drawing lines, ovals as well as string of characters.

Thus, the applet can be initialized once and only once, started and stopped one or more times in its life, and destroyed once and only once.

 Difference between Applets and Applications program in Java

              Applets                 Applications
Applets are small applications that are accessed on an Internet server, transported over the internet, automatically installed and run as a part of web document. Application in java are thought to be larger than applets which runs in desktop mode and is used as productive software application.
They are used to run a program on client browser. They can be executed on stand alone computer system.
Applet is portable and can be executed by any java supported browser. Needs JDK, JRE and JVM installed on client machine.
They are created by extending the java.applet.Applet package. They are created by writing packages other than java.applet.Applet.
In applet, application is initialized by init() method. Applications are initialized by main() method.
Applet application has 5 methods which will be automatically invoked on occurrance of specific event Application has a single start point which is main method.
Example:
public class MyClass extends Applet{
public void init(){}
public void start(){}
public void stop(){}
public void destroy(){}
public void paint(Graphics g){}
}
Example:
public class MyClass{
public static void main(String args[]){}
}

Ways to convert Applications to Applets

An application is a stand-alone program consisting of at least one class with a main method. Applets differ significantly from applications. First, applets do not have a main method that is automatically called to begin the program. Instead, several methods are called at different points in the execution of an applet. The difference between Java applets and applications lies in how they are run. Applications are usually run by loading the application’s main class file with a Java interpreter, such as the java tool in the JDK 6.

The basic steps to follow to convert an application program into an applet program are:

  • Make a HTML page with the appropriate tag to load the applet code.

  • Supply a subclass of Applet or JApplet class. Make this class public.

  • Eliminate the main method.

  • Override the init method to initialize your applet’s resources the same way the main method initializes the application’s resources. init method might be called more than once and should be designed accordingly.

  • Remove the call to setSize as setting the size of the applet is done in the HTML code with the WIDTH and HEIGHT parameters. Also, remove the call to setDefaultCloseOperation and setTitle which is not relevant for applets.

  • Since the applet is displayed automatically, do not call show.

 When compare these two types java programs( java applets and java applications), you may come up with the following major differences between the two:

  • The applet class is declared public so appletviewer can access it.

  • The applet class descends from Applet/JApplet, and the application class descends from Frame/JFrame.

  • The applet version has no main method.

  • The application constructor is replaced in the applet by start and init methods.

  • GUI components are added directly to the Applet, whereas in an application, GUI components are added to the content pane of its JFrame object.

Example of a simple application is java, that will be further changed into Java Applet.

import java.awt.*;
import java.awt.event.*;
public class SimpleApp{
	public static void main(String [] args){
		System.out.println(“Simple Application”);
	}
}

Converting above application into applet by making some certain changes in code:

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

public  class SimpleApplet extends Applet{
	public void init(){
		System.out.println(“Simple applet”);
	}
}
/* <applet code="“SimpleApplet.class”" width="150" height="150"></applet>*/

The post Difference between Applets and Applications Program in Java with Examples appeared first on The Computer Students.


Viewing all articles
Browse latest Browse all 3

Trending Articles