320 likes | 1.69k Views
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>. Sample Program. <html > < head>
E N D
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>
Sample Program <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); } }
getDocumentBase The 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
getCodeBase • 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
Code for An applet program to give demo of getDocumentBase() and getCodeBase() methods /* <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); } }
showDocument method • 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
Applet Context Interface • 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.
Working with Graphics • Rich class of graphics methods defined in the awtpackage. • 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.