1 00:00:00,000 --> 00:00:00,310 2 00:00:00,310 --> 00:00:03,050 >> SPEAKER 1: Let's write a program that has a graphical user interface that 3 00:00:03,050 --> 00:00:06,940 includes a slider that a user can move left to right in order to choose some 4 00:00:06,940 --> 00:00:08,720 value between two integers. 5 00:00:08,720 --> 00:00:11,220 >> To do so, I've begun with some placeholder code. 6 00:00:11,220 --> 00:00:12,720 And now let's fill in a blank. 7 00:00:12,720 --> 00:00:17,760 Let's first allocate a GSlider, calling it slider, and assign to it 8 00:00:17,760 --> 00:00:20,690 the return value of getGSlider. 9 00:00:20,690 --> 00:00:24,160 And now let's specify that I want the lower bound of the slider to be an 10 00:00:24,160 --> 00:00:28,800 integer of 0, the upper bound of the slider to be an integer of 100, and 11 00:00:28,800 --> 00:00:31,860 the default initial value to be 50. 12 00:00:31,860 --> 00:00:35,760 >> Next, let's go ahead and associate an action command with this slider by 13 00:00:35,760 --> 00:00:40,220 calling setActionCommand, passing in slider, and passing in an arbitrary 14 00:00:40,220 --> 00:00:44,500 but unique string like quote unquote "slide." In other words, "slide" is 15 00:00:44,500 --> 00:00:48,150 going to be the identifier uniquely associated with this slider. 16 00:00:48,150 --> 00:00:51,500 Just in case my user interface had multiple sliders, I could distinguish 17 00:00:51,500 --> 00:00:53,180 among them with this technique. 18 00:00:53,180 --> 00:00:58,620 >> Next, let's call addToRegion, passing in window, passing in slider, passing 19 00:00:58,620 --> 00:01:03,310 in quote unquote "SOUTH," in order to add the slider to the southernmost 20 00:01:03,310 --> 00:01:04,830 region of our user interface. 21 00:01:04,830 --> 00:01:08,310 Where quote unquote "SOUTH" happens to be unique identifier defined in the 22 00:01:08,310 --> 00:01:09,950 Stanford Portable Library. 23 00:01:09,950 --> 00:01:12,760 >> But now, for clarity, to the left of the slider let's hard 24 00:01:12,760 --> 00:01:13,980 code the number 0. 25 00:01:13,980 --> 00:01:17,020 And to the right of the slider let's hard code the number 100. 26 00:01:17,020 --> 00:01:20,590 So that the user knows by sliding the thing to the left or to the right how 27 00:01:20,590 --> 00:01:22,170 they're changing its value. 28 00:01:22,170 --> 00:01:26,060 >> Before we allocate that slider, let's go back here and allocate a GLabel, 29 00:01:26,060 --> 00:01:27,210 calling it left. 30 00:01:27,210 --> 00:01:32,170 And assign it the return value of newGLabel, passing in quote unquote-- 31 00:01:32,170 --> 00:01:33,500 in other words-- 32 00:01:33,500 --> 00:01:39,250 of "0." Let's now call addToRegion, passing in window, passing in left, 33 00:01:39,250 --> 00:01:43,560 passing in quote unquote "SOUTH." So that this label is inserted into that 34 00:01:43,560 --> 00:01:45,600 southern region before the slider. 35 00:01:45,600 --> 00:01:49,950 >> And now after the slider, let's go ahead and declare another GLabel. 36 00:01:49,950 --> 00:01:54,550 Calling it right, assigning it the return value of newGLabel. 37 00:01:54,550 --> 00:01:56,320 Quote unquote-- another string-- 38 00:01:56,320 --> 00:02:02,290 "100." And then call addToRegion, passing in window, passing in right, 39 00:02:02,290 --> 00:02:04,640 passing in "SOUTH" as well. 40 00:02:04,640 --> 00:02:08,580 In other words, we've just declared a label, a slider, and another label and 41 00:02:08,580 --> 00:02:12,200 inserted them into the southern region in that order, so that the user knows 42 00:02:12,200 --> 00:02:15,510 when he or she moves it to the right or to the left roughly what value 43 00:02:15,510 --> 00:02:16,380 they'll be approaching. 44 00:02:16,380 --> 00:02:19,040 >> Let's now induce an infinite loop. 45 00:02:19,040 --> 00:02:21,755 And inside of this loop, lets first check for a 46 00:02:21,755 --> 00:02:24,710 GActionEvent, calling it event. 47 00:02:24,710 --> 00:02:27,740 Assigning it the return value of waitForEvent. 48 00:02:27,740 --> 00:02:32,430 Passing in ACTION-EVENT, a constant declared in the Stanford Portable 49 00:02:32,430 --> 00:02:36,460 Library that indicates that you indeed want to wait for an action. 50 00:02:36,460 --> 00:02:42,420 >> Let's next check if getEventType, passing in event, equals 51 00:02:42,420 --> 00:02:46,330 WINDOW_CLOSED, another constant declared in the Stanford Portable 52 00:02:46,330 --> 00:02:49,970 Library that indicates that the action is the closing of a window. 53 00:02:49,970 --> 00:02:53,640 Then let's go ahead and break out of this otherwise infinite loop. 54 00:02:53,640 --> 00:02:57,480 >> Else, let's check if the user has slid that slider. 55 00:02:57,480 --> 00:03:04,930 If string compare of getActionCommand, passing in event, comma, quote unquote 56 00:03:04,930 --> 00:03:07,520 "slide" equals equals 0. 57 00:03:07,520 --> 00:03:10,840 In other words, if the unique identifier associated with the slider 58 00:03:10,840 --> 00:03:15,070 that has been slid equals quote unquote "slide," the unique identifier 59 00:03:15,070 --> 00:03:18,300 that we provided earlier, then I want to do the following. 60 00:03:18,300 --> 00:03:25,040 >> Printf "slider was slid to %i backslash n," indicating that I'd like 61 00:03:25,040 --> 00:03:28,200 to plug in the integral value of the slider. 62 00:03:28,200 --> 00:03:34,300 So comma getValue of slider. 63 00:03:34,300 --> 00:03:39,210 >> Let's now save, compile, run this program, and slide that slider. 64 00:03:39,210 --> 00:03:41,200 Make slider. 65 00:03:41,200 --> 00:03:42,990 ./slider. 66 00:03:42,990 --> 00:03:44,270 There's my user interface. 67 00:03:44,270 --> 00:03:48,130 There's my slider, to the left of which is a 0, to the right of which is 68 00:03:48,130 --> 00:03:49,820 100, both of them labels. 69 00:03:49,820 --> 00:03:52,740 And as I slide the slider to the left and the right, notice that in my 70 00:03:52,740 --> 00:03:56,160 console window I'll see the integer value. 71 00:03:56,160 --> 00:03:59,840 >> Now, I don't necessarily see all of the numbers between 0 and 100, because 72 00:03:59,840 --> 00:04:02,190 depending on the speed with which I move the slider, my 73 00:04:02,190 --> 00:04:03,540 program might not notice. 74 00:04:03,540 --> 00:04:07,040 But indeed, if I move it to any particular location, that's the value 75 00:04:07,040 --> 00:04:08,290 that will print. 76 00:04:08,290 --> 00:04:10,006