In JAVA we write two types of programs or applications. They are standalone applications and distributed applications.
A standalone application is one which runs in the context of local disk and whose result is not sharable. Every standalone application runs from command prompt and it contains main method along with System.out.println statements.
A distributed application is one which runs in the context of browser or World Wide Web and it can be accessed across the globe. Any technology which runs in the browser will have 'n' number of life cycle methods and it does not contain main methods and System.out.println statements.
In JAVA, SUN micro initially has developed a concept called applets which runs in the context of browser. "An applet is a JAVA program which runs in the context of browser or World Wide Web".
In order to deal with applets we must import a package called java.applet.*. This package contains only one class Applet whose fully qualified name is java.applet.Applet.
Since applets are running in the browser, the class Applet contains the life cycle methods.
Life cycle methods are also called as loop back methods.
In java.applet.Applet we have four life cycle methods. They are public void init (), public void start (), public void stop () and public void destroy ().
Another method which is not a life cycle method is public void paint (). This is the method which will be called by the browser after completion of start method. This method is used for displaying the data on to the browser. Paint method is internally calling the method called drawString whose prototype is given below.
java.awt.Graphics
(Graphics => public void drawString (String, int row position, int column position))
An object of Graphics class will be created automatically after loading the applet into the browser.
Using HTML program: In order to run the applet through HTML program we must use the following tag.
Syntax:
<applet code =".class file of the applet" height = height value width = width value> </applet>
For example:
File name: MyApp.html
<HTML> <HEAD> <TITLE> My applet example </TITLE> </HEAD> <BODY> <APPLET code="MyApp" height=100 width=150> </APPLET> </BODY> </HTML>
Appletviewer is a tool supplied by SUN micro system to run the applet programs from the command prompt (in the case of browser is not supporting).
Syntax:
appletviewer filename.java
For example:
appletviewer MyApp.java
When we use appletviewer to run the above applet, MyApp.java program must contain the following tag within the multi line comment.
/*<applet code= "MyApp" height=300 width=300> </applet>*/
Write an applet program which displays "AshaKrishna My Love"?
Answer:
/*<applet code= "MyApp" height=300 width=300> </applet>*/ import java.applet.Applet; public class MyApp extends Applet { public void paint(java.awt.Graphics g) { g.drawString ("AshaKrishna MyLove", 20, 15); } };
Note: To set the font we must use a class called Font.
Syntax:Font f=new Font ("arial", Font.BOLD, 40);
In the Graphics class we have the following method which will set the font.
java.awt.Graphics public void setFont (Font fobj) For example: g.setFont (f);
Write a JAVA program which illustrates the life cycle methods of applet?
Answer:
import java.applet.*; import java.awt.*; /*<applet code="MyApp1" height=300 width=300> </applet>*/ public class MyApp1 extends Applet { String s=" "; public void init () { setBackground (Color.green); setForeground (Color.red); s=s+" INIT "; } public void start () { s=s+" START "; } public void stop () { s=s+" STOP "; } public void destroy () { s=s+" DESTROY "; } public void paint (Graphics g) { Font f=new Font ("arial", Font.BOLD, 40); g.setFont (f); g.drawString (s,100,100); } };