Applet Programing

  • View
    224

  • Download
    0

Embed Size (px)

Text of Applet Programing

  • 7/28/2019 Applet Programing

    1/36

    Applet Programming

  • 7/28/2019 Applet Programing

    2/36

    What is Applet

    An applet is a special kind of Javaprogram which is used for internet

    programming.

    The applets reside in a server and theycan be downloaded from the server to the

    client machine and executed on client

    machine provided that the browser shouldbe enabled with Java technology.

  • 7/28/2019 Applet Programing

    3/36

    Types of Applet

    Applets are basically of two types.1. Local Applet These are applets which are

    created for standalone PC's where the same

    PC is distinguished as server and client.

    Hence, to execute a local applet there is no

    need of internet connection.

    2. Remote Applet The applets which are

    developed for an environment where theserver and the client machine are physically

    apart from each other are called remote

    applets

  • 7/28/2019 Applet Programing

    4/36

    Needs of Applet

    1. Applet is a window based application.2. Applets are basically used to create

    dynamic pages.

    3. The difference of an applet program froma normal program is that it does not

    contain the main function. So it has no

    entry point.4. The program starts executing from some

    functions present in Applet class.

  • 7/28/2019 Applet Programing

    5/36

    Need for applet

    5. an applet program cannot be executedindependently as done in case of normal

    application programs.

    6. They must be executed with the help ofeither applet viewer or with the help of

    any web browser.

  • 7/28/2019 Applet Programing

    6/36

    Creating an applet program

    We need to import two packages for writing anapplet program

    Import java.lang.awt.*;

    Import java.lang.applet.*;

    The java.lang.awt.* package provides a set ofclasses and using those classes we can create awindow component and it also contains a graphicclass;

    the java.lang.applet.* package is used as we areusing an applet class which is present in appletpackage.

  • 7/28/2019 Applet Programing

    7/36

    7

    of

    30Applet Class

    An applet class is a member of JavaApplication Programming Interface(API)

    Package.

    An applet class is extended from the PanelClass, which is further extended from the

    classes Container, Component and

    Object. Object class is the member class of the

    java.lang package and is called in the

    program automatically.

  • 7/28/2019 Applet Programing

    8/36

    8

    of

    30Applet Class

    The component, container and Panelclasses are the members of java.awt

    package that provide visual components

    such as label,button and text fields. Anapplet is the only member of the

    java.applet package.

  • 7/28/2019 Applet Programing

    9/36

    9

    of

    30Java Class Hierarchies

  • 7/28/2019 Applet Programing

    10/36

    10

    of

    30Abstract windows Toolkit(AWT)

    The awt package is a collection of classesand methods that allow an end user to

    design and manage graphical user

    interface(GUI) based application. AWT is used to support applet windows

    and it also helps in creating independent

    windows such as frame window or panelwindow that run in GUI environment.

  • 7/28/2019 Applet Programing

    11/36

    11

    of

    30Abstract windows Toolkit(AWT)

    AWT is javas Largest Package that canbe included in java program by giving the

    java.awt.* statement.

    It is a part of java foundation classes(JFC). AWT has 63 classes and 14 interfaces

    used for creating user interfaces.

  • 7/28/2019 Applet Programing

    12/36

    12

    of

    30Features of the AWT package

    It provides graphics and imaging tools thatenable you to develop GUI.

    It provides layout manager that enables an

    end user to develop window layouts. It supports event handling. for example, an

    error occurred in between execution of a

    program.

  • 7/28/2019 Applet Programing

    13/36

    13

    of

    30Hierarchy of java.awt package

  • 7/28/2019 Applet Programing

    14/36

    14

    of

    30Component Class

    Component class is at the top of the AWTinheritance class hierarchy. It is the super

    class and all others in the hierarchy inherit

    its characteristics directly or indirectly. Component class is the abstract class that

    encapsulated all the properties of visual

    components or object.

  • 7/28/2019 Applet Programing

    15/36

    15

    of

    30Methods of the component class

    void setSize(int width, int height): definesthe size of a component in terms of wisth and

    size.

    void setLocation(int x,int y): specifies thelocation of a components, in the window, at the

    position specified by the x and y coordinates.

    void setBounds(int x,int y,int width ,intheigth): sets the bounds of a component in

    terms of position x and y and size in terms of

    width and height.

  • 7/28/2019 Applet Programing

    16/36

    16

    of

    30Methods of the component class

    Void setForeground(Color b): sets theforeground color of the component.

    Void setBackground(Color b): sets the

    background color of the component.

  • 7/28/2019 Applet Programing

    17/36

    17

    of

    30Container class

    Container class is the subclass of component

    class.

    Methods that are used to manipulate

    components on container objects are:

    void add(object o): Nest a Component or an

    object inside a container.

    void remove(object o): removes a component

    or an object from a container.

    void removeALL(): removes all the

    components or objects that are placed inside a

    container.

  • 7/28/2019 Applet Programing

    18/36

    When we want to execute an applet programwith the help of a web browser, we need to

    write an HTML file.

    ExampleofApplet

  • 7/28/2019 Applet Programing

    19/36

    Applet Programming

    importjava.lang.awt.*;importjava.lang.applet.*;publicclassmessageextendsApplet

    {

    Stringmsg;publicvoidinit()

    {

    msg=Welcometoappletprogramming;

    }

    publicvoidpaint(Graphicsg)

    { g.drawString(msg,100,400); }}

  • 7/28/2019 Applet Programing

    20/36

  • 7/28/2019 Applet Programing

    21/36

    R i th l t i W b b

  • 7/28/2019 Applet Programing

    22/36

    Running the applet in a Web browser

  • 7/28/2019 Applet Programing

    23/36

    23

    of

    30

    APPLET LIFE CYCLE

  • 7/28/2019 Applet Programing

    24/36

    Applet Life Cycle

  • 7/28/2019 Applet Programing

    25/36

    Applet

    Public keyword is used because we run anapplet program in a media different from

    that in which we write it.

    Paint() method used in the above examplecalls drawString() function which is a

    function of Graphics class.

    Applet class contains certain functions likeinit(), start(), stop(), paint(), destroy(), etc.

    which are usually used in programs.

  • 7/28/2019 Applet Programing

    26/36

    Method init()

    init() methodWhenever an applet isloaded into a web page this method is

    automatically called. This method provides

    an appropriate place for initialization of thevariables required and other set up

    operations such as setting of background

    color, defining the applets layout, parsing

    parameters, etc.

  • 7/28/2019 Applet Programing

    27/36

    Method start()

    start() method After the initialization iscompleted, start() method is called.

    The difference between this method and the

    init() method is that init() method is calledonly when the page is loaded, whereas

    start() method is called every time the page

    is loaded and every time the page is

    restarted.

    This method is mainly used while

    implementing threads in Java.

  • 7/28/2019 Applet Programing

    28/36

    Method stop()

    stop() method This method is calledwhenever the user navigates from one

    page to another page.

    It is always advisable to use stop()method if any type of animation or memory

    consuming activities is being performed in

    order to avoid wasting of systemresources.

  • 7/28/2019 Applet Programing

    29/36

    Method destroy()

    destroy() method This method is calledimmediately before the applet exits.

    The exiting operation is caused by either

    the applet operation requesting for the exitor may be the web browser is shut down.

    When it is called it directs the applet to

    free up all the system resources beingused by it.

  • 7/28/2019 Applet Programing

    30/36

    Methods in applet

    /* */

    import java.awt.*;

    import java.applet.*;public class balli1 extends Applet

    {

    String str="Hello ";

  • 7/28/2019 Applet Programing

    31/36

    31

    of

    30Methods in applet

    public void init(){

    str=str+"";

    }

    public void start()

    {

    str=str+" ";

    }

  • 7/28/2019 Applet Programing

    32/36

    32

    of

    30Methods in applet

    public void stop(){

    str=str+"";

    }

    public void destroy()

    {

    str=str+"";

    }

    33

  • 7/28/2019 Applet Programing

    33/36

    33

    of

    30Methods in applet

    public void paint(Graphics g){