HNDApplet

Embed Size (px)

Citation preview

  • 8/9/2019 HNDApplet

    1/21

    4. Applets and Applications

    4.1 How applets and Applications are different.

    An application is a program that you execute from the Ms DOS Prompt. Stand aloneapplications can be either windows based or console applications.

    Applets are java programs that execute inside a web page. herefore unli!e applications"applets re#uire a java$embedded browser. An applet is loaded and executed when a user loadsa web page through a web browser.

    An applet is simple to use since all that the user has to do to start the applet is to access a webpage. An applet can only access the resources of the host machine% it cannot access the fieldson the local computer. he main feature of an applet is that it does not have a main method.

    4.2 Life Cycle of an applet.

    he applet class contains four methods that control the creation and execution of an applet on aweb page. hese methods are&

    Method Description

    init'(

    his is called when an applet is loaded intocomputer memory for the first time. )t isexecuted automatically by the system. heinit'( method can be used to initiatevariables and components such asbuttons.

    start'(

    its called immediately after the init'(method. )t is executed each time you visitother pages and return to the pagecontaining the applet. *ou can use thismethod to restart a process each time auser visits a page.

    stop'(each time an applet losses its focus a stopmethod is called.

    destroy'(.destroy'( is called when you start viewinganother web page. +ava calls the stop'(method before calling destroy'(.

    1 | P a g e

  • 8/9/2019 HNDApplet

    2/21

    int

    start

    stop

    destroy

    Life cycle of the Applet &

    4.3. A Simple Applet

    his program shows how to write a simple java applet and demonstrates compilation andexecuting of an applet.

    /*This is an example of a Java Applet*/

    Import java.applet.*;Import java.awt.Graphics;

    public class WelcomeApplet exten s Applet!

    public voi paint"Graphics #$!

    #. raw%trin#"&Welcome to Java'() +) +$;,//en of metho paint"$,//en of class WelcomeApplet

    4.4. Compiling an Applet

    ,se the javac command to compile an applet. Applets are executed by a web browser from an- M file. *ou need to create an - M file containing a tag called /APP 0 1 that incorporates

    Applets into web pages.

    he following - M file contains a tag to invo!e 2elcomeApplet.class

    -html-bo-applet co e0(WelcomeApplet.class( wi th012+ hei#ht02+-/applet-/bo-html

    he width and height attributes specify the rectangular viewing area of the applet.

    2 | P a g e

  • 8/9/2019 HNDApplet

    3/21

    4.5. !ec"ting Applets

    *ou can view java applet using java3s applet viewer. he following command involves

    WelcomeApplet.html

    Appletviewer WelcomeApplet.html

    4.#. $raphics class and paint method

    4.#.1 $raphics class

    he 4raphics class represents the display Area of an Applet. he 4raphics class is part of the java.awt pac!age and it is used to draw images within the display area of the applet. heobjects of the graphics class are used for painting the applet.

    4.#.2. paint%& method

    he paint'( method draws the graphics of an applet in the drawing area. he method isautomatically called when an applet is displayed on the screen for the first time and each timethe applet receives focus. he paint'( method of the applet ta!es an object of the graphics classas the parameter.

    4.#.3. 'rawing Applet program

    -html-bo-applet co e0(WelcomeApplet.class( wi th012+ hei#ht02+-/applet-/bo-html

    he width and height attributes specify the rectangular viewing area of the applet

    import java.applet.*;import java.awt.Graphics;

    public class 3rawin#Applet exten s Applet!

    4ublic voi paint"Graphics #$!

    #. raw5ine" +) +)2+)6+$;#. raw7ect" +)1+)1+)6+$;#. raw%trin#"&Welcome to Java()6+) +$;

    ,//en of metho paint,//en of class 3rawin#Applet

    he width and height attributes specify the rectangular viewing area of the applet

    3 | P a g e

  • 8/9/2019 HNDApplet

    4/21

    5reate your Applet&

    2rite the - M file 5ompile the program 6un the applet viewer

    *ou can use the 4raphics class to draw text" lines" circles" rectangles etc few of the methods of 4raphics class are used in the above program&

    rawstrin#"$raw7ect"$raw5ine"$

    4.( Animations in Applet

    he following program demonstrates how to use the 4raphic class to create basic animationsusing 7ey istener and MouseMotion istener.

    import java.applet.*;import java.awt.*;import java.awt.event.*;

    public class 8ouse exten s Applet implements 8ouse8otion5istener)9e 5istener !

    int wi th) hei#ht;%trin# s0::;

    int mx) m ; // the mouse coor inates boolean is utton4resse 0 false;

    public voi init"$ ! wi th 0 #et%i

  • 8/9/2019 HNDApplet

    5/21

    m 0 e.#et "$; show%tatus" :8ouse at ": B mx B :): B m B :$: $; repaint"$; ,

    public voi mouse3ra##e " 8ouse?vent e $ !// calle urin# motion with buttons own

    mx 0 e.#et@"$; m 0 e.#et "$; show%tatus" :8ouse at ": B mx B :): B m B :$: $; repaint"$; ,

    public voi =e 7elease " 9e ?vent e $ ! ,public voi =e 4resse " 9e ?vent e $ ! ,

    public voi =e T pe " 9e ?vent e $ ! char c 0 e.#et9e >har"$; if " c '0 9e ?vent.>CA7DEF3? IF?3 $ !

    s 0 s B c; repaint"$; , , public voi paint" Graphics # $ ! if " is utton4resse $ ! #.set>olor" >olor.#reen $; , else ! #.set>olor" >olor.#ra $; ,

    #.set>olor">olor.re $;

    #. raw%trin#"s)mx)m $; ,,//-applet co e0 :8ouse .class: wi th0 6++ hei#ht0 6++ -/applet

    he Output will be as follows.

    5 | P a g e

  • 8/9/2019 HNDApplet

    6/21

    Object

    CardLayoutFlowLayoutGridBagLayoutGridLayout BorderLayout

    4.) Layo"t *anagers

    ayout managers are special objects that determine how components are organi8ed on a user interface. A layout manager specifies the layout of the components in a container. 5ontainershold components. 2hen you create a container" +ava automatically creates and assigns adefault layout manager to it. he layout manager determines the placement of the componentsin the container.

    he 5omponent class is a superclass for all 4,) controls. )t includes methods for handlingevents" colors" images" and fonts. hese methods are available to the subclasses of the5omponent class. he 5ontainer class is derived from the 5omponent class and defines thecomponents that can contain other components such as buttons and labels.

    4.).1. +sing Layo"ts

    *ou can set different layout managers for different containers in an application. Presented hereis a list of the commonly used layout managers in +ava&

    9low layout 4rid layout :order layout 5ard layout :ox layout 4rid bag layout

    0ach layout manager is represented by a class of the same name. hese classes are extendedfrom the superclass object. he Object class hierarchy is illustrated below&

    # | P a g e

  • 8/9/2019 HNDApplet

    7/21

    4.).2. ,he -low Layo"t *anager

    he flow layout manager is the default layout manager for an applet. *ou can use the flowlayout manager to place controls se#uentially in rows in the order in which you add controls to acontainer. 2hen the layout manager reaches the right border of the applet" it begins placingcontrols in the next row. )n its default state" the flow layout manager centers the controls in eachrow.

    he create a layout manager for a container" you need to create an instance of the appropriatelayout class. ;ext" invo!e the set ayout'( method for the container to specify the layout to beused to place components. All layout manager classes are available in the java.awt pac!age.

    ( | P a g e

  • 8/9/2019 HNDApplet

    8/21

    he following code creates a flow layout&

    import java.awt.*;import java.applet.*;public class low5a out?xample exten s Applet !

    // >reate la out mana#erlow5a out la out 0 new low5a out"$;

    5abel lblHne 0 new 5abel":Hne:$;5abel lblTwo 0 new 5abel":Two:$;5abel lblThree 0 new 5abel":Three:$;5abel lbl our 0 new 5abel": our:$;5abel lbl ive 0 new 5abel": ive:$;5abel lbl%ix 0 new 5abel":%ix:$;

    public voi init "$ !

    // choose the flow la out mana#erset5a out"la out$;a "lblHne$;a "lblTwo$;a "lblThree$;a "lbl our$;a "lbl ive$;a "lbl%ix$;

    ,

    ,//-applet co e0: low5a out?xample.class: wi th0 6++ hei#ht0 6++ -/applet

    Output will be as follows.

    ) | P a g e

  • 8/9/2019 HNDApplet

    9/21

    | P a g e

  • 8/9/2019 HNDApplet

    10/21

    4.).3.,he $rid Layo"t *anager

    Although the flow layout manager is the simplest layout manager" you may not be able to use itto create sophisticated frames and applets. 2hen you need greater control on placingcomponents" the grid layout can be used.

    he following code creates a grid layout&

    import java.awt.*;import java.applet.*;

    public class Gri 5a out?xample exten s Applet!

    //create our la out mana#erGri 5a out la out 0 new Gri 5a out"1)6$;

    //create six buttonsutton btnHne 0 new utton":Hne:$;utton btnTwo 0 new utton":Two:$;utton btnThree 0 new utton":Three:$;utton btn our 0 new utton": our:$;utton btn ive 0 new utton": ive:$;utton btn%ix 0 new utton":%ix:$;

    //Gri 5a out?xample class >onstuctorpublic voi init"$!

    //choose the Gri 5a out la out mana#erset5a out"la out$;

    //a our labels

    a "btnHne$;a "btnTwo$;a "btnThree$;a "btn our$;a "btn ive$;a "btn%ix$;

    ,

    Output as follows

    1/ | P a g e

  • 8/9/2019 HNDApplet

    11/21

    4. ,he A0stract indow ,ool it

    he abstract 2indow ool!it 'A2 ( is a pac!age that provides an integrated set of classes tomanage user interface components li!e windows" dialogs" buttons" chec! boxes" lists" menus"scroll bars" and text fields. he 5omponent class" which implements the common functionality"is the super class for all graphical interface elements.

    4. .1. Components

    he methods of the component class allow you to control the internal state and on$screenappearance of all the components. he following diagram depicts the component hierarchy.

    4. .2. Controls

    5lasses involved

    5ontrols 5lass

    ext box ext9ield

    ext area extAreaPush button :utton

    abel abel

    5hec! box 5hec!:ox

    6adio :utton 5hec!box4roup with 5hec!:ox

    5ombo box 5hoice

    11 | P a g e

    5omponent

    5hec!boxMain5omponent5ontainer ext5omponent:utton

    Menu)temMenu:ar Menu2indowPanelext9ield extArea

    9rame Dialog Applet

  • 8/9/2019 HNDApplet

    12/21

    ist box ist

    12 | P a g e

  • 8/9/2019 HNDApplet

    13/21

    ,he La0el class

    abels do not generate an action event

    Syntax

    he text label can be aligned to the left" the right or the center of the label. he values for alignment are abel. 09 " abel.6)4- " abel.50; 06.

    0xample5abel label 0 new 5abel"&Fame($;To ali#n the text to the center) use5abel label 0 new 5abel "&Fame() 5abel.>?FT?7$;

    abels.java

    //Import the Abstract Win ow Tool=it "AWT$ classesimport java.awt.*;import java.applet.*;

    public class 5abels exten s Applet!//create three labels5abel lblTitle 0 new 5abel ":Cere are couple of labels :$;5abel lbl%toc=% mbol 0 new 5abel": E?:$;5abel lbl%toc=4rice 0 new 5abel": 2+.2:$;

    Koi init"$!

    //choose the low5a out la out mana#erset5a out"new low5a out"$$;

    //a our labelsa "lblTitle$;a "lbl%toc=% mbol$;a "lbl%toc=4rice$;

    ,,

    ,he "tton Class

    :uttons are used to trigger events in a 4,) environment. hey are easy to manage and" mostimportantly" they are easy to use. he :utton class is used to create buttons.

    Adding a :utton to a 5ontainer :utton button < new :utton '=:utton>?(%add 'button(%

    ,e!t -ields

    13 | P a g e

  • 8/9/2019 HNDApplet

    14/21

    A text field is a text object that allows the editing of a single line of text. )t is similar to an edit boxin 2indows.

    ext9ields.java

    //Import the Abstract Win ow Tool=it "AWT$ classesimport java.awt.*;import java.applet.*;

    public class exText iel exten s Applet!

    //create two label L text fiel pairs5abel lbl% mbol 0 new 5abel ":?nter our stoc= s mbol:$;

    //create a text fiel with wi th 0 2Text iel txt%toc=% mbol 0 new Text iel "2$;

    5abel lbl ualit 0 new 5abel ":Cow 8an %hares 3o ou HwnM:$;

    //create a text fiel with wi th 0 + an initial value of :+:

    Text iel txtFumberHf%hares 0 new Text iel ":+:) +$;

    public voi init"$!

    //choose the low5a out la out mana#erset5a out "new low5a out"$$;

    //A our componentsa "lbl% mbol$;

    a "txt%toc=% mbol$;a " uantit $;a "txtFumberHf%hares$;

    ,,

    Chec o!es and adio "ttons

    A chec! box is a graphical component that can be on one of two states& on or off 'or chec!ed or unchec!ed if you li!e(. 5hec! boxes are great components to use when you want your user toenable or disable something.

    5hec!boxes. java

    14 | P a g e

  • 8/9/2019 HNDApplet

    15/21

    //Import the Abstract Win ow Tool=it "AWT$ classesimport java.awt.*;import java.applet.*;

    public class >hec= oxes exten s Applet!//create a label an a chec=box #roup

    5abel lblTitle 0 new label ":Cere are some chec= boxes :$;>hec=box cbxCistoricalTrac=in# 0 new >hec=box ":Cistorical

    Trac=in# :$;>hec=box cbxA To 4rofile 0new >hec=box ":A to4ortfolio:)true$;

    //>hec=box #roups L ra io buttons>hec=boxGroup cb#Hr erHption 0 new >hec=box Group"$;>hec=box cbx u 0 new >hec=box ": u :) cb#Hr erHption)true$;>hec=box cbx%ell 0new >hec=box ":%ell:) cb#Hr erHption) false$;>hec=box cbxCol 0new >hec=box ":Col :)cb#Hr erHption) false$;

    public voi init"$!

    //choose the low5a out la out mana#erset5a out "new low5a out"$$;

    //a our labela "lblTitle$;

    //A our >hec=boxesa "cbxCistoricaltrac=in#$;a "cbxA To4ort olio$;

    //a our ra io buttonsa "cbx u $;a "cbx%ell$;a "cbxCol $;

    ,

    ,

    Choice List

    *ou should be familiar with choice lists in their more common name" combo boxes% a choice listis a drop down lists is a drop$down list" or menu" of choices. *ou would use it when you want theuser to select one and only one selection from a list of items and you are trying to conservespace.

    5hoice ists.java

    //Import the Abstract Win ow Tool=it "AWT$ classesimport java.awt.*;import java.applet.*;

    public class exText iel exten s Applet//import the Abstract Win ow Tool=it "AWT$ classesimport java.awt.*;import java.applet.*;

    15 | P a g e

  • 8/9/2019 HNDApplet

    16/21

    public class ex>hoice5ist exten s Applet!

    //create a label an a choice control5abel lblTitle 0 new 5abel ":Cere is a choice list :$;>hoice chlist%toc=% mbol 0 new >hoice"$;

    public voi init "$!

    //choice the low5a out la out mana#erset5a out "new low5a out"$$;

    // uil our choice listchlist%toc=% mbol.a Item ": E?:$;chlist%toc=% mbol.a Item ":8>4:$;chlist%toc=% mbol.a Item ":%A8%:$;chlist%toc=% mbol.a Item ":GHH3:$;

    //A our controlsa "lblTitle$;a "chlist%toc=% mbol$;

    ,,

    4.1/ ent Handling

    4.1/.1. hat Are ents6

    he dictionary defines the term event as an occurrence" incident or significant experience. And"

    in practical terms" that is what a even in +ava is& the occurrence of a user interaction in the user interface. )n technical terms" events are notifications made by the +ava 6untime 0ngine" sent tothe user interface whenever any element of the user interface is manipulated.

    4.1/.2. Handling "tton7s clic e ent

    o handle button3s clic! event" you must create a class that implements Action istener" and thendefine the actioPerformed method in that class. he event sent to the actionPerformed methodis the Action0vent event.

    o add an action event listener to a component" you can all the addAction istener method

    defined as follows&

    import java.awt.event.*;import javax.swin#.*;import java.applet.*;

    public class utton?x exten s Applet implements Action5istener!

    JText iel t 0 new JText iel " 2$;

    1# | P a g e

  • 8/9/2019 HNDApplet

    17/21

    J utton b 0 new J utton":>lic= me:$;J utton b10 new J utton":>lear :$;

    public voi init"$!

    //A our controls

    a "t $;b .a Action5istener"this$;a "b $;b1.a Action5istener"this$;a "b1$;

    ,

    public voi action4erforme "Action?vent e$!if"e.#et%ource"$00b $!

    t .setText":Welcome:$;,if"e.#et%ource"$00b1$!

    t .setText"::$;,

    ,,

    1( | P a g e

  • 8/9/2019 HNDApplet

    18/21

    Output will be as follows

    Com0o o! !ample

    import java.awt.event.*;import javax.swin#.*;import java.applet.*;

    public class >ombo?x exten s Applet implements Item5istener!

    JText iel t 0 new JText iel " 2$;J>ombo ox cmb0 new J>ombo ox"$;

    public voi init"$!

    //a ata to the combo boxcmb.a Item":>olombo:$;cmb.a Item":Few 3elhi:$;cmb.a Item":8oscow:$;

    //A our controlsa "cmb$;cmb.a Item5istener"this$;a "t $;

    ,public voi item%tate>han#e "Item?vent e$!

    if""%trin#$cmb.#et%electe Item"$00:>olombo:$!t .setText":%ri 5an=a:$;,

    if""%trin#$cmb.#et%electe Item"$00:Few 3elhi:$!t .setText":In ia:$;

    ,

    if""%trin#$cmb.#et%electe Item"$00:8oscow:$!t .setText":7ussia:$;,

    ,,

    1) | P a g e

  • 8/9/2019 HNDApplet

    19/21

    Output as follows

    4.11 8"estion 9 er iew

    >(0xplain the Applet life cycle

    @(:riefly explain the A2 tool !it

    (;ame the Abstract methods in Action istener interface

    B(;ame the Abstract methods in )tem istener interface

    C(;ame ayout 5lasses

    (Design the following )nterface

    E.( Design the following )nterface

    1 | P a g e

  • 8/9/2019 HNDApplet

    20/21

    2/ | P a g e

  • 8/9/2019 HNDApplet

    21/21

    F(.Modify the above program to display the relevant course fees next to the 5ombobox

    -;D < @>G GGG:5S < @CGGGG:) < >@CGGG

    H.( Modify the same program by adding two buttons as =5ancel? and =O7?

    5ancel I o clear the all fieldsO7 $ ';othing(

    21 | P a g e