user-defined parameters can be passed to an applet using the tags
TRANSCRIPT
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>
<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
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
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
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
/* <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
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
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
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
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