
CHAPTER 2 Subjects and Views

Here, manipulating the scrollbar causes the square to change various shades of gray, from black, when the thumb-button is at the bottom of the slider, to white, when it is at the top. The object diagram for grays is shown in figure 2-8.
The grays program uses the BoundedFloat subject and introduces a new subject which is a Fresco FigureStyle object. A FigureStyle is just a set of attributes (foreground, background, brush, and font) used by a Fresco figure object. (Figures in Fresco are graphical objects such as rectangles, circles, and splines.) Two new views are added: the RgbView which observes the BoundedFloat, and a ColoredFigView which observes the FigureStyle. The code for these views is shown in listing 2-3.
The behavior of the grays program is simple:
(1) When the BoundedFloat subject changes (due to the scrollbar), the RgbView is updated, and it uses the new value from the BoundedFloat to set the foreground color of the FigureStyle. This is done on line 22 of listing 2-3.
(2) The RgbView then tells the style to notify its views (line 23), so update() is called on the ColoredFigView.
(3) The ColoredFigView defines update() to call need_redraw() on the figure object (line 46). This causes the figure to be redrawn in the new color during the next draw traversal. (More about draw traversals next month.)
Finally, it's time to come back around to the RGB program of figure 1. We already have all the components we need to build this program; it's now simply a matter of snapping them together in the correct configuration. This configuration is shown in the object diagram of figure 2-9.
In this chapter we've built some simple views and connected them to pre-existing subjects, and, as a result, we've created some simple, yet interesting, application programs such as the RGB color editor. The code for these programs can be found in the tutorial directory on the Fresco release.

Generated with CERN WebMaker