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

What is Servet ?

A servlet is a java class that extends an application hosted on a web server. Handles the HTTP request-response process (for our purposes) Often thought of as an applet that runs on a server. Provides a component base architecture for web development, using the Java Platform The foundation for Java Server Pages (JSP). Alternative to CGI scripting and platform specific server side applications.

Latest Java 6 to 10

6. What are the access modifiers in java ? There are 3 access modifiers. Public, protected and private, and the default one if no identifier is specified is called friendly, but programmer cannot specify the friendly identifier explicitly. 7. What is are packages? A package is a collection of related classes and interfaces providing access protection and namespace management. 8. What is meant by Inheritance and what are its advantages? Inheritance is the process of inheriting all the features from a class. The advantages of inheritance are reusability of code and accessibility of variables and methods of the super class by subclasses. 9. What is the difference between superclass and subclass? A super class is a class that is inherited whereas sub class is a class that does the inheriting. 10. What is an abstract class? An abstract class is a class designed with implementation gaps for subclasses to fill in and is deliberately incomplete.

51 to 55 Java Questions

51. What is the difference between a constructor and a method? A constructor is a member function of a class that is used to create objects of that class. It has the same name as the class itself, has no return type, and is invoked using the new operator. A method is an ordinary member function of a class. It has its own name, a return type (which may be void), and is invoked using the dot operator. 52. What will happen to the Exception object after exception handling? Exception object will be garbage collected. 53. Difference between static and dynamic class loading. Static class loading: The process of loading a class using new operator is called static class loading. Dynamic class loading: The process of loading a class at runtime is called dynamic class loading. Dynamic class loading can be done by using Class.forName(….).newInstance(). 54. Explain the Common use of EJB The EJBs can be used to incorporate business logic in a web-centric application. The EJBs can b...