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

Panels in AWT

Panels: import java.applet.*; import java.awt.*; /*<applet code="PanelDemo" width=300 height=300></applet>*/ public class PanelDemo extends Applet { public void  init() { setLayout(new BorderLayout()); Panel pn=new Panel(); Button b1=new Button("Button 1"); pn.add(b1); Button b2=new Button("Button 2"); pn.add(b2); add(pn,"North"); Panel pe=new Panel(); Button b3=new Button("Button 3"); pe.add(b3); add(pe,"East"); Panel pw=new Panel(); Button b4=new Button("Button 4"); pw.add(b4); add(pw,"West"); Panel ps=new Panel(); Button b5=new Button("Button 5"); ps.add(b5); Button b6=new Button("Button 6"); ps.add(b6); add(ps,"South"); } }

56 to 60 Java Questions

56. What is the purpose of apache tomcat? Apache server is a standalone server that is used to test servlets and create JSP pages. It is free and open source that is integrated in the Apache web server. It is fast, reliable server to configure the applications but it is hard to install. It is a servlet container that includes tools to configure and manage the server to run the applications. It can also be configured by editing XML configuration files. 57. Where pragma is used? Pragma is used inside the servlets in the header with a certain value. The value is of no-cache that tells that a servlets is acting as a proxy and it has to forward request. Pragma directives allow the compiler to use machine and operating system features while keeping the overall functionality with the Java language. These are different for different compilers. 58. Briefly explain daemon thread. Daemon thread is a low priority thread which runs in the background performs garbage collection operation for th...

41 to 45 Java Questions

41. What is nested class? If all the methods of a inner class is static then it is a nested class. 42. What is HashMap and Map? Map is Interface and Hashmap is class that implements that. 43. What are different types of access modifiers? public: Any thing declared as public can be accessed from anywhere. private: Any thing declared as private can’t be seen outside of its class. protected: Any thing declared as protected can be accessed by classes in the same package and subclasses in the other packages. default modifier : Can be accessed only to classes in the same package. 44. What is the difference between Reader/Writer and InputStream/Output Stream? The Reader/Writer class is character-oriented and the InputStream/OutputStream class is byte-oriented. 45. What is servlet? Servlets are modules that extend request/response-oriented servers, such as java-enabled web servers. For example, a servlet might be responsible for taking data in an HTML order-entry form and applyi...