Home E-Books Special Effects and Game Development in Java Applet parameters 

Special Effects and Game Development in Java(TM) -Applet parameters 

by Anibal Wainstein

4.1 Applet parameters 

The Java language would never have had such a big breakthrough on the Internet if it had not been for the possibility to configure Java applets. Without configurable Java applets, the Java technology would have stayed at the hands of the developers and never got to the web designers. The applet parameters is what makes an applet recyclable and available for everybody. It is a glorious feeling to randomly surf into a homepage that is using the applet that you have developed and reconfigurated. With parameters you can change almost anything in an applet such as the images used, colors, texts, font styles and more. Parameters also simplify your work as a programmer due to the simplicity to change parameter values and not having to change variables directly in the source code.

4.1.1 Working with getParameter()

It is very simple to read a parameter. Look at the following HTML code:

<APPLET CODE="myapplet.class" WIDTH=200 HEIGHT=100>
<PARAM name="myparameter" value="Long live Java!">
</APPLET>

In HTML, PARAM is used to specify a parameter, where "name" stands for the name of the parameter and "value" for the content. The method getParameter() always return a String and can, for instance, fetch "myparameter" like this:

String oneparameter=getParameter("myparameter");

Now "oneparameter" will contain the phase "Long live Java!". You may have how many parameters as you like, theoretically. However note that each parameter must have an unique name label. To simplify your work and to make your source code easy to read you should always read parameters in the init() method. The alternative is to put these lines in the run() method which will make your code difficult to read.

4.1.2 How to read an integer as a parameter (getIntegerParameter())

As we saw in the last section, getParameter() always return a string. If you want to read an integer instead, then you can use the following method:

public int getIntegerParameter(String name, int base)
{
   String value=getParameter(name);
   int intvalue;
   try {intvalue=Integer.parseInt(value,base);}
   catch (NumberFormatException e) {return 0;}
   return intvalue;
}

The method above is always good to have in an configurable applet, so you should always have the habit of pasting these lines into your source code, if you are going to make such an applet. The Integer class in Java is used to handle integers, but please note carefully now: "Integer" is NOT the same as "int". Integer is a class with methods while int is just a data type. A useful method that Integer has, is the static method parseInt() that converts a String to an integer ("int"). The other argument in parseInt() is the number base that may be decimal or hexadecimal (number base 10 or 16 respectively). The decimal number base is the normal number base system you use on your everyday life. If you are a web designer then you should also know the hexadecimal number system when you specify colors (that is if you write the HTML code yourself without a graphical HTML editor). You will later see the use of this when we specify colors for the textscroll applet.

 


Next Page >>