Special Effects and Game Development
in Java(TM) - Other examples with showStatus()
by Anibal Wainstein
3.1 Other examples with showStatus()
Status window effects are often used among web designers
today. Thanks to our new knowledge in threads, we can begin
making some status window animations.
3.1.1 Your first scroller (statusscroller)
I remember that the first applet I programmed was an applet
called URLScroll.
It was one of the most common effects at that time, and they
usually were a text message that moved from right to left
or upwards. We reviewed in section 1.4.1
how to get a substring from a larger string. Start by opening
the old threadtest.java file and save a copy under another
name ("statusscroller" for instance). Now we add
an init() method where the message will be initialized:
public String message; public void init()
{
message=" "; message+=" "; message+=" "; message+=" "; message+="Anibal Wainstein's Basic Course "; message+="in Special Effects and Game development in Java(TM)";}
First we declare a global variable called "message".
This will contain the actual message that we will write. We
start by initializing the variable with a blankspace line and
then by adding more subsequent blankspace lines. The message
is added at the end of these lines. If you are confused by the
way I am creating the message then you should know that:
message="message 1 ";
message+="message 2";
is the same thing as
message="message 1 " + "message 2";
The idea with our scroller is that we crop the string and show
shorter and shorter bits on the status window until there is
nothing left to show. Rewrite the run() method in the old applet:
public void run()
{ while (true) { int L=message.length(); for (int i=0; i<L; i++) { showStatus(message.substring(i,L)); try {Thread.sleep(100);} catch(InterruptedException e) {} } }
}
Here, we start by first finding out the length of our string,
with the help of the String class' length() method, the result
is stored in the variable "L". The for-loop will count
up from position 0 in the string, to the end of the message.
Substring() can then take the part of the string that begins
on this position and ends at the end of the string, that is
the "L" position. We add a pause of one tenth of a
second (100 milliseconds) after displaying the string. When
the string has been cropped to the end, then the whole process
start again, thanks to us having contained the code within the
infinite while loop.
Take a look at the finished statusscroller by clicking
here .
Next Page >>
|
| |
|