Skip to main content

What is Applet ?

***What is an Applet?


An Applet is a program that can be
referenced by an HTML source code of a
web page.


It is dynamically downloaded from a web
server to a browser and it is executed in the
environment provided by the browser.



  • Applet Viewer can also be used to execute  the applet.


***FirstApplet.java

import java.applet.Applet;
import java.awt.Graphics;
/*<applet code=“FirstApplet” width=200 height=200>
</applet>*/
public class FirstApplet extends Applet
{
public void paint(Graphics g)
{
g.drawString(“Hello World”,20,100);
}
}


*** In the above example,

The first two line is importing the java.applet and
java.awt.Graphics package.

The third line is the comment entry which is used only for
applet viewer to execute the applet using the HTML source
code.

The next line declares the FirstApplet class which extends
Applet class. Note that each applet created by user must
extend the Applet class.

There is a paint() method defined which accepts the object of
Graphics class. This method is invoked automatically
whenever the applet is displayed.

The drawString() method is defined in the class Graphics and
it takes three arguments, message and x and y co-ordinates.
Appletviewer is used to execute the applet.



***LIFE CYCLE OF APPLET

Normally, any program starts execution
from the main() method of a class.

An apple executes in an environment
provided by the browser or an appletviewer.

There are total four methods that are called
during the life cycle of an applet. They are:
init(), start(), stop() and destroy().

All these methods are defined in the
java.applet.Applet class.

The init() method is called when the applet
begins execution. The code which is to be
executed only once is placed in this method.


The start() method is executed after the
init() method completes execution. This
method is also called when an appletviewer
or the browser resumes the execution of the
applet.

The stop() method is called by the applet
viewer or web browser to suspend
execution of an applet. Therefore, the
start() and stop() methods may be called
multiple time during the life cycle of the
applet.

The stop() method is also called when the
browser calls another web page.

Finally, the destroy() method is called by the
applet viewer or web browser before the
applet is terminated.

The stop() method is invoked before the
destroy() method.



EXAMPLE :

import java.applet.Applet;
import java.awt.Graphics;
/*<applet code="AppletLifeCycle" width=200
height=200></applet>*/

public class AppletLifeCycle extends Applet
{
String str="";
public void init()
{
str+="Init:";
}
public void start()
{
str+="Start:";
}
public void stop()
{
str+="Stop";
}
public void destroy()
{
System.out.println("Destroy");
}
public void paint(Graphics g)
{
g.drawString(str,20,20);
}
}


***GRAPHICS CLASS
A graphics object encapsulates a set of
methods that can perform graphics output.


It allows to draw lines, ovals, rectangles,
strings, images, characters and arcs.




Comments

Popular posts from this blog

46 to 50 Java Questions

46. What is Constructor? A constructor is a special method whose task is to initialize the object of its class. It is special because its name is the same as the class name. They do not have return types, not even void and therefore they cannot return values. They cannot be inherited, though a derived class can call the base class constructor. Constructor is invoked whenever an object of its associated class is created. 47. What is an Iterator ? The Iterator interface is used to step through the elements of a Collection. Iterators let you process each element of a Collection. Iterators are a generic way to go through all the elements of a Collection no matter how it is organized. Iterator is an Interface implemented a different way for every Collection. 48. What is the List interface? The List interface provides support for ordered collections of objects. Lists may contain duplicate elements. 49. What is memory leak? A memory leak is where an unreferenced object that will ne...

36 to 40 Java questions

36. What is the difference between yielding and sleeping? When a task invokes its yield() method, it returns to the ready state. When a task invokes its sleep() method, it returns to the waiting state. 37. What is the difference between preemptive scheduling and time slicing? Under preemptive scheduling, the highest priority task executes until it enters the waiting or dead states or a higher priority task comes into existence. Under time slicing, a task executes for a predefined slice of time and then reenters the pool of ready tasks. The scheduler then determines which task should execute next, based on priority and other factors. 38. What is mutable object and immutable object? If a object value is changeable then we can call it as Mutable object. (Ex., StringBuffer, …) If you are not allowed to change the value of an object, it is immutable object. (Ex., String, Integer, Float, …) 39. What is the purpose of Void class? The Void class is an uninstantiable placehold...

26 to 30 Questions of Java

26. What is the difference between this() and super()? this() can be used to invoke a constructor of the same class whereas super() can be used to invoke a super class constructor. 27. What is Domain Naming Service(DNS)? It is very difficult to remember a set of numbers(IP address) to connect to the Internet. The Domain Naming Service(DNS) is used to overcome this problem. It maps one particular IP address to a string of characters. For example, www. mascom. com implies com is the domain name reserved for US commercial sites, moscom is the name of the company and www is the name of the specific computer, which is mascom’s server. 28. What is URL? URL stands for Uniform Resource Locator and it points to resource files on the Internet. URL has four components: http://www. address. com:80/index.html, where http – protocol name, address – IP address or host name, 80 – port number and index.html – file path. 29. What is RMI and steps involved in devel...