|
-
Feb 2nd, 2002, 11:29 AM
#1
Thread Starter
Lively Member
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!
-
Feb 2nd, 2002, 10:59 PM
#2
Hyperactive Member
-
Feb 3rd, 2002, 03:44 AM
#3
New Member
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.
-
Feb 3rd, 2002, 03:49 PM
#4
New Member
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:
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.
-
Jan 3rd, 2003, 03:29 PM
#5
Frenzied Member
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

-
Jan 3rd, 2003, 03:37 PM
#6
myPen.PenType = PenType.SolidColor
-
Jan 3rd, 2003, 04:29 PM
#7
Frenzied Member
-
Jan 3rd, 2003, 08:28 PM
#8
-
Jan 8th, 2003, 02:24 PM
#9
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|