Passing Parameters to Applets in java

       Parameters passing is an very important in any context and is same in the case of java applets also. The tag is used to pass the parameters to the applet tag. The getParameter() method is used to get the parameters from the  Applet. The following java program shows how to pass parameters to java applets.









/*
            Get Applet parameter Example
            This java example shows how get the parameter passed to an applet using
            getParameter() method of Java Applet class.
    */
    
    import java.applet.Applet;
    import java.awt.Graphics;
    
    /*
    

     * <param name="paramName" value="paramValue"/>
     *
     * You can pass multiple parameters for an Applet using multiple
     * <param> HTML tag.
     */
    
    /*
    <applet code="GetAppletParameterExample1" width=200 height=200>
            <param name="msg" value="This is a parameter example program"/>
            <param name="xPosition" value="50"/>
            <param name="yPosition" value="50"/>
    </applet>
    */
    
    public class GetAppletParameterExample1 extends Applet{
    
            public void paint(Graphics g){
                  
                    int x = 0;
                    int y = 0;
                    String msg = "";
                  
                    try{
                            x = Integer.parseInt(getParameter("xPosition"));
                    }
                    catch(NumberFormatException ne){
                            msg = msg + "Invalid x Value";
                    }
                  
                    try{
                            y = Integer.parseInt(getParameter("yPosition"));
                    }
                    catch(NumberFormatException ne){
                            msg = msg + "Invalid y Value";
                    }
                  
                    msg = getParameter("msg");
                  
                    g.drawString(msg, x, y);
            }
    }

No comments: