Get Applet parameter - Java Applet

Java examples for Applet:Applet Creation

Description

Get Applet parameter

 
import java.applet.Applet;
import java.awt.Graphics;
 
/*
<applet code="Main" 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 Main {
 
        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);
        }
}

Related Tutorials