• No results found

Java Chapter-14

N/A
N/A
Protected

Academic year: 2020

Share "Java Chapter-14"

Copied!
7
0
0

Loading.... (view fulltext now)

Full text

(1)

1

BBBB....Naresh, Assistant Professor, BVRICE, Vishnupur::BhimavaramNaresh, Assistant Professor, BVRICE, Vishnupur::BhimavaramNaresh, Assistant Professor, BVRICE, Vishnupur::Bhimavaram Naresh, Assistant Professor, BVRICE, Vishnupur::Bhimavaram

Chapter-14

Applet Programming

In JAVA we write two types of programs or applications. They are standalone applications and distributed applications.

A Standalone Application is one which runs in the context of local disk and whose result is not

sharable. Every standalone application runs from command prompt and it contains main method along with System.out.println statements.

A Distributed Application is one which runs in the context of browser or World Wide Web

and it can be accessed across the globe. Any technology which runs in the browser will have ‘n’ number of life cycle methods and it does not contain main methods and System.out.println statements.

Definition of an Applet

In JAVA, SUN micro initially has developed a concept called applets which runs in the context of browser. “An applet is a JAVA program which runs in the context of browser or World Wide Web”.

In order to deal with applets we must import a package called java.applet.*. This package contains only one class Applet whose fully qualified name is java.applet.Applet.

Since applets are running in the browser, the class Applet contains the life cycle methods. Life cycle methods are also called as loop back methods.

Life cycle of an Applet or Life cycle methods of an Applet

(2)

2

BBBB....Naresh, Assistant Professor, BVRICE, Vishnupur::BhimavaramNaresh, Assistant Professor, BVRICE, Vishnupur::BhimavaramNaresh, Assistant Professor, BVRICE, Vishnupur::Bhimavaram Naresh, Assistant Professor, BVRICE, Vishnupur::Bhimavaram

1. Initialization or Born state i.e., public void init ()

This is the method which is called by the browser only one time after loading the applet. In this method we write some block of statements which will perform one time operations, such as, obtaining the resources like opening the files, obtaining the database connection, initializing the parameters, etc.

2. Running State i.e., public void start ()

After calling the init method, the next method which is from second request to sub-sequent requests the start method only will be called i.e., short method will be called each and every time. In this method we write the block of statement which provides business logic.

3. Idle or Stopped State i.e., public void stop ()

This is the method which is called by the browser when we minimize the window. In this method we write the block of statements which will temporarily releases the resources which are obtained in init method.

4. Dead state i.e., public void destroy ()

This is the method which will be called by the browser when we close the window button or when we terminate the applet application. In this method we write same block of statements which will releases the resources permanently which are obtained in init method.

Display state i.e., public void paint()

Another method which is not a life cycle method is public void paint (). This is the method which will be called by the browser after completion of start method. This method is used for displaying the data on to the browser. Paint method is internally calling the method called drawstring() whose prototype is given below.

java.awt.Graphics

(Graphics => public void drawString (String, int row_position, int column_position))

An object of Graphics class will be created automatically after loading the applet into the browser.

STEPS for developing APPLET Program

1. Import java.applet.Applet package.

2. Choose the user defined class that must extends java.applet.Applet class and ensure the modifier of the class must be public.

(3)

3

BBBB....Naresh, Assistant Professor, BVRICE, Vishnupur::BhimavaramNaresh, Assistant Professor, BVRICE, Vishnupur::BhimavaramNaresh, Assistant Professor, BVRICE, Vishnupur::Bhimavaram Naresh, Assistant Professor, BVRICE, Vishnupur::Bhimavaram

4. Save the program and compile.

5. Run the applet: To run the applet we have two ways. They are using HTML program and using applet viewer tool.

Using HTML program

In order to run the applet through HTML program we must use the following tag.

Syntax:

<applet code =”.class file of the applet” height = height value width = width value> </applet>

For example:

File name: MyApp.html

<HTML>

<HEAD>

<TITLE> My applet example </TITLE>

</HEAD>

<BODY>

<APPLET code="MyApp.class" height=100 width=150> </APPLET>

</BODY>

</HTML>

Using appletviewer:

appletviewer is a tool supplied by SUN micro system to run the applet programs from the command prompt (in the case of browser is not supporting).

Syntax: appletviewer filename.java

For example:

appletviewer MyApp.java

When we use appletviewer to run the above applet, MyApp.java program must contain the following tag within the multi line comment.

(4)

4

BBBB....Naresh, Assistant Professor, BVRICE, Vishnupur::BhimavaramNaresh, Assistant Professor, BVRICE, Vishnupur::BhimavaramNaresh, Assistant Professor, BVRICE, Vishnupur::Bhimavaram Naresh, Assistant Professor, BVRICE, Vishnupur::Bhimavaram

Write a JAVA program which illustrates the life cycle methods of applet

import java.applet.*;

import java.awt.*;

/*<applet code="MyApp1" height=300 width=300> </applet>*/

public class MyApp1 extends Applet

{

String s=" ";

public void init ()

{

setBackground (Color.green);

setForeground (Color.red);

s=s+" INIT ";

}

public void start ()

{

s=s+" START ";

}

public void stop ()

{

s=s+" STOP ";

}

public void destroy ()

{

s=s+" DESTROY ";

}

public void paint (Graphics g)

{

Font f=new Font ("arial", Font.BOLD, 40);

(5)

5

BBBB....Naresh, Assistant Professor, BVRICE, Vishnupur::BhimavaramNaresh, Assistant Professor, BVRICE, Vishnupur::BhimavaramNaresh, Assistant Professor, BVRICE, Vishnupur::Bhimavaram Naresh, Assistant Professor, BVRICE, Vishnupur::Bhimavaram

g.drawString (s,100,100);

}

}

Output:

HTML TAGS:

1. <HTML> </HTML> : Specifies the beginning and end of the file.

2. <HEAD> </HEAD> : This tag includes the details about the web page.

3. <TITLE> </TITLE> : displays title.

4. <BODY> </BODY> : This contains the main text of the web page. This is the place where

the applet tag is declared.

5. <H1> </H1> : Header tags: These are used to display the headings.

6. <H6> </H6> : Creates the smallest tag.

7. <CENTER> </CENTER> : places the text contained at the centre of the page.

8. <PARAM>: supplies user-defined parameter using <PARAM> tags. The PARAM tag is

allowed only in the <Applet> tag.

9. <B> </B> : Text between these tags will be displayed in bold type.

10.<BR> : Line break.

11.<p> : Para tag: this tag moves us to the next line and starts paragraph text.

12.<img> : This tag declares attributes of an image to be displayed.

13.<A…..></A> : Used to add Hyperlinks

(6)

6

BBBB....Naresh, Assistant Professor, BVRICE, Vishnupur::BhimavaramNaresh, Assistant Professor, BVRICE, Vishnupur::BhimavaramNaresh, Assistant Professor, BVRICE, Vishnupur::Bhimavaram Naresh, Assistant Professor, BVRICE, Vishnupur::Bhimavaram

How the Applets are differ from Applications ( Applet vs Applications )

All Java programs can be classified as Applications and Applets. The striking differences are

Applications contain main() method where as applets do not.

One more is, applications can be executed at DOS prompt and applets in a browser. We can say,

an applet is an Internet application.

Some more differences are given below….

Why the Applet classes must be public

Consider the following statements…..

import java.applet.*;

public class MyApplet extends Applet

{

}

The first line, tells to the Java compiler that this applet will be using some or all of the classes

(7)

7

BBBB....Naresh, Assistant Professor, BVRICE, Vishnupur::BhimavaramNaresh, Assistant Professor, BVRICE, Vishnupur::BhimavaramNaresh, Assistant Professor, BVRICE, Vishnupur::Bhimavaram Naresh, Assistant Professor, BVRICE, Vishnupur::Bhimavaram

The second line of code declares a new class called MyApplet. This new class is declared as

public so that the class can be accessed when the applet is run in a Web browser or in the Appletviewer application.

If we fail to declare the applet class as public, the code will compile fine, but the applet will

refuse to run. In other words, all applet classes must be public.

Important Questions

Define an Applet

How an Applet is differ from an Application. ( 2M or 4M)

Why the Applet classes must be public.

Explain the life cycle methods of an Applet or Explain the life cycle of an Applet in java.

Write a java Applet program with illustration.

Explain any ten HTML commands.

References

Related documents

34 Stephanie Jordan Bennett, Paternalistic Manipulation Through Pictorial Warnings: The First Amendment, Commercial Speech, and the Family Smoking Prevention and Tobacco

Stejným způsobem jako při požadavcích zaskladnění probíhá komunikace mezi ERP a WMS v případě požadavků vyskladnění, kdy se odesílají požadavky z ERP,

(Home Science) Third Semester (For SOS &amp; College Students)

Ozkan (2007) in a research titled &#34;corporate governance and CEO compensation in UK companies&#34; studied the performance of companies and effect of

The methodology introduced and the new DSS, GMCR+, were applied to a wide range of real world conflicts including a complex water conflict along the Euphrates River in the Middle

The difference between the adapted EU-method and our first new method is the use of the individual columns with the estimated bilateral IO exports from (8)-(9). The very low value

The calibration of the model is conducted using data on the 46 industries that match the consumption items as in table 1 in the appendix. The 46 industries are divided in 23 low