Drawing Shapes and Lines C# Help

You have almost finished the first part of the chapter, and you have seen all the basic classes and objects required to draw specified shapes and so on to the screen, This section starts by reviewing some of the drawing methods the Graphics class makes available and presents a short example that illustrates the use of several brushes and pens.

System Drawing Graphics has a large number of methods that allow you to draw various lines, outline shapes, and solid shapes. Once again, there are too many to provide a comprehensive list here, but the following table lists the main ones and should give you some idea of the variety of shapes you can draw.

Before we leave the subject of drawing simple objects, this section rounds off with a simple example that demonstrates the kinds of visual effects you can achieve using brushes, The example is called Scroll More Shapes, and it is essentially a revision of Scroll Shapes, Besides the rectangle and ellipse, you will add a thick line and fill in the shapes with various custom brushes, You have already learned the principles of drawing, so the code.speaks for itself, First, because of your new brushes, you need to indicate that you are using the System. Drawing. Drawing 2D namespace:

Next are Some extra fields in your Forml class, which contain details of the locations where the shapes are to be drawn, as well as various pens and brushes you will use:

The brickBrush field has been declared as static so that you can use its value to initialize the brickWidePen field, C# will not let you use one instance field to initialize another instance field because it has not defined which on will be initialized first, However, declaring the field as static solves the problem, Because only one instance of the Forml class will be instantiated, it is immaterial whether the fields are static or instance fields.

The following is the On Paint () override:

As before, you also set the AutoScrollMinsize to (250,350). Figure 33-13 shows the new results.

Notice that the thick diagonal line has been drawn on top of the rectangle and ellipse because it was the last item to be painted.

Figure 33-13

Figure 33-13

Posted on November 3, 2015 in Graphics with GDI+

Share the Story

Back to Top