Sunday, December 18, 2011

Get Applet parameter

package org.best.example;

    /*
            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;
    
    /*
     * To pass a parameter to an Applet use <param> HTML tag.
     * Specify name and value attribute to pass a parameter to an applet like
     * given below.
     *
     * <param name="paramName" value="paramValue"/>
     *
     * You can pass multiple parameters for an Applet using multiple
     * <param> HTML tag.
     */
    
    /*
    <applet code="GetAppletParameterExample" 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 GetAppletParameterExample 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: