Graphics Reference
In-Depth Information
development environment in which you would like to work. In the examples in
this topic, we used Visual Studio 2010 in a freely distributed “basic edition,”
along with the Microsoft Windows SDK for browsing documentation, among
other things. Follow the directions on the website for getting the 2D test bed up
and running, and then experiment with the sample program: Try clicking a button
or moving a slider and see what happens. Browse through the code to see if you
can begin to piece together how it works. Then read on.
We assume that you are familiar with an integrated development environment
like Visual Studio and that you know a programming language like C#, C++, or
Java. (The test bed is written in C#, but anyone familiar with C++ or Java will find
it easy to use C#.)
4.2.2 Corner Cutting
In Section 4.6, we develop the corner-cutting application step by step. To prepare
for that exercise, download the Subdivide application from the topic's website and
then run it. When the application starts, click several times in the main window
to create a polygon; then click on the Subdivide button to see the corner-cutting
operation, or the Clear button to start over.
Examine the Window1.xaml code. You'll see the words “Subdivide” and
“Clear”; the XAML code around these words creates the buttons you were
clicking, and Click="b1Click" tells WPF that when one of those buttons is
clicked, a procedure called b1Click should be invoked. We'll see more details
of this later; here, we want you to gain a broad picture of where the working parts
of this example lie.
Now examine the Window1.xaml.cs code. The initializations for the Window1
class create a pair of Polygon objects, which are initialized in the constructor for
Window1 and are then add ed to something called gp (for “graph paper”), which
represents everything that will be drawn on the display. The polygon initialization
code sets up certain properties for the polygons; lots of other properties could also
be set, but these are left with their default values. Finally, the b2click and b1click
procedures describe what should happen when the user clicks on the two buttons.
Look at them briefly. The handling of the Clear button should seem reasonably
simple to you; the handling of the Subdivide button is more complicated, but you
can see that at its core, it involves multiplying various coordinates by 1
/
3 and 2
/
3,
as you'd expect.
Those are the essential parts of the corner-cutting application. Almost every-
thing else is boilerplate —the bits of code that make it easy to write applications
like this. In fact, we created this corner-cutting application by starting with the
test bed, which creates points, lines, arrows, a mesh, and various moving things,
deleting most of them, and then editing the XAML to remove some user-interface
components that weren't needed and renaming a few others. With this example in
mind, we can now look at the rest of the test bed.
4.2.3 The Structure of a Test-Bed-Based Program
As you learned in Chapter 3, WPF applications are typically specified in two parts:
one in XAML, the other in C#. The two parts cooperate to make the whole. Indeed,
one can have object classes that have this same two-part separation, or that are
purely C#; our code has both types.
 
 
 
Search WWH ::




Custom Search