Applet

Application                        Applet
1) console                          Browser
2) main()                            init()            once in life cycle
                                         start     maximize    multiple time
                                         stop    minimize    multiple time
                                         destroy            once in life cycle
3) System.out.println()        paint()

1) An applet in a Java class file embedded in a htm; page which is loaded and executed by the browser
2) Applets require browser where as Applications are console (DOS prompt) based.
3) The entry point in an Application in the main() method where as the life cycle methods of an Applet are init(), start(), stop() and destroy().
4) init() and destroy() are called only once in the life cycle where as start() and stop() is called whenever the Applet is maximized and minimized respectively.
5) In an Application for printing we use System.out.println() method where as in Applet we use the paint() method.

import java.applet.*;
import java.awt.*;
public class MyApplet extends Applet
{
    public void paint(Graphics g)
    {
        g.drawString("Hello",100,100);
    }
}
Save this file as MyApplet.java and compile. Don’t execute the file.
In NotePlus - Tools - Configure User Tools - java - [ ] Capture Output (Remove)

Click new - File - html     and save this file as MyApplet1.html

<BODY>

<APPLET code=MyApplet.class width=400 height=400 >

</APPLET>

</BODY>

Save this file MyApplet.html and press CTRL+3 to see appletview. When you click CTRL+3, MyApplet.html file get executed which run Applet MyApplet.class which prints “Hello” at 100th row and 100th column.
Next Applet takes values (parameters) from HTML page and print it on the screen.

import java.applet.*;
import java.awt.*;
public class ParamApplet extends Applet
{
    String s;
    int x,y;
    public void init()
    {
        s = getParameter("N");
        String s1 = getParameter("X");
        String s2 = getParameter("Y");
        x = Integer.parseInt(s1);
        Integer j = new Integer(s2);
        y=j.intValue();
    }
    public void paint(Graphics g)
    {
        g.drawString(s,x,y);
    }
}

Click new -> File -> html    and save this file as ParamApplet1.html

<BODY BGCOLOR="#FFFFFF">

<Applet code=ParamApplet.class width=400 height=400>

<Param name="N" value="ABC">

<Param name="X" value="100">

<Param name="Y" value="200">

</Applet>

</BODY>


OUTPUT : ABC get printed at 100th row and 100th column
Life Cycle of an Applet : String is an immutable class whereas StringBuffer is a mutable object which means any changes.
We use the toString() method to convert an Object to a string.

import java.applet.*;
import java.awt.*;
public class LifeCycleApplet extends Applet
{
    StringBuffer sb;
    public void init()
    {
        sb = new StringBuffer();
        sb.append("init ");
    }
    public void start()
    {
        sb.append("start ");
    }
    public void stop()
    {
        sb.append("stop ");
    }
    public void paint(Graphics g)
    {
        g.drawString(sb.toString(),100,100);
    }
}

Click new -> File -> html    and save this file as ParamApplet1.html

<BODY BGCOLOR="#FFFFFF">

<Applet code=ParamApplet.class width=400 height=400>

<Param name="N" value="ABC">

<Param name="X" value="100">

<Param name="Y" value="200">

</Applet>

</BODY>


OUTPUT : ABC get printed at 100th row and 100th column
Life Cycle of an Applet : String is an immutable class whereas StringBuffer is a mutable object which means any changes.
We use the toString() method to convert an Object to a string.

import java.applet.*;
import java.awt.*;
public class LifeCycleApplet extends Applet
{
    StringBuffer sb;
    public void init()
    {
        sb = new StringBuffer();
        sb.append("init ");
    }
    public void start()
    {
        sb.append("start ");
    }
    public void stop()
    {
        sb.append("stop ");
    }
    public void paint(Graphics g)
    {
        g.drawString(sb.toString(),100,100);
    }
}

Click new -> File -> html    and save this file as LifeCycleApplet.html

<BODY BGCOLOR="#FFFFFF">

<APPLET code=LifeCycleApplet.class height=400 width=400>

</APPLET>

</BODY>









Method In Applet :











import java.applet.*;
import java.awt.*;
import java.net.*;
public class CodeDocTextApplet extends Applet
{
    URL u1, u2;
    public void init()
    {
        u1 = getCodeBase();
        u2 = getDocumentBase();
    }
    public void paint(Graphics g)
    {
        g.drawString(u1.toString(),50,50);
        g.drawString(u2.toString(),50,100);
    }
}

Click new -> File -> html    and save this file as CodeDocTextApplet.html

<BODY>

<APPLET code=CodeDocTextApplet.class HEIGHT=200 WIDTH=400 >

</APPLET>

</BODY>