10
Passing Parameter to an Applet User-defined parameters can be passed to an applet using the <PARAM…> tags. <APPLET …..> <PARAM name=“age” value = “22”> <PARAM name=“class” value = “MCA”> </PARAM> </APPLET>

User-defined parameters can be passed to an applet using the tags

Embed Size (px)

Citation preview

Page 1: User-defined parameters can be passed to an applet using the tags

Passing Parameter to an Applet

User-defined parameters can be passed to an applet using the

<PARAM…> tags.<APPLET …..>

<PARAM name=“age” value = “22”> <PARAM name=“class” value =

“MCA”> </PARAM></APPLET>

Page 2: User-defined parameters can be passed to an applet using the tags

<html><head> <title>Hello World</title></head><body> <applet code="ParamTest.class" height=200

width=200> <param name="String" value="Welcome to

Applet Programming"> </param>

</applet></body></html>

Sample Program

Page 3: User-defined parameters can be passed to an applet using the tags

import java.awt.*;import java.applet.*;public class ParamTest extends Applet{

String str;public void init(){

str=getParameter("String");setBackground(Color.yellow);

setForeground(Color.red);}public void paint(Graphics g){

super.paint(g);g.drawString(str,20,20);

}}

Sample Program

Page 4: User-defined parameters can be passed to an applet using the tags

getDocumentBaseThe signature of the method getDocumentBase()

defined by Applet class is:

public URL getDocumentBase()

The method getDocumentBase() gets the URL of the document in which this applet is embedded. For example, suppose an applet is contained within the document:

http://java.sun.com.products/jdk/1.5/index.html

The document base is:

http://java.sun.com.products/jdk/1.5/index.html

Page 5: User-defined parameters can be passed to an applet using the tags

The signature of the getCodeBase method is◦ public URL getCodeBase()

The method gets the base URL. This is the URL of the directory which contains this applet

getCodeBase

Page 6: User-defined parameters can be passed to an applet using the tags

/* <applet code="URLDemo" height=100 width=400> </applet> */

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

Public class URLDemo extends Applet{ publicvoid paint(Graphics g){ String msg;

URL url=getCodeBase(); msg = "Code Base : "+url.toString(); g.drawString(msg,10,20);

url=getDocumentBase(); msg="Document Base : "+url.toString(); g.drawString(msg,10,40); }}

Code for An applet program to give demo of getDocumentBase() and getCodeBase() methods

Page 7: User-defined parameters can be passed to an applet using the tags

Defined in the Applet Context interface. showDocument(URL)

– Displays the document represented by the URL

showDocument(URL,where)• Displays the document represented by the

URL at the location• specified by where

showDocument method

Page 8: User-defined parameters can be passed to an applet using the tags

Applet Context is an interface that is defined in the java.applet package.

It is used by an applet to obtain information from the applet's environment through its methods.

Using methods of this interface, we can transfer control to another document.

Applet Context Interface

Page 9: User-defined parameters can be passed to an applet using the tags

Rich class of graphics methods defined in the awt package.

Can be drawn relative to a window. The origin of each window is at the top-left corner and is 0,0. Coordinates are specified in pixels.

Working with Graphics

Page 10: User-defined parameters can be passed to an applet using the tags

Methods Description

drawLine(int sX, int sY, int eX, int sY)

Draw a line from (sX, sY) to (eX , eY)

void drawRect(int top, int left, int width, int height)

Draws a rectancle

void drawOval(int top, int left, int width, int height)

Draw an oval inside a rectangle whosedimensions are given

void drawPolygon(int x[ ], int y[ ], int numPoints)

Draws a polygon whose points aredefined by the arrays of x and y. total no.of points given by numPoints

Graphic Methods