VB2019 VB2017 VB2015 VB2013 VB2012 VB2010 VB2008 VB6 VB Sample Codes 中文VB About Us

Lesson 24: Drawing Ellipse and Circle


24.1 Drawing 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:

Figure 24.1

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 can still draw an ellipse with the DrawEllipse method without first creating a rectangle.

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 24.1(a)

Dim myPen As Pen
Dim myGraphics As Graphics = Me.CreateGraphics
Dim myRectangle As New Rectangle

 myPen = New Pen(Drawing.Color.Blue, 5)
 myRectangle.X = 10
 myRectangle.Y = 10
 myRectangle.Width = 200
 myRectangle.Height = 100
 myGraphics.DrawEllipse(myPen, myRectangle)

The output image is shown in the following Figure 24.2:

Figure 24.2

The second method is using the DrawEllipse method without creating a rectangle object. Off course 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 24.1(b)

Dim myPen As Pen
Dim myGraphics As Graphics = Me.CreateGraphics

myPen = New Pen(Drawing.Color.Blue, 5)
myGraphics.DrawEllipse(myPen, 10, 10, 200, 100)

24.2 Drawing a Circle

After you have learned how to draw an ellipse, drawing a circle becomes very simple. We use exactly the same methods used in the preceding section but modify the width and height so that they are of the same values.

The following examples draw the same circle.

Example 24.2(a)

Dim myPen As Pen
Dim myGraphics As Graphics = Me.CreateGraphics
Dim myRectangle As New Rectangle

myPen = New Pen(Drawing.Color.Blue, 5)
myRectangle.X = 10
myRectangle.Y = 10
myRectangle.Width = 100
myRectangle.Height = 100
myGraphics.DrawEllipse(myPen, myRectangle)

Example 24.2(b)

Dim myPen As Pen
Dim myGraphics As Graphics = Me.CreateGraphics

myPen = New Pen(Drawing.Color.Blue, 5)
myGraphics.DrawEllipse(myPen, 10, 10, 100, 100)

The output image is show below:

Figure 24.3


❮ Previous Lesson Next Lesson ❯

Copyright©2008 Dr.Liew Voon Kiong. All rights reserved |Contact|Privacy Policy