Follow up to ‘drawing rotated text’

The same programmer who ask for an example of rotated text is back with another interesting request; how to partially fill a circle from the bottom up…

as if it was a glass that you’ve poured water into… so here goes (this is only a snippet of the code, see the original post for the rest);


    Protected Overrides Sub OnPaint( _
            ByVal e As System.Windows.Forms.PaintEventArgs)
        e.Graphics.Clear(Me.BackColor)
        Dim bounds As Rectangle
        Dim g As Graphics
        Dim rotation As Single = 0
        g = e.Graphics
        bounds = New Rectangle(50, 50, _
            Me.Width - 100, Me.Height - 100)
        Dim percentageToFill As Single = 0.75
        Dim fillArea As New Rectangle( _
            50, 50 + ((Me.Height - 100) * (1 - percentageToFill)), _
            Me.Width - 100, ((Me.Height - 100) * percentageToFill))
        Dim oldClip As Region = g.Clip
        g.SetClip(fillArea)
        g.FillEllipse(Brushes.Red, bounds)
        g.Clip = oldClip
        g.DrawEllipse(Pens.Black, bounds)


There is probably more than one way to do this, but my code just fills the whole circle, but sets the clip region first so that it only draws within the bounds of a certain rectangle…

Follow up to ‘drawing rotated text’

6 Responses

  1. Once, I was under the impression that I excelled at VB. Matter of fact, once, I thought I was ‘pretty good’.
    Can you explain to me what Protected Override does and while you’re at it, what are the functions of the Option <whatever besides Explicit>. My world has just been turned upside down.

    Kerowren December 10, 2004 at 11:26 pm #
  2. Just to try to answer Kerowren’s questions about what Protected Overrides does. The Overrides is a keyword in VB.NET to specify that you would be overriding the default behavior of the OnPaint Method with the one you write in your code. Basically, the Parent Class Form that the code above inherits from has it’s own “default” code for OnPaint. Child classes of the class Form can change the way they paint the screen by specifying their own OnPaint code. You specify that you want to “change” the default code of a Parent Class by specifying the keyword Overrides. Here’s an example of inheritance and overriding:

    Class Man
    Public Overridable Function GetName() as String
    Return “Anonymous Man”
    End Function
    End Class

    Class EmmanuelMan : Inherits Man
    Public Overrides Function GetName() as String
    Return “Emmanuel Man”
    End Function
    End Class

    The keyword Protected and so is Public and Private changes the “visibility” and accessiblity of a method. For example Private will make the method accessible to only the class where the method was defined. Public makes it accessible to everything. Protected is somewhere in between.

    Emmanuel Nepomuceno December 18, 2004 at 1:47 pm #
  3. Thanks, that does explain somethings. For one its VB.NET not VB6 :-P.
    I’m not into .NET. It gives me the feeling of programming in a cage.

    Kerowren December 26, 2004 at 2:54 pm #
  4. Oh, come on! .NET is a wonderful world!

    疯牛涕淌 December 31, 2004 at 3:38 pm #
  5. Do you show me how to use listbox in visual basic ?

    Jasreel Pogay January 3, 2005 at 8:47 am #
  6. Hi there,
    I’m really helped with your example, but I want something more! 😉

    I’m working on a project to use a lpt1 port to steer an antenna rotor around, the compass I want to use to display the current location and the new location with an arrow from the center to the degrees.

    So how can I get a line from the center to the southwest for example?

    Just started with VB.net so any help is welcome….

    Cheers!
    Yoeri

    Yoeri June 1, 2005 at 11:34 am #

Leave a Reply