|
-
Feb 5th, 2011, 12:47 AM
#1
Thread Starter
Hyperactive Member
Drawing a circle with a center reference
Is there a way to draw a circle by specifying the center of the circle rather than the upper left corner?
This is what I'm using to draw the circle:
vb.ent Code:
Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
Dim pencolor As New Pen(Color.Blue, 3)
Me.CreateGraphics.DrawEllipse(pencolor, 125, 100, 75, 75)
End Sub
The first two numbers 125 and 100 represent the x and y locations of the upper left hand corner of the circle. I'd like the x and y locations to represent the center of the circle. Is there a somewhat simple way to do this?
Any suggestions are appreciated as always!
-
Feb 5th, 2011, 12:57 AM
#2
Re: Drawing a circle with a center reference
No there isn't, but there doesn't need to be. If you know the centre then you know the top, left corner. All you have to do is subtract the radius of the circle from each of the coordinates. You can quite easily write a method that takes the coordinates of the centre and subtracts the radius from the coordinates and then calls DrawEllipse.
Also, you're doing several things wrong with respect to the drawing. First, if you ever do call CreateGraphics you are creating a Graphics object, which you MUST dispose afterwards. That said, you should basically NEVER call CreateGraphics. If you ever want to draw on a control, you must handle its Paint event and do the drawing in the event handler, using the Graphics object provided. For examples, follow the CodeBank link in my signature and check out the threads on drawing.
-
Feb 5th, 2011, 01:05 AM
#3
Thread Starter
Hyperactive Member
Re: Drawing a circle with a center reference
Why must I dispose of the Graphics object?
-
Feb 5th, 2011, 01:11 AM
#4
Re: Drawing a circle with a center reference
Any objects that you create that have a Dispose method, you should dispose. Some are more important than others, and the Graphics class is an important one. It ties up important OS resources that disposing releases.
-
Feb 5th, 2011, 01:24 AM
#5
Thread Starter
Hyperactive Member
Re: Drawing a circle with a center reference
Okay so it's an efficiency thing.
I searched your threads and found a couple on drawing one was Manipulating GDI+ Drawings and the other was Very Simple Drawing Program.
They both seem a bit over my head. I just want to draw a simple circle. Thanks.
-
Feb 5th, 2011, 01:57 AM
#6
Re: Drawing a circle with a center reference
The main point is the Paint event of the control(s) you want to draw on. The only place you should do any drawing on a control is either in its OnPaint method if its your own custom control, or in its Paint event handler. In your case, it's the latter. The steps are simple:
1. Declare one or more member variables in which to store the data that describes the drawing.
2. Handle the Paint event of the control.
3. In the event handler, get the data from the aforementioned member variables and use it to do the drawing, using the Graphics object provided by the event.
4. Whenever you want to change the drawing, update the member variables as required and then call Invalidate and Update or Refresh on the control.
You'll find those steps implemented in both the threads you mentioned.
-
Feb 5th, 2011, 02:18 AM
#7
Thread Starter
Hyperactive Member
Re: Drawing a circle with a center reference
Thanks a lot for the help! I'll give it a try first thing in the morning.
-
Feb 5th, 2011, 11:17 AM
#8
Thread Starter
Hyperactive Member
Re: Drawing a circle with a center reference
Okay I've been at this for a while and all I'm doing is frustrating myself.
Here is what I tried:
Code:
Private Sub form1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs)
Dim g As Graphics = e.Graphics
Dim pn As New Pen(Color.Blue, 100)
g.DrawEllipse(pn, 50, 50, 200, 100)
End Sub
This does absolutely nothing. I want to be able to draw a circle/ellipse when I click on the form. I can do it with the code I originally posted but I want to do it the right way!
-
Feb 5th, 2011, 11:30 AM
#9
Re: Drawing a circle with a center reference
That code does absolutely nothing because it's never being executed. There's no Handles clause on that method so it's not handling any event. In future, I suggest that you use either the Properties window or the drop-down lists at the top of the code window to create event handlers.
-
Feb 5th, 2011, 11:34 AM
#10
Thread Starter
Hyperactive Member
Re: Drawing a circle with a center reference
btw, jmcilhinney, I went through your tut on "Manipulating GDI+ Drawings, which was very good, but I must be too dense to use the methods. However, was able to draw some rectangles!
-
Feb 5th, 2011, 11:40 AM
#11
Thread Starter
Hyperactive Member
Re: Drawing a circle with a center reference
Okay yeah now it draws a filled in ellipse. Now I need to draw it only when I click on the mouse. And I don't really want it filled in.
-
Feb 5th, 2011, 11:47 AM
#12
Re: Drawing a circle with a center reference
Can you explain EXACTLY what your code does?
-
Feb 5th, 2011, 11:55 AM
#13
Thread Starter
Hyperactive Member
Re: Drawing a circle with a center reference
 Originally Posted by jmcilhinney
Can you explain EXACTLY what your code does?
Well I have an idea.
Code:
Private Sub Form1_Paint1(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim g As Graphics = e.Graphics
Dim pn As New Pen(Color.Blue, 1)
g.DrawEllipse(pn, 50, 50, 200, 100)
End Sub
It calls the paint event for Form1 which handles the drawing. e.Graphics gets the graphics to paint. "Dim pn As New Pen" specifies the pen I'm drawing with (color, stroke). And DrawEllipse does the drawing.
Not sure why I have to use e.Graphics though. I tried "Imports System.Drawing.Graphics" at the top of my code and tried to call DrawEllipse without e.Graphics but it didn't work. Must be two different things.
-
Feb 5th, 2011, 12:06 PM
#14
Re: Drawing a circle with a center reference
The reason I asked you to explain EXACTLY what your code was doing was because I wanted to hear you tell why you were passing 100 to the Pen constructor. Your ellipse wasn't filled in. It was just an outline, but you were specifically telling the Pen to draw an outline 100 pixels wide. I see that you've changed that part of the code but you left it out of your explanation. When you are writing code, every little thing means something. When I say EXACTLY I mean EXACTLY. You glossed over a lot of detail in that explanation. You can't afford to do that. You have to be aware of every detail because if you just arbitrarily stick numbers into your code then your code will do unexpected things.
You use e.Graphics because that is the Graphics object created by the object raising the Paint event for anyone handling the event to use to draw on the control. Again, don't just stick arbitrary code in. What did you expect to happen when you imported System.Drawing.Graphics? Importing a name simply lets you use members of that name without qualifying them in code. If you import System.Drawing.Graphics then you can use members of the Graphics class unqualified, which is completely useless. The System.Drawing namespace is already imported, which is useful as it allows you to use just Graphics in code instead of System.Drawing.Graphics every time.
-
Feb 5th, 2011, 12:18 PM
#15
Thread Starter
Hyperactive Member
Re: Drawing a circle with a center reference
Well, I didn't really know what that number 100 did until I started playing with the numbers. Then I realized that it changed the "stroke" of the line. Guess the proper term is Pen constructor. btw that was mentioned in my description. Honestly, I don't know exactly what these things do but I am trying to learn. Thanks for steering me in the right direction.
-
Feb 5th, 2011, 12:24 PM
#16
Thread Starter
Hyperactive Member
Re: Drawing a circle with a center reference
Now how would I go about drawing the circle when the user clicks the mouse rather than when the form loads? I'm not sure how I would use both the Click event and the Paint event. Any suggestions?
-
Feb 5th, 2011, 01:03 PM
#17
Thread Starter
Hyperactive Member
Re: Drawing a circle with a center reference
Okay, I tried the following but it didn't work.
vb.net Code:
Private Sub Form1_Paint1(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
If Control.MouseButtons = Windows.Forms.MouseButtons.Left Then
Dim g As Graphics = e.Graphics
Dim pn As New Pen(Color.Blue, 3)
g.DrawEllipse(pn, 50, 50, 200, 100)
End If
End Sub
I know you said not just plug in arbitrary code but this seemed (to me) like it would work. Guess not.
-
Feb 5th, 2011, 08:00 PM
#18
Re: Drawing a circle with a center reference
Go back and read post #6 again.
-
Feb 5th, 2011, 09:37 PM
#19
Thread Starter
Hyperactive Member
Re: Drawing a circle with a center reference
Could you elaborate? I know you don't want to give anything away and I don't want you to, but I'm having a hard time. I've read post 6, done your tutorial, and gone through your code and I the furthest I've got was to draw a circle when the form loads. Anything I do at this point would be a shot in the dark but I'll keep trying.
Thanks for your time.
-
Feb 5th, 2011, 10:39 PM
#20
Re: Drawing a circle with a center reference
Here's a tip. Store where the mouse clicks next. In your paint event draw using the stored data.
-
Feb 5th, 2011, 10:51 PM
#21
Thread Starter
Hyperactive Member
Re: Drawing a circle with a center reference
Thanks for the tip.
The circle just needs to be drawn in the center so it doesn't matter where the mouse clicks as long as it's on the form. How would I prevent the Paint event from drawing when I open the form? Sorry I know I'm probably asking stupid questions.
-
Feb 5th, 2011, 10:52 PM
#22
Re: Drawing a circle with a center reference
It needs to be drawn directly in the center? If you know the center, then use a boolean to determine if it's the right time to draw.
They're not stupid questions, but we're practically pulling teeth to figure out what you're trying to do.
-
Feb 5th, 2011, 11:06 PM
#23
Thread Starter
Hyperactive Member
Re: Drawing a circle with a center reference
All I want to do, at the moment is draw a circle in the center of the form (using the Paint event) when the user clicks anywhere on the form. After that I want to draw concentric multi-colored circles outside the first one. For now though all I want to do is the first step. Once I figure that out I think I can get the rest. I've already done it all using CreateGraphics but I realize that is not the best way to do things.
Also, thanks to jmcilhinney, I figured out how to draw the circles in the center of the form, so I'm all set there too. Just need to figure out how to draw a circle using the Paint event upon mouse click.
Sorry for the confusion.
Last edited by nicnicman; Feb 5th, 2011 at 11:14 PM.
-
Feb 5th, 2011, 11:12 PM
#24
Re: Drawing a circle with a center reference
How do you usually get code to behave differently under different circumstances? With an IF statement. As I have already said, you need to store the data that represents the drawing in member variables. In your case, that data consists of a Boolean that indicates whether the Button has been clicked or not. Initially that's False, then it gets set to True when the Button is clicked. It's that simple. As I said earlier:
Whenever you want to change the drawing, update the member variables as required and then call Invalidate and Update or Refresh on the control.
-
Feb 5th, 2011, 11:29 PM
#25
Thread Starter
Hyperactive Member
Re: Drawing a circle with a center reference
Okay, thanks. Sorry to be a pest. I think it's just the syntax that's screwing me up. Sometimes I know what I want to do but I don't know how to write it. This being the first time using the Paint event isn't helping either. I'll give it another go though.
Thanks for your patience.
-
Feb 5th, 2011, 11:45 PM
#26
Thread Starter
Hyperactive Member
Re: Drawing a circle with a center reference
Finally, I got it!
vb Code:
Public Class Form1
Dim userClick As Boolean
Private Sub Form1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
userClick = True
Refresh()
End Sub
Private Sub Form1_Paint1(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
If userClick = True Then
Dim g As Graphics = e.Graphics
Dim pn As New Pen(Color.Blue, 3)
g.DrawEllipse(pn, 50, 50, 100, 100)
End If
End Sub
End Class
Thanks for the help!
-
Feb 5th, 2011, 11:54 PM
#27
Thread Starter
Hyperactive Member
Re: Drawing a circle with a center reference
Here's the rest. How can it be improved?
vb Code:
Public Class Form1 Dim userClick As Boolean Private Sub Form1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick userClick = True Me.Refresh() End Sub Private Sub Form1_Paint1(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint If userClick = True Then Dim g As Graphics = e.Graphics Dim diameter As Int32 = 5 Dim random As New Random For count As Int32 = 1 To 50 Dim radius As Int32 = CInt(diameter / 2) Dim xCenter As Int32 = CInt(Me.ClientSize.Width / 2) - radius Dim yCenter As Int32 = CInt(Me.ClientSize.Height / 2) - radius Dim penCurrent As New Pen(Color.FromArgb(random.Next(1, 256), random.Next(1, 256), random.Next(1, 256)), 4) Me.CreateGraphics.DrawEllipse(penCurrent, xCenter, yCenter, diameter, diameter) diameter += 15 System.Threading.Thread.Sleep(45) Next End If End Sub End Class
-
Feb 6th, 2011, 12:48 AM
#28
Thread Starter
Hyperactive Member
Re: Drawing a circle with a center reference
Just noticed in my last post I forgot to replace CreateGraphics with g.
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
|