Graphics Reference
In-Depth Information
dots; meshes; and images. The details of these various entities are given on the
book's website.
As you saw in Chapter 2, WPF contains tools for creating animations. You can
animate almost any kind of value—a
double
,a
Point
, etc.—and then use that
changing value to make the display change. You can define animations in XAML
code or in C#. For XAML, there are many predefined animations that you can
combine to make quite complex motions. In C# code, you can use these predefined
animations or you can create your own using arbitrarily complex programs. You
might, for instance, write a program that computes the position of a bouncing ball
over time, and then use that varying position to control the location of some shape
(like a disk) shown on the display. Writing the simulation program in XAML isn't
really feasible, so it's more natural to write such a program in C#.
In our sample, we have just one animation, but it shows off the key ideas
(see Listing 4.8). In the code, we constructed a segment with one endpoint being
at a
Dot
called
p1
. In this code, we animate a point by specifying the start-
ing and ending locations, how long the animation should take (it starts at time
0, i.e., when the program is run, and takes five seconds), and the fact that it
should auto-reverse and should repeat forever. (This relatively simple anima-
tion is an example of something that could be easily described in XAML, by
the way.) The result is a point that moves back and forth between the two
specified locations over time. Note that this point is not actually displayed
on the
GraphPaper
; it's just a
Point
whose value changes constantly. But the
line
p1.BeginAnimation(Dot.PositionProperty, animaPoint1);
says that the
PositionProperty
of the
Dot
called
p1
should be animated by
animaPoint1
;
this causes that
Dot
to move back and forth on the display. Once again, WPF's
dependency mechanism is doing the hard work: It is detecting every change in
animaPoint1
and making sure that the
PositionProperty
of
p1
is changed as
well; this property is what determines the location of the
Dot
on the canvas so that
we see motion.
Listing 4.8: Code to animate a
Point
.
1
2
3
4
5
6
7
PointAnimation animaPoint1 = new PointAnimation(
new Point(-20, -20),
new Point(-40, 20),
new Duration(new TimeSpan(0, 0, 5)));
animaPoint1.AutoReverse = true;
animaPoint1.RepeatBehavior = RepeatBehavior.Forever;
p1.BeginAnimation(Dot.PositionProperty, animaPoint1);
We've been working with
Point
s quite freely, assigning values to their
x
- and
y
-coordinates. Rigid adherence to object-oriented programming doctrine might
require that we treat these coordinates as instance variables to be hidden, and
that we access them only through accessor/mutator (or get/set) methods. In fact, a
Point
is rather more like a Pascal
record
or a C
struct
than a typical C++ or Java
object. In blurring the distinction between
records
(collections of related values)
and objects, C# allows such uses, which can have a huge impact on efficiency with
relatively little impact on debuggability.