SPEAKER 1: Let's write a program with a graphical user interface that also
includes label.
And in this label, we're going to store number, albeit as a string.
And we're going to update that label again, and again, and again.
So that we ultimately count down from 50 to zero.
Glabel, calling it label, gets the return value of new glabel.
Now, I'm not going to give this label value yet, so I'll
put in quote, unquote.
>> Next let's call setFont, passing in the label, and let's pass in
specifically a font called SansSerif 36 point.
A font that happens to exist inside the CS50 appliance.
Then finally, let's add the label to the window as follows.
Now, let's proceed to induce a loop that's going to count
from 50 down to zero.
And within that loop, let's iteratively update the label and
display it on the screen, For, int i gets 50, i is greater than, or equal
to zero, i minus, minus.
>> Now labels, it turns out, have to be strings.
But I'm actually counting, using integers.
So somehow I'm going to have to convert the integer, i, to a string
representation arrow.
To do so let's declare char s bracket three.
So that we have enough storage space for two digit number, followed by a
null terminator.
Then let's call s print f passing in s, passing in quote,
unquote percent i.
Indicating that we indeed want to format an integer.
Finally passing in i itself.
In other words, s print f, or string print f, just like print f, expects a
format string followed by some variables to substitute into that
format string.
But it also accepts is its first argument, the location in which you
would like to store the string that you've represented
with that format string.
>> So next, let's go ahead and call set, label, passing in label, passing in s.
Now, finally, just because this labels width is going to change over time as
we count down from 50, to 49, to dot, dot, dot, to nine to eight.
Which aren't as wide as a two digit number is.
Let's proceed to figure out dynamically what the width of this
label should be and then ensure that it's always centered on the screen.
I'm first going to declare a double, calling it x, and I'm then going to
store inside of x, the result of get width, passing in the width of the
whole window, minus get width, passing in the label.
Then dividing the whole thing by two.
Similarly I'm going to declare y to be equal to get height of the whole
window, minus get height of just the label, and divide that by two as well.
Finally, I'm going to call setlocation, passing in the label,
passing in x, passing in y.
There by positioning the label at x comma y.
>> Finally, so that this countdown doesn't happen too rapidly let's
pause, for say, 100 milliseconds between each update of the label.
To do so, we can call the pause function, that's defined in the
Stanford portable library, quite simply as follows.
>> Now let's save, compile, and run this program.
Make label, dot slash, label.
There's my user interface counting down from 50.
Counting down, and down, and down, and done.