PDA

Click to See Complete Forum and Search --> : Circles and Lines


Gherkin
Feb 2nd, 2002, 10:29 AM
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!

thinktank2
Feb 2nd, 2002, 09:59 PM
http://www.angelfire.com/tx4/cus/shapes/csharp.html

enigaman
Feb 3rd, 2002, 02:44 AM
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.

AdiF
Feb 3rd, 2002, 02:49 PM
Okay. Let's make a new Form, a Button, and a PictureBox.

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

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:

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:

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

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:

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:

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:

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 :D

MrGTI
Jan 3rd, 2003, 02:29 PM
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.

Cander
Jan 3rd, 2003, 02:37 PM
myPen.PenType = PenType.SolidColor

MrGTI
Jan 3rd, 2003, 03:29 PM
Name 'PenType' is not declared :(

Am i missing an import statement? :confused:

MrGTI
Jan 3rd, 2003, 07:28 PM
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.

MrGTI
Jan 8th, 2003, 01:24 PM
I found the answer myself. It's .FillRectangle. :)