Exercises
447
{
// Find the next point’s coordinates.
oat x1 = marginX + scaleX * i;
oat y1 = graphPictureBox.ClientSize.Height –
marginY - scaleY * values[i];
// Draw a line from the previous point to this one.
graphics.DrawLine(Pens.Black, x0, y0, x1, y1);
// Draw this point’s value in text.
if (values[i] >= 50)
{
graphics.DrawString(values[i].ToString(),
this.Font, Brushes.Black, x1, y1 - 12);
}
else
{
graphics.DrawString(values[i].ToString(),
this.Font, Brushes.Red, x1, y1);
}
// Update (x0, y0).
x0 = x1;
y0 = y1;
}
}
Please select Lesson 39 on the DVD to view the video that accompanies this lesson.
EXERCISES
1. Build a program that uses a Paint event handler to draw a picture similar to the one shown
in Figure 39-6. (Feel free to embellish with ears, a hat, a scarf, or anything else you like.)
2. Build a program that uses a Paint event handler to draw a bar chart similar to the one
shown in Figure 39-7. (Hint: Use the program you built for the Try It as a starting point.)
FIGURE 396
FIGURE 397
596906c39.indd 447 4/7/10 12:35:15 PM
448
LESSON 39 Drawing with gDi+
You can download the solutions to these exercises from the book’s web page at
www.wrox.com or www.CSharpHelper.com/24hour.html. You can find those
solutions in the Lesson39 folder.
3. Build a Scribble program similar to the one shown in Figure 39-8. Hints: Make fields
isDrawing (true when drawing) and lastPoint (the last point drawn). Make a MouseDown
event handler that sets
isDrawing to true and records the current point. In the MouseMove
event handler, verify that the user is drawing, draw between the last and current points, and
update
lastPoint. In the MouseUp event handler, set isDrawing to false. Do all drawing on
a
Bitmap that is created when the program starts and if the user selects the File menu’s New
command. Finally, be sure to call the
PictureBox’s Refresh method any time you draw.
4. (Hard) Make a program that uses a Paint event handler to draw a star as shown in
Figure 39-9. Make the number of points a constant and allocate arrays big enough to hold
the star’s corner points and the
PathGradientBrush’s SurroundColors. (Hint: If the star
has 7 points as in Figure 39-9, you’ll need 14 corner points in the polygon and 14 colors for
the brush.) Use
Math.Cos and Math.Sin to calculate positions for the points.
FIGURE 398
FIGURE 399
596906c39.indd 448 4/7/10 12:35:15 PM
Click here to Play
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.190.182