***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.
***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.
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
Post a Comment