Parameters


Description:The sample applet on this page takes three parameters as input and produces text display using that input. Inspect source code. Note that the string passed to getParameter must match the name which the parameter is given on the html page.
Assignment: Pass five parameters to an applet which then displays this information in a meaningful context.


import java.applet.*; import java.awt.*; public class simpleP extends Applet { Font f = new Font("TimesRoman",Font.BOLD,18); String name, inst, where; Dimension s; public void init() { name=getParameter("name"); inst=getParameter("inst"); where=getParameter("where"); s=size(); inst= "Instructor: " + inst; where="Room "+ where + ", Trona High School"; //#3 } public void paint(Graphics g) { g.setFont(f); g.setColor(Color.green); g.fillRect(0,0,s.width,s.height); g.setColor(Color.black); g.drawString(name,10,50); g.drawString(inst,10,110); g.drawString(where,10,170); } } This is the html code necessary to load this applet: <applet code=simpleP.class height=200 width=300> <param name=name value="ROP Computer Science"> <param name=inst value="Mr. McGirr"> <param name=where value="33"> </applet>
Passing parameters:
1. Notice that the parameters used by this applet are passed to it from the web page. Look at the code used in the APPLET tag.
2. Notice that the name of the parameter is the same in three places. First in the web page code. Then a string is given the same name as the parameter. Finally the parameter name is referenced in the getParameter method. It is not necessary for the string receiving the value from the getParameter method to have the same name as the parameter it is receiving.
3. Notice the way that words can be added to a string in the line with the #3 comment following it. Adding the contents of strings together is called concatenation.

FYI, Collecting Parameters With JavaScript.