0

I am new to java and currently trying to create a code of passing parameter in java applet. Below is the code. While running the code the result is coming null. Can you friends can help me with rectification and suggestion.

Regards Sandeep

    package Example_165_Applet_Fundamentals;

//Use parameters

import java.applet.*;
import java.awt.*;

/*
 * <applet code="Passing_Parameters_To_Applets" width=300 height=80>
 * <param name="fontName" value="Courier">
 * <param name="fontSize" value="14">
 * <param name="leading" value="2">
 * <param name="accountEnabled" value="true">
 * </applet>
 */
public class Passing_Parameters_To_Applets extends Applet
{
    String fontName;
    int fontSize;
    float leading;
    boolean active;

    //Initialize the string to be displayed
    public void start()
    {
        String param;

        fontName=getParameter("fontName");
        if(fontName==null)
        {
            fontName="Not Found";
        }

        param=getParameter("fontSize");
        try
        {
            if(param!=null)  //if not found
            {
                fontSize=Integer.parseInt(param);
            }
            else
            {
                fontSize=0;
            }
        }
        catch(NumberFormatException e)
        {
            fontSize=-1;
        }

        param=getParameter("leading");
        try
        {
            if(param!=null)  //if not found
            {
                leading=Float.valueOf(param).floatValue();
            }
            else
            {
                leading=0;
            }
        }
        catch(NumberFormatException e)
        {
            leading=-1;
        }

        param=getParameter("accountEnabled");
        if(param!=null)
        {
            active=Boolean.valueOf(param).booleanValue();
        }
    }
    //Display parameters
    public void paint(Graphics g)
    {
        g.drawString("Font Name: "+fontName, 0, 10);
        g.drawString("Font Size: "+fontSize, 0, 26);
        g.drawString("Leading: "+leading, 0, 42);
        g.drawString("Account Active: "+active, 0,58);
    }

}
  • [This](http://docs.oracle.com/javase/tutorial/deployment/applet/param.html) may be of use to you. – Azar Nov 03 '14 at 12:15
  • The applet is apparently not being launched using the HTML defined in the Java source code. What is the content of the HTML that launches it? – Andrew Thompson Nov 03 '14 at 22:02
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Nov 03 '14 at 22:02

1 Answers1

0

Instead of using getParameter(), you should use this.getParameter(). I think that would work!

dryairship
  • 6,022
  • 4
  • 28
  • 54