Tip:
Highlight text to annotate it
X
>> SPEAKER 1: Let's write a program that has a graphical user interface that
includes a slider that a user can move left to right in order to choose some
value between two integers.
>> To do so, I've begun with some placeholder code.
And now let's fill in a blank.
Let's first allocate a GSlider, calling it slider, and assign to it
the return value of getGSlider.
And now let's specify that I want the lower bound of the slider to be an
integer of 0, the upper bound of the slider to be an integer of 100, and
the default initial value to be 50.
>> Next, let's go ahead and associate an action command with this slider by
calling setActionCommand, passing in slider, and passing in an arbitrary
but unique string like quote unquote "slide." In other words, "slide" is
going to be the identifier uniquely associated with this slider.
Just in case my user interface had multiple sliders, I could distinguish
among them with this technique.
>> Next, let's call addToRegion, passing in window, passing in slider, passing
in quote unquote "SOUTH," in order to add the slider to the southernmost
region of our user interface.
Where quote unquote "SOUTH" happens to be unique identifier defined in the
Stanford Portable Library.
>> But now, for clarity, to the left of the slider let's hard
code the number 0.
And to the right of the slider let's hard code the number 100.
So that the user knows by sliding the thing to the left or to the right how
they're changing its value.
>> Before we allocate that slider, let's go back here and allocate a GLabel,
calling it left.
And assign it the return value of newGLabel, passing in quote unquote--
in other words--
of "0." Let's now call addToRegion, passing in window, passing in left,
passing in quote unquote "SOUTH." So that this label is inserted into that
southern region before the slider.
>> And now after the slider, let's go ahead and declare another GLabel.
Calling it right, assigning it the return value of newGLabel.
Quote unquote-- another string--
"100." And then call addToRegion, passing in window, passing in right,
passing in "SOUTH" as well.
In other words, we've just declared a label, a slider, and another label and
inserted them into the southern region in that order, so that the user knows
when he or she moves it to the right or to the left roughly what value
they'll be approaching.
>> Let's now induce an infinite loop.
And inside of this loop, lets first check for a
GActionEvent, calling it event.
Assigning it the return value of waitForEvent.
Passing in ACTION-EVENT, a constant declared in the Stanford Portable
Library that indicates that you indeed want to wait for an action.
>> Let's next check if getEventType, passing in event, equals
WINDOW_CLOSED, another constant declared in the Stanford Portable
Library that indicates that the action is the closing of a window.
Then let's go ahead and break out of this otherwise infinite loop.
>> Else, let's check if the user has slid that slider.
If string compare of getActionCommand, passing in event, comma, quote unquote
"slide" equals equals 0.
In other words, if the unique identifier associated with the slider
that has been slid equals quote unquote "slide," the unique identifier
that we provided earlier, then I want to do the following.
>> Printf "slider was slid to %i backslash n," indicating that I'd like
to plug in the integral value of the slider.
So comma getValue of slider.
>> Let's now save, compile, run this program, and slide that slider.
Make slider.
./slider.
There's my user interface.
There's my slider, to the left of which is a 0, to the right of which is
100, both of them labels.
And as I slide the slider to the left and the right, notice that in my
console window I'll see the integer value.
>> Now, I don't necessarily see all of the numbers between 0 and 100, because
depending on the speed with which I move the slider, my
program might not notice.
But indeed, if I move it to any particular location, that's the value
that will print.