Results 1 to 9 of 9

Thread: Circles and Lines

  1. #1

    Thread Starter
    Lively Member Gherkin's Avatar
    Join Date
    Aug 2001
    Location
    Gherkin Land
    Posts
    65

    Circles and Lines

    Hi!

    I was looking on MSDN and I saw that there are no circle and line functions.

    How do you make circles and lines in VB.NET?

    Thanks!

  2. #2
    Hyperactive Member thinktank2's Avatar
    Join Date
    Nov 2001
    Location
    Arctic
    Posts
    272

  3. #3
    New Member
    Join Date
    Feb 2002
    Location
    Phoenix
    Posts
    1
    I would imagine that it would be something like this:

    Dim graphicscontext As Graphics
    graphicscontext = MyBase.CreateGraphics
    graphicscontext.DrawRectangle(New Pen(Color.Red, 3), 0, 0, 200, 100)

    This code doesn't quite work, and I have a feeling that it's in the way that VB is attempting to carry out inheritance, or in my faulty understanding of the way that VB is attempting to carry out inheritance.

    The MyBase keyword accesses methods in the classes parent. CreateGraphics is a part of the Form Class, and for some reason is not accessable when your form class is instanced. I had a similar problem when modifying the title bar of a form during execution. When the IDE tells you "This reference to a non-shared member requires an object reference, and none is supplied." using the mybase keyword seems to make things better.

    The MSDN documetation isn't exactly clear, and there are no samples, and there are differences between the class view for forms and what is seen in the IDE as availible functions for the forms parents.

    Also, there is a C# version of scribble with the VS.NET install, and if you are feeling extra curious you could go and look at it.
    Ceo
    Wicked Plots Inc.

  4. #4
    New Member
    Join Date
    Feb 2002
    Posts
    3
    Okay. Let's make a new Form, a Button, and a PictureBox.

    In our Button's code, let's add the following:

    Code:
    Dim g As Graphics = PictureBox1.CreateGraphics
    We do that cause first we have to obtain something called an "object reference". So we "refer" to the PictureBox object. So now we have to give the computer a pen to draw with:

    Code:
    Dim myPen As New Pen(Color.Red)
    So that's just a default pen, (no fancy dotted-line brushes or anything), and we set the colour to red. After "Color.red" you can just add a comma and type in the value of the width, OR you can just add the following line of code:

    Code:
    myPen.Width = 2
    So now our pen has the width of 2 pixels. Let's finally draw the stupid circle:

    Code:
    g.DrawEllipse(myPen, New Rectangle(4, 4, 50, 50))
    Okay, so referring to our graphics object reference (which is the "g"), we tell it to draw an Elliptical shape (oval or circle). We tell the computer to draw it using the pen we gave it (red and 2 pixels thick), given the co-ordinates of an invisible rectangular region starting (4, 4) and going diagonally to the opposite corner: (50, 50). These units are all measured in pixels.

    So you should end up with the following in your button's code:

    Code:
    Dim g As Graphics = PictureBox1.CreateGraphics
    Dim myPen As New Pen(Color.Red)
    myPen.Width = 2
    g.DrawEllipse(myPen, New Rectangle(4, 4, 50, 50))
    Run it, click the button! OMG IT'S A CIRCLE! hehehe. YAAAY!
    Okay, so you want a Line instead/as well...

    Just add or replace the last line we have with:

    Code:
    g.DrawLine(myPen, 4, 4, 50, 50)
    And that will draw a 2px-thick red line from (4, 4) to (50, 50).
    We can also do:

    Code:
    g.DrawBezier(myPen, 15, 15, 30, 30, 45, 30, 87, 20)
    g.DrawPolygon(myPen, New PointF() {New PointF(1, 1), _
       New PointF (20, 10), New PointF(5, 4), New PointF(100, 2), _
       New PointF(200, 50), New PointF(39, 45)})
    Reply if you're still having probs
    Last edited by AdiF; Feb 3rd, 2002 at 03:52 PM.

  5. #5
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Thumbs up

    Great stuff. Thanks AdiF.

    How do you fill the circle or rectangle with colour? I can't find any parameter that allows you to make the object solid.
    ~Peter


  6. #6
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    myPen.PenType = PenType.SolidColor
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  7. #7
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Unhappy

    Name 'PenType' is not declared

    Am i missing an import statement?
    ~Peter


  8. #8
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Thumbs down

    I added Imports System.Drawing.Drawing2D and it says Property 'PenType' is 'ReadOnly'.

    A Google search on PenType is not helpful, and i searched a bunch of other .NET sites with no luck.

    I think maybe that .PenType is not the answer.
    ~Peter


  9. #9
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Thumbs up

    I found the answer myself. It's .FillRectangle.
    ~Peter


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width