Applet Programming

Applets

            Applets are small java programs that are primarily used in internet programming.It Can be transported over the internet from one computer to another.It uses appletviewer or any other web browsers for execution.we can embed applets into webpages in two ways

  Local applets:can write our own applets and embed into webpages

  Remote applets:can download an applet from remote computer and then embed into webpages

Applets vs Applications

    Applets do not use main() method for execution

    Applets can’t run independently.

    can’t read or write to the files in local computers

    Can’t communicate with other servers on the n/w

    Applets can’t run any program from local system

    Are restricted from using libraries from other lang.

 

Steps involved in developing and testing applet

1.                  Building an applet code(.java file)

2.                  Creating an executable applet(.class file)

3.                  Designing a web page using html tags

4.                  Preparing <applet> tag

5.                  Incorporating <applet> tag into webpage

6.                  Creating html file

7.                  Testing the applet code

 

Building an applet code

    Appet code uses the services of Applet and Graphics class of java library

    Applet class contained in java.applet package

§  Provides life and behaviour to applet through init() , start() and paint()

§  Paint() is used to displays the result of the code

Syntax:            public void paint(Graphics g)

    Graphics class contained in java.awt package

§  all output operations of an applet are performed using methods defined in this class

 

Syntax of an applet code











        The Hellojava applet


Import java.awt.*;

Import java.applet.*;

Public class Hellojava  extends Applet

{
            public void paint(Graphics  g)

            {

                        g.drawString(‘Hello java”,10,100);

            }

}
           

 

Applet life cycle

    The applet states include

§  Born or initialization state

§  Running state

§  Idle state

§  Dead or destroyed state

 




Initialization state    

    Applet enters to the initialization state when it is first loaded

    Achieved by init() of Applet Class

    Main functions here include

§  Create object needed

§  Set up initial values

§  Load images or fonts

§  Setup colors

Public void init()

{

            . . . . . . .

            . . . . . . .(Action)

            . . . . . . .

}

 

Running state

    Enters to this state when system calls start() method

    It occurs automatically after the applet is initialized

    Also happens when an applet is idle and ready to execute

    Start() method can be called more than once

Public void start()

{

            . . . . . . .

            . . . . . . .(Action)

            . . . . . . .

}

 

 

Idle or stopped state

    An applet become idle when it is stoped from running

    Stopping occurs when leave the page currently running

    We can do this explicitly by stop() method

 

Public void stop()

{

            . . . . . . .

            . . . . . . .(Action)

            . . . . . . .

}

 

Dead state

    Said dead once it is removed from memory

    Occurs automatically by invoking the destroy()

    It occurs only once in the applet life cycle

 

Public void destroy()

{

            . . . . . . .

            . . . . . . .(Action)

            . . . . . . .

}

 

Display state

    Applet moves to the display state whenever it has to perform some output operations on screen

    It happens immediately after the applet enters running state

    Paint() method is used to accomplish this task

 

 

Public void paint(Graphics g)

{

            . . . . . . .

            . . . . . . .(Display statements)

            . . . . . . .

}

 

Designing a webpage

    A web page is designed embedding the applet tag in the body section of the page

    Applet tag supplies the name of the applet to be loaded.

<APPLET

            CODE=“java_appletpgm.class”

            WIDTH=400

            HEIGHT=200

</APPLET>

 

    Display area for the applet output will be 400 pixel width and 200 pixel height

Running the applet

    We must have following files

§  hellloJava.java

§  helloJava.class

§  Hellojava.html

 

    After the success full completion of the compilation process of the “hellojava.java pgm and saving the html file

§  Running of applet is done by

§   appletviewer hellojava.html

 

The Hellojava applet

Import java.awt.*;

Import java.applet.*;

Public class Hellojava  extends Applet

{
            public void paint(Graphics  g)

            {

                        g.drawString(‘Hello java”,10,100);

            }

}
           

 

Hellojava.html

<HTML>

<HEAD>

            <TITLE>welcome to java applets</TITLE>

</HEAD>

<BODY>

<APPLET

CODE=“helloJava.class”

WIDTH=400

HEIGHT=200>

</APPLET>

</BODY>

</HTML>

 

Running the applet

    >javac hellloJava.java

    >appletviewer hellojava.html

 

 

 

 

Passing parameters to applets

    By using <PARAM…> Tag

    Each <PARAM…> Tag has name and value attribute

    Eg: name: as color and value as red

<Applet ……>

<param=color value=“red”>

</applet>

 

The AppletHellojavaParam

Import java.awt.*;

Import java.applet.*;

Public class AppletHellojavaParam  extends Applet

{

            String str;

            public void init()

            {

                        str=getParameter(“string”);

                        if(str==null)  str=“Java”

                        str=“Hello”+str;

            }

            public void paint(Graphics  g)

            {

                        g.drawString(str,10,100);

            }

}
           

 

Html file:HellojavaParam.html

<HTML>

<HEAD>

            <TITLE>welcome to java applets</TITLE>

</HEAD>

<BODY>

<APPLET

CODE=“AppletHellojavaParam.class”

WIDTH=400

HEIGHT=200>

<PARAM        

            NAME=“string”

            VALUE=“Applet!    “>

</APPLET>

</BODY>

</HTML>

 

Running the applet

    >javac AppletHellojavaParam.java

    >appletviewer HellojavaParam.html

 

 

No comments:

Post a Comment