1 00:00:00,000 --> 00:00:00,490 2 00:00:00,490 --> 00:00:03,350 SPEAKER 1: Let's write a program with a graphical user interface that also 3 00:00:03,350 --> 00:00:04,580 includes label. 4 00:00:04,580 --> 00:00:07,940 And in this label, we're going to store number, albeit as a string. 5 00:00:07,940 --> 00:00:10,880 And we're going to update that label again, and again, and again. 6 00:00:10,880 --> 00:00:15,040 So that we ultimately count down from 50 to zero. 7 00:00:15,040 --> 00:00:20,910 Glabel, calling it label, gets the return value of new glabel. 8 00:00:20,910 --> 00:00:22,900 Now, I'm not going to give this label value yet, so I'll 9 00:00:22,900 --> 00:00:25,040 put in quote, unquote. 10 00:00:25,040 --> 00:00:29,080 >> Next let's call setFont, passing in the label, and let's pass in 11 00:00:29,080 --> 00:00:32,910 specifically a font called SansSerif 36 point. 12 00:00:32,910 --> 00:00:36,370 A font that happens to exist inside the CS50 appliance. 13 00:00:36,370 --> 00:00:41,940 Then finally, let's add the label to the window as follows. 14 00:00:41,940 --> 00:00:44,580 Now, let's proceed to induce a loop that's going to count 15 00:00:44,580 --> 00:00:46,400 from 50 down to zero. 16 00:00:46,400 --> 00:00:49,520 And within that loop, let's iteratively update the label and 17 00:00:49,520 --> 00:00:55,800 display it on the screen, For, int i gets 50, i is greater than, or equal 18 00:00:55,800 --> 00:00:59,750 to zero, i minus, minus. 19 00:00:59,750 --> 00:01:02,090 >> Now labels, it turns out, have to be strings. 20 00:01:02,090 --> 00:01:04,110 But I'm actually counting, using integers. 21 00:01:04,110 --> 00:01:07,750 So somehow I'm going to have to convert the integer, i, to a string 22 00:01:07,750 --> 00:01:09,010 representation arrow. 23 00:01:09,010 --> 00:01:12,480 To do so let's declare char s bracket three. 24 00:01:12,480 --> 00:01:15,500 So that we have enough storage space for two digit number, followed by a 25 00:01:15,500 --> 00:01:16,910 null terminator. 26 00:01:16,910 --> 00:01:22,480 Then let's call s print f passing in s, passing in quote, 27 00:01:22,480 --> 00:01:24,220 unquote percent i. 28 00:01:24,220 --> 00:01:26,960 Indicating that we indeed want to format an integer. 29 00:01:26,960 --> 00:01:29,420 Finally passing in i itself. 30 00:01:29,420 --> 00:01:34,100 In other words, s print f, or string print f, just like print f, expects a 31 00:01:34,100 --> 00:01:37,450 format string followed by some variables to substitute into that 32 00:01:37,450 --> 00:01:38,430 format string. 33 00:01:38,430 --> 00:01:41,910 But it also accepts is its first argument, the location in which you 34 00:01:41,910 --> 00:01:44,400 would like to store the string that you've represented 35 00:01:44,400 --> 00:01:45,830 with that format string. 36 00:01:45,830 --> 00:01:52,540 >> So next, let's go ahead and call set, label, passing in label, passing in s. 37 00:01:52,540 --> 00:01:56,430 Now, finally, just because this labels width is going to change over time as 38 00:01:56,430 --> 00:02:00,640 we count down from 50, to 49, to dot, dot, dot, to nine to eight. 39 00:02:00,640 --> 00:02:03,170 Which aren't as wide as a two digit number is. 40 00:02:03,170 --> 00:02:05,570 Let's proceed to figure out dynamically what the width of this 41 00:02:05,570 --> 00:02:08,930 label should be and then ensure that it's always centered on the screen. 42 00:02:08,930 --> 00:02:12,390 I'm first going to declare a double, calling it x, and I'm then going to 43 00:02:12,390 --> 00:02:16,880 store inside of x, the result of get width, passing in the width of the 44 00:02:16,880 --> 00:02:22,120 whole window, minus get width, passing in the label. 45 00:02:22,120 --> 00:02:24,880 Then dividing the whole thing by two. 46 00:02:24,880 --> 00:02:29,660 Similarly I'm going to declare y to be equal to get height of the whole 47 00:02:29,660 --> 00:02:37,280 window, minus get height of just the label, and divide that by two as well. 48 00:02:37,280 --> 00:02:40,680 Finally, I'm going to call setlocation, passing in the label, 49 00:02:40,680 --> 00:02:43,180 passing in x, passing in y. 50 00:02:43,180 --> 00:02:45,970 There by positioning the label at x comma y. 51 00:02:45,970 --> 00:02:49,290 >> Finally, so that this countdown doesn't happen too rapidly let's 52 00:02:49,290 --> 00:02:53,350 pause, for say, 100 milliseconds between each update of the label. 53 00:02:53,350 --> 00:02:56,320 To do so, we can call the pause function, that's defined in the 54 00:02:56,320 --> 00:03:00,390 Stanford portable library, quite simply as follows. 55 00:03:00,390 --> 00:03:04,230 >> Now let's save, compile, and run this program. 56 00:03:04,230 --> 00:03:08,320 Make label, dot slash, label. 57 00:03:08,320 --> 00:03:11,000 There's my user interface counting down from 50. 58 00:03:11,000 --> 00:03:14,570 Counting down, and down, and down, and done. 59 00:03:14,570 --> 00:03:16,992