Home E-Books Special Effects and Game Development in Java A computer terminal

Special Effects and Game Development in Java(TM) - A computer terminal in your status window (statusdatatext)

by Anibal Wainstein

3.1.2 A computer terminal in your status window (statusdatatext)  

Computer terminals in the beginning of the 80's were very sluggish and had a special way to write out sentences. Here, we shall try to simulate that effect, but on the status window and with only one sentence.

Do a copy of the previous Java file and rewrite the init() method to the following code:

public void init()
    message="Preparing to launch....";
    message+="6... 5... 4... 3... 2... 1... 0..."; }
We do not need blankspaces before the message, because the message will stand still on the status window. Write the following run() method: 
public void run()
    int L=message.length();
    boolean showcursor=false;
    //Write the message step by step with
//the blinking cursor.
    for (int i=0; i<L; i++)
    {         if (showcursor) showStatus(message.substring(0,i)+"_");
        else showStatus(message.substring(0,i));
        try {Thread.sleep(100);}
        catch(InterruptedException e) {}     }     //The message is written. Animate the cursor
//(keep it blinking).
    while (true)
        if (showcursor) showStatus(message+"_");
        else showStatus(message);
        try {Thread.sleep(100);}
        catch(InterruptedException e) {}
    } }
The effect is in two steps and in two loops. The first thing we do before entering the loops is to initialize a variable that will be used for the cursor effect ("showcursor"). Inside the loops and using the following expression, this variable will be set to true when it is false and the other way around:
The keyword "!" means complement or opposite. So "showcursor" will be set to the opposite value of itself. When this variable is true, then the computer cursor will be displayed, which in our case happens to be an underscore character "_". It will be added to the message string before it is written to the status window. In the for loop we write the message step by step using the substring() method (plus the cursor) and add a pause after writing it.
The last while-loop in the run() method will see to that the thread keeps animating the cursor for an infinite time. It is needed to make the effect a bit more realistic.

Click here to see statusdatatext in action. 

Please note, that if you want to speed up the effects in this example and in the example in the last section, then decrease the value in the sleep() method. You will then have a more processor intensive applet. In more complicated effects that demands many calculations this could cause big problems, which we will give example of in later chapters.


Next Page >>