As you can see, it is fairly simple to allow the user of your applet to have certain choices when using your applet and it provides more flexibility. Give the code a try and see if you can modify some of it and put in your own parameters. Try adding some extra colors to be processed or allowing the user to change the font or style of the text. You will find that there are many variations and techniques.
|
|
|
Creating Applets That Read Parameters From HTML
Java Applets
by Donald King
I remember when I used my first applet in a Web page. It was a scrolling text applet, as
anyone who has ever been on the Internet has probably seen, and the most exciting part about it to me was the parameter section of the HTML code. I thought it was the coolest thing in the world that not only could I decide what the scrolling text was going to say, but I also got to decide the size, speed, font, and color of the text as well.
October 13, 1997
This made using the applet exciting and seemed to give me part of the programming responsibility, even though I hadn't programmed it at all. Besides being fun, allowing for setting of parameters provides for better use of your applet by the user as they can have the applet fit their particular needs. The applet example below, "paramText", is nothing complex at all. The only thing it does is make a string of text appear to be floating upward. The purpose of the applet is to demonstrate allowing the user of your applet to make choices, using parameter tags in HTML code, that can be read by your applet.
Parameters in HTML code are easy to make. You will need to let the user know what the names
and possible values are for each parameter to use them properly. An example of a parameter
might look like this:
<param name = "text" value = "This is sample text for an applet!">
In this example, the parameter (param) is named "text". When the Java applet looks for that
parameter, it will be referenced by that name. The value that is returned is the value portion. In
this case it is "This is sample text for an applet!" The actual method that retrieves values from
parameters is the getParameter() method. getParameter is a method of the class
java.applet.Applet that takes a string as its argument:
String get_text;
get_text = getParameter("text");
In the above case, get_text was declared as a String object and then assigned the value
getParameter() returned from the HTML code. So in this case:
get_text = "This is sample text for an applet!"
Using this method makes for more flexible use of your applet by anyone wanting to use it. In the
example code below, I have broken up the code into several sections to make it easier to
understand how the getParameter() method is used.
Section 1
public class paramText extends Applet implements Runnable
{
//
//Declaration of variables including Strings, Integers,
Thread, Color and Font
//
String text_String, get_text_String, get_delay,
get_top_Pos, get_bot_Pos, get_Color,
set_Color, get_Size;
int top_Pos;
int bot_Pos;
int font_Size;
int delay;
int distance;
Thread moveText;
Color back_Color, fore_Color;
Font f;
//
//End declarations
//
In this section we simply import the Java packages needed for our program. Next comes the
declaration of the variables that are needed to make our program work. I have demonstrated here
that you can write all variables of the same type on one line or you can give each one its own
type identifier.
Section 2
In section two we will read our parameters from the HTML code. Each parameter that is read
has a default value associated with it in case there is no value for that parameter provided. The
code checks for a null value and if so, assigns a value that the Java program can use. Notice that
with the numerical values we have to use the Integer.parseInt() method to extract the value from
the string since that is how the parameter reads it in from the HTML code. With the parameters
that are getting a color, we use the parseColor() method in section four. I have only accounted
for a few different colors and a default value in the parseColor() method. If you wish to add
more color checks you can. I will explain that further in section four. The main focus here is
that almost any aspect of the applet can be a flexible parameter. As long as you have a way to
process the value from a string into a form that the code in your Java program can use, virtually
anything can be user's choice. Notice also that the code is checking for beginning and ending
location of the text as it floats from the bottom to the top. Even the speed of the animation is a
user configured parameter. The higher the number for this parameter, the slower the text will
rise.
//
//The applet's start method which will start the Thread
//
public void start()
{ get_text_String = getParameter("text");
if (get_text_String == null)
text_String = "This is the default text!";
else
text_String = get_text_String;
get_delay = getParameter("delay");
if (get_delay == null)
delay = 100;
else
delay = Integer.parseInt(get_delay);
get_top_Pos = getParameter("top");
if (get_top_Pos == null)
top_Pos = 15;
else
top_Pos = Integer.parseInt(get_top_Pos);
get_bot_Pos = getParameter("bottom");
if (get_bot_Pos == null)
bot_Pos = 170;
else
bot_Pos = Integer.parseInt(get_bot_Pos);
get_Color = getParameter("backcolor");
if (get_Color == null)
back_Color = Color.white;
else
back_Color = parseColor(get_Color);
setBackground(back_Color);
set_Color = getParameter("forecolor");
if (set_Color == null)
fore_Color = Color.black;
else
fore_Color = parseColor(set_Color);
setForeground(fore_Color);
get_Size = getParameter("fontsize");
if (get_Size == null)
font_Size = 12;
else
font_Size = Integer.parseInt(get_Size);
f = new Font("helvetica", Font.PLAIN, font_Size);
if (moveText == null)
{ moveText = new Thread(this);
moveText.start();
}
}
The thread is started here at the end of the start() method. The next section, section three, deals
mostly with some housekeeping chores that make the thread run in the applet.
Section 3
This section provides the stop(), run(), and paint() methods that are the motor that drives the
applet. Since our primary focus is on the parameter section of the code, I will not go in depth
here. This part is pretty self-explanatory. Primarily these methods working together paint the
value of string_text at the designated location, allow the thread to sleep, recalculate coordinates
(move) and then repaints it in a new location. Working together they produce a moving text
effect much like an animation.
public void stop()
{
if (moveText != null)
moveText.stop();
moveText = null;
}
public void run()
{
while (moveText != null)
{ distance = (distance + 1) % (bot_Pos - top_Pos);
repaint();
try { moveText.sleep(delay);
}
catch (InterruptedException ex)
{
}
}
}
public void paint(Graphics g)
{
super.paint(g);
g.setFont(f);
g.drawString(text_String, 15, bot_Pos - distance);
}
In the next section I will explain the parseColor() method since it is directly called from the code
in the start() method when the getParameter method is reading in color attributes from the HTML
code. Basically, the code is passed a string value and the string is evaluated. If the string
matches a particular value then a Color object will be assigned a Color.color constructor via the
corresponding code. Once again, I have only allotted for a few colors and there are many more
to choose from. If the color is spelled wrong in the HTML code then it will default to a specified
color value. The same holds true for a null value.
Section 4
public Color parseColor(String get_Color)
{
Color col;
if(get_Color.equals("black"))
col = Color.black;
else
if(get_Color.equals("blue"))
col = Color.blue;
else
if(get_Color.equals("red"))
col = Color.red;
else
if(get_Color.equals("green"))
col = Color.green;
else
if(get_Color.equals("yellow"))
col = Color.yellow;
else
col = Color.white;
return col;
}
}
This shows you a little about checking arguments and making decisions based on that argument.
You could write a similar routine to process a font type or something else that might affect the
text position or appearance. In the next section is sample HTML code to run this applet in a Web
browser.
<applet code = "paramText.class" width = 300 height = 200>
<param name = "text" value ="Help! I'm floating upward!">
<param name = "delay" value = "100">
<param name = "top" value = "20">
<param name = "bottom" value = "150">
<param name = "backcolor" value = "red">
<param name = "forecolor" value = "green">
<param name = "fontsize" value = "18">
</applet>
As you can see, it is fairly simple to allow the user of your applet to have certain choices when using your applet and it provides more flexibility. Give the code a try and see if you can modify some of it and put in your own parameters. Try adding some extra colors to be processed or allowing the user to change the font or style of the text. You will find that there are many variations and techniques.
Donald King is the Network Manager and Internet Developer for the Kansas Secretary of State. He is also a Computer Information Systems major at Washburn University in Topeka, Kansas.
|
|
|