Applet Programming Chapter5 Marks20 Introduction There are two

  • Slides: 31
Download presentation
Applet Programming Chapter-5 Marks-20

Applet Programming Chapter-5 Marks-20

Introduction • There are two kinds of Java programs- – Applications (stand-alone programs) –

Introduction • There are two kinds of Java programs- – Applications (stand-alone programs) – Applets. • An Applet is a small Internet-based program that has the Graphical User Interface (GUI), written in the Java programming language.

Introduction • A Java applet is a small application written in Java and delivered

Introduction • A Java applet is a small application written in Java and delivered to users in the form of bytecode. • A applet is typically embedded in a Web page and can be run from a browser. • Applets can be transported over the Internet from one computer to another.

How applet differ from application • Applets are the small programs, applications are larger

How applet differ from application • Applets are the small programs, applications are larger programs. • Applets don't have the main method while in an application execution starts with the main method. • Applications run independently while Applets cannot. They run from inside the web page. • Applets cannot read from or write to the files in the local computer.

Introduction • An applet can perform – arithmetic operation, – display graphics, – play

Introduction • An applet can perform – arithmetic operation, – display graphics, – play sounds, – accept user input, – create animation and – play interactive games.

Local and Remote Applets • We can embed applets into web pages in two

Local and Remote Applets • We can embed applets into web pages in two ways 1. Create our own applets ( Local ) 2. Download an applet from remote computer system (Remote Applet )

Local Applet • An applet developed locally and stored in local system is known

Local Applet • An applet developed locally and stored in local system is known as local applet. • When a web page trying to find a local applet, it does not need to use the Internet. • It simply searches the directories in the local system and locates and loads the specified applet. local applet

Remote Applet • A remote applet is that which is developed by someone else

Remote Applet • A remote applet is that which is developed by someone else and stored on the remote computer connected to internet. • Users can download the remote applet onto there system and run it. Internet Local Computer Remote Computer

Steps involved in developing applets 1. 2. 3. 4. 5. Building an applet code

Steps involved in developing applets 1. 2. 3. 4. 5. Building an applet code (. java file ) Creating an executable applet (. class file ) Designing a Web Page using HTML tags. Preparing <APPLET> tag. Incorporating <APPLET> tag into the Web Page. 6. Creating HTML file. 7. Testing the Applet Code.

Building Applet Code • It is essential that applet code uses services of two

Building Applet Code • It is essential that applet code uses services of two classes –Applet class and –Graphics class.

Chain of classes inherited by Applet class java. lang. Object | +----java. awt. Component

Chain of classes inherited by Applet class java. lang. Object | +----java. awt. Component | +----java. awt. Container | +----java. awt. Panel | +----java. applet. Applet

The Applet Class • The java. applet package is the smallest package in Java

The Applet Class • The java. applet package is the smallest package in Java API. • The Applet class is the only class in the package. • The Applet class provides life and behaviour to applet through its methods. • The Applet class has many methods that are used to display images, play audio files etc but it has no main() method.

Methods of Applet Class • init() : – used for whatever initializations are needed

Methods of Applet Class • init() : – used for whatever initializations are needed for applet. – Applets can have a default constructor, but it is better to perform all initializations in the init method instead of the default constructor. • start() : – This method is automatically called after Java calls the init method.

 • stop() : – This method is automatically called when the user moves

• stop() : – This method is automatically called when the user moves off the page where the applet sits. • destroy(): – Java calls this method when the browser shuts down.

paint() Method of Applet Class • Paint method actually displays the result of applet

paint() Method of Applet Class • Paint method actually displays the result of applet code on the screen. • The output may be text, graphics, or sound. • Syntaxpublic void paint(Graphics g) • The paint method requires graphics object as an argument. • Hence we require to import Graphics class from java. awt package.

Applet Life Cycle Begin (Load applet) Born start() Initialization stop() Idle Running start() Display

Applet Life Cycle Begin (Load applet) Born start() Initialization stop() Idle Running start() Display paint() Destroyed Stopped destroy() Dead Exit browser Applet state transition diagram End

Born or Initialization State • An applet begins its life when the web browser

Born or Initialization State • An applet begins its life when the web browser loads its classes and calls its init() method. • This is called exactly once in Applets lifecycle. • init() method provides initialization code such as initialization of variables, loading images or fonts. Eg. public void init() { //initialization

Running State • Once the initialization is complete, the web browser will call the

Running State • Once the initialization is complete, the web browser will call the start() method in the applet. • This method must called atleast once in the Applets lifecycle. • The start() method can also be called if the Applet is in “Idle” state. Eg. public void start() { //Code }

Stopped State • The web browser will call the Applets stop() method, if the

Stopped State • The web browser will call the Applets stop() method, if the user moved to another web page while the applet was executing. • The stop() method is called atleast once in Applets Lifecycle. Eg. public void stop() { //Code }

Dead State • Finally, if the user decides to quit the web browser, the

Dead State • Finally, if the user decides to quit the web browser, the web browser will free up system resources by killing the applet before it closes. • To do so, it will call the applets destroy() method. Eg. public void destroy() { // Code }

Display State • Applet moves to the display state whenever it has to perform

Display State • Applet moves to the display state whenever it has to perform the output operations on the screen. • This happens immediately after the applet enters into the running state. • The paint() method is called to accomplish this task. Eg. public void paint(Graphics g) { //Display Statements

General format of Applet Code import java. awt. *; import java. applet. *; …………

General format of Applet Code import java. awt. *; import java. applet. *; ………… public class applet_class_name extends Applet { …………… public void paint(Graphics g) { …………. //applet operation code } }

Example. import java. awt. *; import java. applet. *; public class Simple. Applet extends

Example. import java. awt. *; import java. applet. *; public class Simple. Applet extends Applet { public void paint(Graphics g) { g. draw. String("My First Applet", 40); } }

Designing a Web Page • To execute an applet in a web browser, write

Designing a Web Page • To execute an applet in a web browser, write a short HTML text file that contains the appropriate APPLET tag. • For above example it is <html> <body> <applet code="Simple. Applet. class" width=200 height=100> </applet> </body> </html>

The Applet tag <applet [codebase=codebase. URL] code=”Applet file” [ALT=”alternative text] [name=Applet. Instance. Name] Width=pixels

The Applet tag <applet [codebase=codebase. URL] code=”Applet file” [ALT=”alternative text] [name=Applet. Instance. Name] Width=pixels height= pixels [align= alignment] > [<param name=”Attributename” value =”Attribute value”] </applet>

Passing Parameter to Applet • We can supply user-defined parameters to an applet using

Passing Parameter to Applet • We can supply user-defined parameters to an applet using <param. . . > tag. • Each <param. . > tag has a name , and a value attribute. • For e. g. the color of the text can be changed to red by an applet using a <param. . . > tag as follows <applet ……> <param name=“color” value=“RED”> </applet>

Passing Parameters to Applet • To handle parameters, we need to do two things.

Passing Parameters to Applet • To handle parameters, we need to do two things. 1) Include <param. . . > tags in HTML document. 2) Provide code in the applet to pass these parameters. • We can define the init() method in the applet to get parameters defined in the <param> tags. • • This is done using the get. Parameter() method, which takes one string argument representing the name of the parameter and returns a string containing the value of that parameter.

Example : Parameter Passing import java. awt. *; import java. applet. *; public class

Example : Parameter Passing import java. awt. *; import java. applet. *; public class Hello. Java extends Applet { String str; public void init() { str=get. Parameter(“String”); if(str==null) str=“Java”; str=“Hello ” +str; } Public void paint(Graphics g) { g. draw. String(“str”, 10)

<html> <body> <applet code=“Hello. Java. class” width=400 height=400 > <param name= “String” value= “Applet!”

<html> <body> <applet code=“Hello. Java. class” width=400 height=400 > <param name= “String” value= “Applet!” > </applet> </body> </html>

Displaying numerical values import java. awt. *; import java. applet. *; public class Hellojava

Displaying numerical values import java. awt. *; import java. applet. *; public class Hellojava extends Applet{ public void paint(Graphics g) { int value 1 =10; int value 2 = 20; int sum =value 1+value 2; String s= "Sum: "+String. value. Of(sum); g. draw. String(s, 100); }

Using Control Structure import java. awt. *; import java. applet. *; public class Hellojava

Using Control Structure import java. awt. *; import java. applet. *; public class Hellojava extends Applet { public void paint(Graphics g) { int y=50; for(int i=1; i<=10; i++) { g. draw. String(i+” ”, 50, y); y=y+10; } } } /*<applet code=“Hellojava. class” width=300 height=100> </applet> */