We have learned how to draw
rectangles with various line style in the previous lesson. Now
we shall learn how to draw ellipse and
circle.
23.1 Drawing
Ellipse
First of all we need to understand the
principal behind drawing an ellipse. The basic structure of most
shapes is a rectangle, ellipse is no exception. Ellipse is an oval
shape that is bounded by a rectangle, as shown
below:
Therefore, you need to create a
Rectangle object before you can draw an ellipse. This
rectangle serves as a bounding rectangle for the ellipse. On
the other hand, you still draw an ellipse with the DrawEllipse
method without first creating a rectangle. We will show you
both ways. |
|
In the first method, let say you have
created a rectangle object known as myRectangle and a pen object as
myPen, then you can draw an ellipse using the following
statement:
myGraphics.DrawEllipse(myPen,
myRectangle)
* Assume you have also already created
the Graphics object myGraphics.
The following is an example of the full
code:
Example
23.1(a)
Dim
myPen As Pen
myPen =
New Pen(Drawing.Color.Blue, 5)
Dim
myGraphics As
Graphics =
Me.CreateGraphics
Dim
myRectangle As
New Rectangle
myRectangle.X = 10
myRectangle.Y = 10
myRectangle.Width = 200
myRectangle.Height = 100
myGraphics.DrawEllipse(myPen,
myRectangle)
The output image is shown in the
following diagram:
The second method is using the
DrawEllipse method without creating a rectangle object. Offcourse
you still have to create the Graphics and the Pen objects. The
syntax is:
myGraphics.DrawEllipse(myPen,
X,Y,Width, Height)
Where (X,Y) are the coordinates of the
upper left corner of the bounding rectangle, width is the width of
the ellipse and height is the height of the ellipse.
The
following is an example of the full
code:
Example
23.1(b)
Dim
myPen As
Pen
myPen = New
Pen(Drawing.Color.Blue, 5)
Dim
myGraphics As
Graphics = Me.CreateGraphics
myGraphics.DrawEllipse(myPen, 10,
10, 200,
100) |
|