Results 1 to 28 of 28

Thread: Drawing a circle with a center reference

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2010
    Posts
    264

    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:
    1. Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
    2.         Dim pencolor As New Pen(Color.Blue, 3)
    3.  
    4.         Me.CreateGraphics.DrawEllipse(pencolor, 125, 100, 75, 75)
    5.  
    6.     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!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2010
    Posts
    264

    Re: Drawing a circle with a center reference

    Why must I dispose of the Graphics object?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2010
    Posts
    264

    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.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2010
    Posts
    264

    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.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2010
    Posts
    264

    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!

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2010
    Posts
    264

    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!

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2010
    Posts
    264

    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.

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Drawing a circle with a center reference

    Can you explain EXACTLY what your code does?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2010
    Posts
    264

    Re: Drawing a circle with a center reference

    Quote Originally Posted by jmcilhinney View Post
    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.

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2010
    Posts
    264

    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.

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2010
    Posts
    264

    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?

  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2010
    Posts
    264

    Re: Drawing a circle with a center reference

    Okay, I tried the following but it didn't work.

    vb.net Code:
    1. Private Sub Form1_Paint1(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    2.         If Control.MouseButtons = Windows.Forms.MouseButtons.Left Then
    3.             Dim g As Graphics = e.Graphics
    4.             Dim pn As New Pen(Color.Blue, 3)
    5.             g.DrawEllipse(pn, 50, 50, 200, 100)
    6.         End If
    7.     End Sub

    I know you said not just plug in arbitrary code but this seemed (to me) like it would work. Guess not.

  18. #18
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Drawing a circle with a center reference

    Go back and read post #6 again.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  19. #19

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2010
    Posts
    264

    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.

  20. #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.

  21. #21

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2010
    Posts
    264

    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.

  22. #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.

  23. #23

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2010
    Posts
    264

    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.

  24. #24
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  25. #25

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2010
    Posts
    264

    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.

  26. #26

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2010
    Posts
    264

    Re: Drawing a circle with a center reference

    Finally, I got it!

    vb Code:
    1. Public Class Form1
    2.     Dim userClick As Boolean
    3.  
    4.     Private Sub Form1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
    5.         userClick = True
    6.         Refresh()
    7.     End Sub
    8.  
    9.     Private Sub Form1_Paint1(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    10.         If userClick = True Then
    11.             Dim g As Graphics = e.Graphics
    12.             Dim pn As New Pen(Color.Blue, 3)
    13.             g.DrawEllipse(pn, 50, 50, 100, 100)
    14.         End If
    15.     End Sub
    16.  
    17. End Class

    Thanks for the help!

  27. #27

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2010
    Posts
    264

    Re: Drawing a circle with a center reference

    Here's the rest. How can it be improved?

    vb Code:
    1. Public Class Form1
    2.     Dim userClick As Boolean
    3.  
    4.     Private Sub Form1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
    5.         userClick = True
    6.         Me.Refresh()
    7.     End Sub
    8.  
    9.     Private Sub Form1_Paint1(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    10.  
    11.         If userClick = True Then
    12.             Dim g As Graphics = e.Graphics
    13.             Dim diameter As Int32 = 5
    14.             Dim random As New Random
    15.  
    16.             For count As Int32 = 1 To 50
    17.                 Dim radius As Int32 = CInt(diameter / 2)
    18.                 Dim xCenter As Int32 = CInt(Me.ClientSize.Width / 2) - radius
    19.                 Dim yCenter As Int32 = CInt(Me.ClientSize.Height / 2) - radius
    20.                 Dim penCurrent As New Pen(Color.FromArgb(random.Next(1, 256), random.Next(1, 256), random.Next(1, 256)), 4)
    21.  
    22.                 Me.CreateGraphics.DrawEllipse(penCurrent, xCenter, yCenter, diameter, diameter)
    23.                 diameter += 15
    24.  
    25.                 System.Threading.Thread.Sleep(45)
    26.             Next
    27.         End If
    28.     End Sub
    29.  
    30. End Class

  28. #28

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2010
    Posts
    264

    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
  •  



Click Here to Expand Forum to Full Width