33
APPLET

APPLET. 2 Introduction to Java Applet Programs Applications are stand alone programs ◦ executed with Java interpreter Applet is a small program ◦ can

Embed Size (px)

Citation preview

Page 1: APPLET. 2 Introduction to Java Applet Programs Applications are stand alone programs ◦ executed with Java interpreter Applet is a small program ◦ can

APPLET

Page 2: APPLET. 2 Introduction to Java Applet Programs Applications are stand alone programs ◦ executed with Java interpreter Applet is a small program ◦ can

2

Introduction to Java Applet Programs

Applications are stand alone programs◦executed with Java interpreter

Applet is a small program◦can be placed on a web page◦will be executed by the web browser◦give web pages “dynamic content”

Page 3: APPLET. 2 Introduction to Java Applet Programs Applications are stand alone programs ◦ executed with Java interpreter Applet is a small program ◦ can

Advantages of Applet

There are many advantages of applet. They are as follows:It works at client side so less response

time.SecuredIt can be executed by browsers running

under many plateforms, including Linux, Windows, etc

Page 4: APPLET. 2 Introduction to Java Applet Programs Applications are stand alone programs ◦ executed with Java interpreter Applet is a small program ◦ can

4

Java AppletsJava applets are usually graphical

◦Draw graphics in a defined screen area◦Enable user interaction with GUI elements

Applet class is one of the Abstract Windowing Toolkit (AWT) components

Page 5: APPLET. 2 Introduction to Java Applet Programs Applications are stand alone programs ◦ executed with Java interpreter Applet is a small program ◦ can

6

Java AppletsApplets are Java programs that can be

embedded in HTML documents◦ To run an applet you must create a .html file which

references the applet◦ Ready to Program also will run an applet

When browser loads Web page containing applet◦ Applet downloads into Web browser◦ begins execution

Can be tested using appletviewer program

Page 6: APPLET. 2 Introduction to Java Applet Programs Applications are stand alone programs ◦ executed with Java interpreter Applet is a small program ◦ can

7

AppletsApplet

◦ Program that runs in appletviewer (test utility for applets) Web browser (IE, Communicator)

◦ Executes when HTML (Hypertext Markup Language) document containing applet is opened

◦ Applications run in command windows

Page 7: APPLET. 2 Introduction to Java Applet Programs Applications are stand alone programs ◦ executed with Java interpreter Applet is a small program ◦ can

8

Applets and Web Pages – HTML Applets embedded in a web page

◦Executed when web page loaded by browserWeb pages structured with HTML codes

◦HyperText Mark-up LanguageSyntax<command> . . .</command>

Turns format onTurns format on

Turns the format offTurns the format off

Page 8: APPLET. 2 Introduction to Java Applet Programs Applications are stand alone programs ◦ executed with Java interpreter Applet is a small program ◦ can

9

Applets and Web Pages – HTMLEmbedding Java applets

◦Insert applet tags<APPLET></APPLET>

Call the specific applet by its file name<APPLET CODE = "Whatever.class" WIDTH = nnn HEIGHT = mmmm><\APPLET>

Where nnn and mmm are specific pixel sizes

Page 9: APPLET. 2 Introduction to Java Applet Programs Applications are stand alone programs ◦ executed with Java interpreter Applet is a small program ◦ can

10

Applets and Web Pages – HTML

Create the web page code using a text editor

Save it with an .html suffix

Open this file with appletviewer or with a web browser that supports Java

Java Plug-in must be installed (part of J2SDK 1.4.1 from Sun)

<HTML>

<HEAD>

</HEAD>

<BODY>

<APPLET CODE = . . . >

</APPLET>

</BODY>

</HTML>

Page 10: APPLET. 2 Introduction to Java Applet Programs Applications are stand alone programs ◦ executed with Java interpreter Applet is a small program ◦ can

11

Applets and Web Pages – HTMLClient Web browser anywhere can access

this web page from its host serverEmbedded Java applet runs on client

browser (of any type platform)This means a client anywhere on any type

of platform can run a piece of software developed on any other type of platform

Platform Independence

Page 11: APPLET. 2 Introduction to Java Applet Programs Applications are stand alone programs ◦ executed with Java interpreter Applet is a small program ◦ can

The following is a simple applet named HelloWorldApplet.java:

import java.applet.*;

import java.awt.*;

public class HelloWorldApplet extends Applet {

public void paint (Graphics g) {

g.drawString ("Hello World", 25, 50);

}

}

Page 12: APPLET. 2 Introduction to Java Applet Programs Applications are stand alone programs ◦ executed with Java interpreter Applet is a small program ◦ can

These import statements bring the classes into the scope of our applet class:

java.applet.Applet.java.awt.Graphics.Without those import statements, the Java

compiler would not recognize the classes Applet and Graphics, which the applet class refers to.

Page 13: APPLET. 2 Introduction to Java Applet Programs Applications are stand alone programs ◦ executed with Java interpreter Applet is a small program ◦ can

Invoking an Applet:

An applet may be invoked by embedding directives in an HTML file and viewing the file through an applet viewer or Java-enabled browser.

The <applet> tag is the basis for embedding an applet in an HTML file.

Page 14: APPLET. 2 Introduction to Java Applet Programs Applications are stand alone programs ◦ executed with Java interpreter Applet is a small program ◦ can

Below is an example that invokes the "Hello, World" applet:

<html>

<title>The Hello, World Applet</title> <hr>

<applet code="HelloWorldApplet.class" width="320" height="120">

If your browser was Java-enabled, a "Hello, World" message would appear here.

</applet>

<hr> </html>

Page 15: APPLET. 2 Introduction to Java Applet Programs Applications are stand alone programs ◦ executed with Java interpreter Applet is a small program ◦ can

The code attribute of the <applet> tag is required. It specifies the Applet class to run.

Width and height are also required to specify the initial size of the panel in which an applet runs.

The applet directive must be closed with a </applet> tag.

Page 16: APPLET. 2 Introduction to Java Applet Programs Applications are stand alone programs ◦ executed with Java interpreter Applet is a small program ◦ can

Non-Java-enabled browsers do not process <applet> and </applet>. Therefore, anything that appears between the tags, not related to the applet, is visible in non-Java-enabled browsers.

Page 17: APPLET. 2 Introduction to Java Applet Programs Applications are stand alone programs ◦ executed with Java interpreter Applet is a small program ◦ can

Lifecycle of Java Applet

Applet is initialized.Applet is started.Applet is painted.Applet is stopped.Applet is destroyed.

Page 18: APPLET. 2 Introduction to Java Applet Programs Applications are stand alone programs ◦ executed with Java interpreter Applet is a small program ◦ can

Lifecycle methods for Applet:

The java.applet.Applet class 4 life cycle methods and java.awt.Component class provides 1 life cycle methods for an applet.

java.applet.Applet classFor creating any applet java.applet.Applet class must

be inherited. It provides 4 life cycle methods of applet.public void init(): is used to initialized the Applet. It

is invoked only once.public void start(): is invoked after the init() method

or browser is maximized. It is used to start the Applet.public void stop(): is used to stop the Applet. It is

invoked when Applet is stop or browser is minimized.public void destroy(): is used to destroy the Applet. It

is invoked only once.

Page 19: APPLET. 2 Introduction to Java Applet Programs Applications are stand alone programs ◦ executed with Java interpreter Applet is a small program ◦ can

java.awt.Component classThe Component class provides 1 life

cycle method of applet.public void paint(Graphics g): is used to

paint the Applet. It provides Graphics class object that can be used for drawing oval, rectangle, arc etc.

Page 20: APPLET. 2 Introduction to Java Applet Programs Applications are stand alone programs ◦ executed with Java interpreter Applet is a small program ◦ can

Passing parameters to an appletTo pass parameters to an applet, two

things are required- a special parameter tag in HTML file and the code in the applet to parse those parameters.

Page 21: APPLET. 2 Introduction to Java Applet Programs Applications are stand alone programs ◦ executed with Java interpreter Applet is a small program ◦ can

Import java.awt.*;

Import java.applet.*;

Public class ParamtestApplet extends Applet{

Font f=new Font(“Times Roamn”,Font.BOLD,40);

String name;

Public void init(){

name= getParameter(“name”);

If(name==null)

{

name=“friend”;

}

Name=“Have a nice day”+name;

}

Public void Paint(Graphics g){

g.setFont(f);

g.setColor(Color.darkGray);

g.drawString(name,50,50);

} }

Page 22: APPLET. 2 Introduction to Java Applet Programs Applications are stand alone programs ◦ executed with Java interpreter Applet is a small program ◦ can

ParamtestApplet.html

<APPLET CODE=“ParamtestApplet.class” CODEBASE=“/test” WIDTH=400 HEIGHT=400>

<PARAM NAME=name VALUE=“Abi”></APPLET>

Page 23: APPLET. 2 Introduction to Java Applet Programs Applications are stand alone programs ◦ executed with Java interpreter Applet is a small program ◦ can

Example of Applet by appletviewer tool:

//First.java  

import java.applet.Applet;  

import java.awt.Graphics;  

public class First extends Applet

{ public void paint(Graphics g)

{  g.drawString("welcome to applet",150,150);  

}  }  

/* 

<applet code="First.class" width="300" height="300"> 

</applet> 

*/  

Page 24: APPLET. 2 Introduction to Java Applet Programs Applications are stand alone programs ◦ executed with Java interpreter Applet is a small program ◦ can

To execute the applet by appletviewer tool, write in command prompt:

c:\>javac First.java

c:\>appletviewer First.java

Page 25: APPLET. 2 Introduction to Java Applet Programs Applications are stand alone programs ◦ executed with Java interpreter Applet is a small program ◦ can

import java.applet.Applet;  

import java.awt.*;  

  

public class GraphicsDemo extends Applet{  

  public void paint(Graphics g){  

g.setColor(Color.red);  

g.drawString("Welcome",50, 50);  

g.drawLine(20,30,20,300);  

g.drawRect(70,100,30,30);  

g.fillRect(170,100,30,30);  

g.drawOval(70,200,30,30);  

  

g.setColor(Color.pink);  

g.fillOval(170,200,30,30);  

g.drawArc(90,150,30,30,30,270);  

g.fillArc(270,150,30,30,0,180);  

 }  

}  

<html>  

<body>  

<applet code="GraphicsDemo.class" width="300" height="300">  

</applet>  

</body>  

</html>  

Page 26: APPLET. 2 Introduction to Java Applet Programs Applications are stand alone programs ◦ executed with Java interpreter Applet is a small program ◦ can

Methods used to load and display image in an applet

drawImage(Image, int x, int y, ImageObserver): is used draw the specified image at the given coordinates.Image referes to picture to be drawn.x,y are the coordinates where the image is to drawn.Image observer is usually the applet itself.

getImage(URL , String ):Loads an image from an URL.URL specifies the location from which the image is to be loaded.String is usually the name of the image file.

Page 27: APPLET. 2 Introduction to Java Applet Programs Applications are stand alone programs ◦ executed with Java interpreter Applet is a small program ◦ can

Helper functions in getImage() method

getDocumentBase(): is used to return the URL of the HTML document in which applet is embedded.

getCodeBase(): is used to return the path( URL) of the .class file.

Page 28: APPLET. 2 Introduction to Java Applet Programs Applications are stand alone programs ◦ executed with Java interpreter Applet is a small program ◦ can

import java.awt.*;  

import java.applet.*;  

    

public class DisplayImage extends Applet {  

  

  Image picture;  

  

  public void init() {  

    picture = getImage(getCodeBase(),“rabbit.gif");  

  }  

    

  public void paint(Graphics g) {  

    g.drawImage(picture, 30,30, this);  

  }  

      

  }  

Page 29: APPLET. 2 Introduction to Java Applet Programs Applications are stand alone programs ◦ executed with Java interpreter Applet is a small program ◦ can

<html>  

<body>  

<applet code="DisplayImage.class" width="300" height="300">  

</applet>  

</body>  

</html>  

Page 30: APPLET. 2 Introduction to Java Applet Programs Applications are stand alone programs ◦ executed with Java interpreter Applet is a small program ◦ can

getImage(getCodeBase(),“rabbit.gif"); 

Loads the file rabbit.gif from the directory where the .class file is located and stores it in the instance variable img.The paint() method of the applet,displays

the image at the coordinates mentioned using drawImage() method.

The parameter this(the applet itself) passed in the drawImage() function calls the image observer which monitors the drawing of the image.

Page 31: APPLET. 2 Introduction to Java Applet Programs Applications are stand alone programs ◦ executed with Java interpreter Applet is a small program ◦ can

ClipplingClipping is a technique by which the

drawing area can be restricted to a small portion of the screen.A clipping region is an area where drwaing is allowed.

Page 32: APPLET. 2 Introduction to Java Applet Programs Applications are stand alone programs ◦ executed with Java interpreter Applet is a small program ◦ can

Clipper.java

Import java.awt.*;

Import java.applet.*;

public class clipper extends Applet{

public void paint(Graphics g){

g.clipRect(10,10,150,100);

G.setFont(new Font(“TimesRoman”,Font.ITALIC,28);

g.fillOval(00,60,80);

g.drawString(“Happy New Year”,50,30)

Page 33: APPLET. 2 Introduction to Java Applet Programs Applications are stand alone programs ◦ executed with Java interpreter Applet is a small program ◦ can

Clipper.html<APPLET CODE=“Clipper.class”

WIDTH=300 HEIGHT=150></APPLET>