Applet Methods
DragonClock.java
To better understand applets let us examine an applet (DragonClock.class) which will paint a digital clock to the computer screen. Applets have four methods available. All applets need not use all four. They are:
- init() - This is called the first time the webpage is loaded. init() is the method where you will declare and construct any necessary objects. Initialization of variables can also go in init().
- start() - After init() has executed, start() is called. start() is also called whenever the browser returns to the webpage.
- stop() - stop() is called any time the browser leaves the webpage.
- destroy() - This method, if used, is called before the browser closes. Not all applets use destroy(). Use this method if you have processes you do not want to have to recreate every time the browser returns to the webpage.
In the last lesson we examined the DragonClock applet up to the init() method. Now we take each of the Applet methods and examine the code.
The code will be in red and comments will be in blue. Any commentary will be in regular text.
init()
public void init()
{
// Get PARAMETERS from HTML file
String background = getParameter("background");
String foreground = getParameter("foreground");
/**
SWITCH statement returns type STRING. In order to use type INT
with SWITCH, you must first wrap the parameter in INTEGER.
Notice that as with the background and foreground variables, the
BACKGND and FOREGND variables can be declared and initialized
in one step in the body of the program.
**/
We define an instance of type String for the background and foreground colors. The getParameter method returns type int and we need type String to build a clock display. It is therefore necessary to wrap the type int in type String. This is one method of type casting in Java.
int backgnd = Integer.parseInt(background);
int foregnd = Integer.parseInt(foreground);
/**
Set up a FOR loop to set background and foreground colors. This way
we only have one set of SWITCH statements. COUNTER keeps track of
how many times we go through the loop. COL takes the parameter color
and runs the SWITCH logic. COL then returns the predefined color to
the program.
PARAMETER -> COL -> PREDEFINED COLOR -> COL -> BACKGROUND or FOREGROUND color
**/
We have taken a rather obtuse path to define our colors. We want to use a switch block to match pre-defined colors with the parameters in the HTML file. We want to use only one block of switch statements. We therefore must make two passes through the switch logic. To acomplish this, I used one variable (col) to hold the int value of the parameter. col holds both background and foreground colors in turn. I must have a 'flip-flop' block before the switch block to choose first the background attribute and second the foreground. I must have another 'flip-flop' block to actually set the particular color.
The way it works is like this: counter will point to the applet background color the first run through the 'for' loop. In the second run through the 'for' loop, counter will point to the applet foreground color. To start off, counter points to the background color parameter read from the HTML file. The value of the parameter is passed to col. The switch block matches the value of col to a pre-defined color. Now we turn our attention back to counter. counter now points to the setBackground method. The method is executed and the color is set. We are at the bottom of the 'for' loop. We loop back to the top, increment counter, and execute the loop again. The second run through the loop points to the foreground color attribute. After two iterations of the 'for' loop, we fall through the loop and continue with the program.
I took this approach rather than have two passes through pre-defined colors and multiple if-then statements. I wanted to use pre-defined colors because there is no need to use values for the red, green, and blue components of the colors.
int col,counter;
for (counter = 1; counter < 3; counter++ )
{
// First, setup which parameter to SWITCH
if (counter == 1)
{
col = backgnd;
}
else
{
col = foregnd;
}
// SWITCH to proper color
switch (col)
{
case 1:
colour = Color.black;
break;
case 2:
colour = Color.blue;
break;
case 3:
colour = Color.cyan;
break;
case 4:
colour = Color.darkGray;
break;
case 5:
colour = Color.gray;
break;
case 6:
colour = Color.green;
break;
case 7:
colour = Color.lightGray;
break;
case 8:
colour = Color.magenta;
break;
case 9:
colour = Color.orange;
break;
case 10:
colour = Color.pink;
break;
case 11:
colour = Color.red;
break;
case 12:
colour = Color.white;
break;
case 13:
colour = Color.yellow;
break;
default:
if (counter == 1)
{
colour = Color.black;
}
else
{
colour = Color.white;
}
} // end of SWITCH block
// Set the color
switch (counter)
{
case 1:
setBackground(colour);
break;
case 2:
setForeground(colour);
}
} // end of FOR loop
} // end of INIT method
start()
public void start()
{
/**
Applet method.
Start the applet and create a thread for the clock to
run under.
**/
thread = new Thread();
thread.start();
}
start() is an applet method. The only thing of note is that we are running the clock display under a thread.
In the last lesson we briefly mentioned using a thread. What does it mean to run a thread in a program? What is a thread, anyway? Simply put, a thread is a subprocess running concurrently with a program. By using threads a program can do two or more things at once. Threads allow multi-tasking to occur. If we were writing a game program to have enemey spaceships attack your spaceship, then each spaceship could be controlled with its own thread.
Why do we want to use a thread in this instance? If we ran the clock display without a thread, we might either lock up the browser or fail to have a smoothly updating clock. This way we can put the thread to 'sleep' for a few milli-seconds to allow the computer time to do other tasks. We then awaken the thread and continue to display the clock. The whole process occurs so quickly that we are not aware it is happening.
stop()
public void stop()
{
/**
Applet method.
Make sure that the thread, and therefore the processing of the
clock, are stopped when the browser goes to another page.
**/
thread = null;
}
All this method does is stop the thread if the browser leaves the webpage. If we go back to the page, the start() method will be called, and we start a new thread and again display the current time.
destroy()
There is no destroy() method in DragonClock. There is nothing in this applet that needs to remain in existance when the browser displays another webpage. Therefore, there is nothing to stop whenever the browser closes.
In the next lesson we will look at required methods other than those required by Applet.
Next ->
Java and java Development Kit are trademarks of Sun Microsystems.
All material is copyright of P & G Dragonsites and may not be reproduced in whole or in part in any media without prior permission.
For questions or comments please contact us at pgdragonsites@thecenter.zzn.com
Reprinted with permission from the author. Click here to visit the original version
Download this tutorial (~30 KB)
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.