Basics To Write Applet Code

While writing applet code we must take care to import two classes ,Applet And Graphics from the java class library.The Applet class which is contained  in the Java.applet package provides life and behaviour to the applet through its methods such as init(),start() and point().Unlike the application where java calls the main() method directly to initiate the execution of the program,when an applet is loaded,java automatically calls a series of applet class for starting,running and stopping the applet code.The Applet class therefore maintains the life-cycle  of an object.

   The paint() method of  the Applet class,when it is called,actually displays the result of the applet code on the screen.The output may be text,graphics or sound.the paint() method,which requires a Graphics object as an argument,this is shown as below:-

       

         public void paint(Graphics g)

 This requires that the applet code imports the java.awt package that contains the Graphics class.All output operations of an applet are performed using the methods defined in the Graphics class.It is the thus clear from the above discussions that an applet code will have a general as shown:-

           

                   import java.awt.*;

                   import java.applet.*;

                   public class appletname extends Applet

                   {

                        ………………………..

                        ………………………..

                        public void paint (Graphics g)

                         {

                              ………………………………..

                              ………………………………..

                             }

                             ………………………………….

                             ………………………………….

                         }

The appletclassname is the main class for the applet.When the applet is loaded,Java creates an istance of this class,and then a series of  Applet class methods are called on that instance to execute the code.

 

Example Program:-

 

import java.awt.*;

import java.applet.*;

public class Hellojava extends Applet

{

     public void paint(Graphics g)

     {

            g.drawString(“Hello Java”,10,100)

       }

}

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s