Home

GDI+ Shapes: Pies

 

Description

A pie is a fraction of an ellipse delimited by a starting angle and an angle that constitutes the desired portion to make up a pie. It can be illustrated as follows:

Pie Illustration

To draw a pie, you can use the Graphics.DrawPie() method that comes in various versions as follows:

Public Sub DrawPie ( _
	pen As Pen, _
	rect As Rectangle, _
	startAngle As Single, _
	sweepAngle As Single _
)
Public Sub DrawPie ( _
	pen As Pen, _
	rect As RectangleF, _
	startAngle As Single, _
	sweepAngle As Single _
)
Public Sub DrawPie ( _
	pen As Pen, _
	x As Integer, _
	y As Integer, _
	width As Integer, _
	height As Integer, _
	startAngle As Integer, _
	sweepAngle As Integer _
)
Public Sub DrawPie ( _
	pen As Pen, _
	x As Single, _
	y As Single, _
	width As Single, _
	height As Single, _
	startAngle As Single, _
	sweepAngle As Single _
)

A pie is based on an ellipse (like an arc). The ellipse would fit in a rectangle passed as the rect argument. The rectangle can also be specified by its location (x, y) and its dimensions (width and height).

Inside of the parent rectangle in which an ellipse would be drawn, you set a starting angle. This angle is measured from 0 up counted clockwise (like the numbers of an analog clock). This means that an angle of 90 represents 6 o'clock and not 12 o'clock. This starting angle is passed as the startAngle argument.

After specifying the starting angle, you must specify the amount of angle covered by the pie. This also is measured clockwise. This value is passed as the sweepAngle argument.

Here is an example:

Imports System.Drawing
Imports System.Windows.Forms

Module Exercise

    Public Class Starter
        Inherits Form

        Dim components As System.ComponentModel.Container

        Public Sub New()
            InitializeComponent()
        End Sub

        Public Sub InitializeComponent()

        End Sub

        Private Sub FormPaint(ByVal sender As Object, _
                              ByVal e As PaintEventArgs) _
                              Handles MyBase.Paint

            Dim penCurrent As Pen = New Pen(Color.Red)
            e.Graphics.DrawPie(penCurrent, 20, 20, 200, 100, 45, 255)

        End Sub
    End Class

    Function Main() As Integer

        Dim frmStart As Starter = New Starter

        Application.Run(frmStart)

        Return 0
    End Function

End Module

This would produce:

Pie

Practical LearningPractical Learning: Drawing an Ellipse

  1. Start Microsoft Visual Basic and create a new Windows Application named SchoolEnrolment1
  2. Design the form as follows:
     
    School Enrolment
    Control Name Text
    Label Label   Enrolment / Program ___________________________
    Label Label   Graduates
    Label Label   Undergraduates
    Label Label   Certificates
    TextBox TextBox txtGraduates 0
    TextBox TextBox txtUndergraduates 0
    TextBox TextBox txtCertificates 0
    Button Button btnCreateChart Create Chart
    PictureBox PictureBox pbxChart  
    Label Label   ____Legend____
    Label Label lblGraduates Graduates
    Label Label lblUndergraduates Undergraduates
    Label Label lblCertificates Certificates
    Button Button btnClose Close
  3. Right-click the form and click View Code
  4. Declare three variables as follows:
     
    Public Class Form1
        Private Graduates As Single
        Private Undergraduates As Single
        Private Certificates As Single
    End Class
  5. In the Class Name combo box, select (Form1 Events)
  6. In the Method Name combo box, select Paint and implement the event as follows:
     
    pbxChart.CreateGraphics().DrawEllipse(New Pen(Color.Red), _
                                  New Rectangle(0, 0, 260, 200))
            pbxChart.CreateGraphics().DrawPie(New Pen(Color.Blue), _
                                      0.0F, 0.0F, 260.0F, 200.0F, 0.0F, Graduates)
            pbxChart.CreateGraphics().DrawPie(New Pen(Color.Green), _
                                      0.0F, 0.0F, 260.0F, 200.0F, _
    				  Graduates, Undergraduates)
            pbxChart.CreateGraphics().DrawPie(New Pen(Color.Fuchsia), _
                                      0.0F, 0.0F, 260.0F, 200.0F, _
    				  Graduates + Undergraduates, Certificates)
    
            e.Graphics.DrawEllipse(New Pen(Color.Blue), _
                                  New Rectangle(lblGraduates.Left, _
                                lblGraduates.Top + 20, _
                        lblUndergraduates.Width, 20))
    
            e.Graphics.DrawEllipse(New Pen(Color.Green), _
                                  New Rectangle(lblUndergraduates.Left, _
                                lblUndergraduates.Top + 20, _
                        lblUndergraduates.Width, 20))
    
            e.Graphics.DrawEllipse(New Pen(Color.Fuchsia), _
                                  New Rectangle(lblCertificates.Left, _
                                lblCertificates.Top + 20, _
                        lblUndergraduates.Width, 20))
    End Sub
  7. In the Class Name combo box, select pbxChart
  8. In the Method Name combo box, select Paint and implement its event as follows:
     
    Private Sub pbxChart_Paint(ByVal sender As Object, _
                               ByVal e As System.Windows.Forms.PaintEventArgs) _
                               Handles pbxChart.Paint
            Invalidate()
    End Sub
  9. In the Class Name combo box, select pbxCreateChart
  10. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub btnCreateChart_Click(ByVal sender As Object, _
                                         ByVal e As System.EventArgs) _
                                         Handles btnCreateChart.Click
            Dim Grad As Single = 0.0
            Dim Under As Single = 0.0
            Dim Cert As Single = 0.0
            Dim Total As Single = 0.0
            Dim PercentGraduates As Single
            Dim PercentUndergraduates As Single
            Dim PercentCertificates As Single
    
            Try
                Grad = CSng(txtGraduates.Text)
            Catch ex As FormatException
                MsgBox("Invalid graduate value")
            End Try
    
            Try
                Under = CSng(txtUndergraduates.Text)
            Catch
                MsgBox("Invalid graduate value")
            End Try
    
            Try
                Cert = CSng(txtCertificates.Text)
            Catch
                MsgBox("Invalid graduate value")
            End Try
    
            Total = Grad + Under + Cert
            PercentGraduates = (Grad / Total) * 100
            PercentUndergraduates = (Under / Total) * 100
            PercentCertificates = (Cert / Total) * 100
    
            Graduates = (360 * PercentGraduates) / 100
            Undergraduates = (360 * PercentUndergraduates) / 100
            Certificates = (360 * PercentCertificates) / 100
    
            pbxChart.Invalidate()
    End Sub
  11. Execute the application and test the form
     
     School Enrolment
  12. After using it, close the form

Download

 

Home Copyright © 2008-2016, FunctionX, Inc. Home