EXAMPLE 4: Let us now get into a more serious program.
We'll create
a menu which gives the user the choice of displaying the "Hello
World!" message in either red, blue or
green colors. Each time the
user selects one color, the message will be displayed in that color
then the menu will
appear again to allow him another choice.
=========================================================================================
public
class a : pcs {
public override void init() {
tia=toa="t";
// Select "text screen for both text input & output
bli=1;
// Requesting to start execution at block 1.
base.init();
// Must be the last in init()
}
public override void run() {
if (blp==1) {
// Start of code block 1.
cls="r0";
// Set color to "Pure red".
fns="trb14";
// Set font to "times roman",bold,size 14.
os="
MENU";
// and display this text.
tm();
// Calling method tm() to execute.
os="";tm();
// Skip one line
cls="S9";fns="trb10";
// Change color, font
os=" (R) Red.";tm();
// Display this line.
os=" (B) Blue.";tm();
// and this line.
os=" (G) Green.";tm();
// and this line
os=" (E) Exit.";tm();
// and this line
os="";tm();
// skip one line
cls="b0";
// Change Color.
os="Type your selection into the text field at bottom then hit the [ENTER]
key.";tm();
os="Selection :";bli=2;tm("i");// Get user selection and jump to block 2.
}
if (blp==2) {
// Start of code block 2.
String message="Hello World."; // Message to be displayed.
int index="EeRrBbGg".IndexOf(os);// Check selection
if (index<0){
// If unexpected char entered
bli=1;um("b");
// return to block 1.
return;
}
else if (index<2) sm("e");// If selection was "E
or e" exit program
else if (index<4) {os=message;cls="r0";tm();}
// If selection was "R or r" display msg in red.
else if (index<6) {os=message;cls="b0";tm();}
// If selection was "B or b" display msg in blue.
else if (index<8) {os=message;cls="G2";tm();}
// If selection was "G or g" display msg in green.
bli=1;um("b");
// Then return back to block 1 to repeat the menu.
}
}
}
=========================================================================================
HOW
TO WRITE, COMPILE AND RUN THE PROGRAM? See Example 1.
=========================================================================================
TUTORIAL:
(1)
The first thing to notice is that the program is divided into seperate blocks. Each
block is asigned a unique number and
in method init(), we have made a request to start
execution at block 1. Now why do we need blocks:
(a) The utilities method um() allows us to jump
from any block to another which greatly
simplifies programing tasks as you'll see in this program.
(b) When we come to examples on components, you'll
see an additional programming
simplification. You do not have to worry about delegates any more. Handling
events is
very easy.
(2) JUMPING BETWEEN BLOCKS: You can do any jump (forward
or backward) by assigning the
destination block to (bli) and calling the utilities method at mode "b". There is one
important
rule. The jump statement should be the last one to be executed in the original
block. Since this rule is very important,
always follow each jump statement with a
(return;) even if it was naturally the last in the block.
(3) GETING DATA BACK FROM THE USER: Method tm() at mode
"i" allows the user to "input"
data into your program. In the example, after we have displayed the menu to the user, we
wanted
to display the phrase "Selection?" then read back user's response. Method tm() does
the two jobs. It uses (os) for both
jobs, which means that it expects our phrase to be
asigned to (os) and it sends us the user respose back asigned also to
(os)
There is only one simple problem, your program cannot continue
on same block. Method tm()
must send the user response to a different block. So, we supply the method with the number
of
the block which we like to continue at, terminate our current block and resume the
program at the new block. Since method
tm("i") executes a jump, it must also be followed
with (return;)
(4) Whenever method tm() displays text on the "text screen",
it uses applet's color and
font for the display. PC# initializes their values. Whenever you like to change their
values,
asign your wanted color and font codes to (cls) and (fns) respectively before
calling the method.
(5) FORMING THE COLOR CODE: Your program can state the
color it wants either accurately by
supplying the red, green, blue and opacity components or by using the PC# simplified
method.
For text purposes, we use the simplified method only.
In the simplified method, you supply a three character
code like "r25" to (cls).
The first char tells the color wanted which is "red" in
this case.
The second char is a digit which tells the shade.
It means how much white color or black
color you want to mix your color with. If you want to mix it with white color you
make the
first char a lower case one and if you want to mix it with black color you make it an
upper case one. The second
character can be in the range 0:9 in either case.
"r0" means pure red and "r5" means half way between red
and white. "R0" also means pure
red and "R5" means half the way between red and black.
The third char is for the opacity and it is optional. "0"
means fully transparent, "9"
means fully opaque and if eliminated it will be considered to be "9".
(6) FORMING THE FONT CODE: Let us analyze the code "trbiu12"
.The first two chars
represent the font name. tr=Times Roman cr=Courier hl=Helvetica.
The number at the end represents the font size. The chars
in between the two are optional.
You may add any or all of them. They are: b=bold i=Italic u=Underlined
s=Strikeout.
For more information on color and font, see PC# Reference.
=========================================================================================
Output
of Example 4 is shown below: