Results 1 to 22 of 22

Thread: [2005] Graphics problem

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2007
    Posts
    9

    [2005] Graphics problem

    Hi,

    I'm trying to draw a simple ellipse on the form after pressing a button. It works when I have the ellipse in the upper left hand corner of the screen, but doesn't show up anywhere else. I've noticed that the location it does show up in was the original size of the form before I expanded it. Does anyone know how to fix this?

    Thanks

  2. #2
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] Graphics problem

    Are you drawing it on your form? If yew then do it in the Form's Paint event.
    vb Code:
    1. Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
    2.         e.Graphics.DrawEllipse(Pens.Red, New Rectangle(5, 5, 40, 40))
    3.     End Sub

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2007
    Posts
    9

    Re: [2005] Graphics problem

    Oh, thanks a bunch. Is there a way to call that from a button? It starts animating immediately after the program starts, can I make it start after pressing a button? Also, it seems to loop infinitely

  4. #4
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] Graphics problem

    Almost all the drawings should be done in the controls’ Paint event, because when you minimize of maximize the form it is being redrawn so the drawing you have is being erased. But if you draw it in the form’s Paint event it is being erased and redrawn.

    Yes you can do it from the button click. You just need to have a Boolean flag to prevent or allow the drawing. Here is an example:
    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim draw As Boolean
    4.     Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
    5.         If Me.draw Then
    6.             e.Graphics.DrawEllipse(Pens.Red, New Rectangle(5, 5, 40, 40))
    7.         End If
    8.     End Sub
    9.  
    10.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    11.         Me.draw = True
    12.         Me.Refresh()
    13.     End Sub
    14. End Class

  5. #5

    Thread Starter
    New Member
    Join Date
    Jun 2007
    Posts
    9

    Re: [2005] Graphics problem

    Yay! Thanks a lot, I finally got this program working

  6. #6

    Thread Starter
    New Member
    Join Date
    Jun 2007
    Posts
    9

    Re: [2005] Graphics problem

    Hmm, actually, when I replace the DrawEllipse command with a function that draws multiple ellipses, the problem comes back again

  7. #7
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] Graphics problem

    What problem? Show the code!

  8. #8

    Thread Starter
    New Member
    Join Date
    Jun 2007
    Posts
    9

    Re: [2005] Graphics problem

    Heres the code; you'll see the circles get cut off as they move around

    Code:
    Public Class Form1
        Dim amt, a, b, c, d, time, wait As Integer
        Dim acctemp, dt As Double
        Dim p(0 To 4, 0 To 1, 0 To 1), v(0 To 4, 0 To 1), m(0 To 4), rad(0 To 4) As Double
        Dim Crash As Boolean
        Dim gr As Graphics = CreateGraphics()
        Const G = 6.67 * 10 ^ -11
        Dim Draw As Boolean
    
    
        Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
            If Me.Draw Then
                CreatePlanets(100, 100, 200, 200, 300, 300, 0, 0, 0, 0, -0.2, 0.2, 0.3, -0.2, 0, 0, 0, 0, 0, 0, 100000000000, 100000000000, 1000000000, 0, 0, 10, 20, 30, 0, 0)
                Me.Draw = False
            End If
        End Sub
    
        Private Sub CreatePlanets(ByVal p00, ByVal p01, ByVal p10, ByVal p11, ByVal p20, ByVal p21, ByVal p30, ByVal p31, ByVal p40, ByVal p41, ByVal v00, ByVal v01, ByVal v10, ByVal v11, ByVal v20, ByVal v21, ByVal v30, ByVal v31, ByVal v40, ByVal v41, ByVal m0, ByVal m1, ByVal m2, ByVal m3, ByVal m4, ByVal r0, ByVal r1, ByVal r2, ByVal r3, ByVal r4)
            amt = 3
            dt = 3
            Crash = False
            p(0, 0, 0) = p00
            p(0, 1, 0) = p01
            p(1, 0, 0) = p10
            p(1, 1, 0) = p11
            p(2, 0, 0) = p20
            p(2, 1, 0) = p21
            p(3, 0, 0) = p30
            p(3, 1, 0) = p31
            p(4, 0, 0) = p40
            p(4, 1, 0) = p41
            v(0, 0) = v00
            v(0, 1) = v01
            v(1, 0) = v10
            v(1, 1) = v11
            v(2, 0) = v20
            v(2, 1) = v21
            v(3, 0) = v30
            v(3, 1) = v31
            v(4, 0) = v40
            v(4, 1) = v41
            m(0) = m0
            m(1) = m1
            m(2) = m2
            m(3) = m3
            m(4) = m4
            rad(0) = r0
            rad(1) = r1
            rad(2) = r2
            rad(3) = r3
            rad(4) = r4
    
            For time = 0 To 1000
                For a = 0 To amt - 1
                    For b = 0 To 1
                        For c = 0 To amt - 2
                            acctemp = acctemp + G * (m((a + c + 1) Mod amt) * (p((a + c + 1) Mod amt, b, 0) - p(a, b, 0))) / ((p(a, 0, 0) - p((a + c + 1) Mod amt, 0, 0)) ^ 2 + (p(a, 1, 0) - p((a + c + 1) Mod amt, 1, 0)) ^ 2) ^ 1.5
                            If (((p(a, 0, 0) - p((a + c + 1) Mod amt, 0, 0)) ^ 2 + (p(a, 1, 0) - p((a + c + 1) Mod amt, 1, 0)) ^ 2) ^ 0.5 < (rad(a) + rad((a + c + 1) Mod amt))) Then
                                Crash = True
                            End If
                            gr.FillEllipse(Brushes.Black, CInt(p(a, 0, 0)), CInt(p(a, 1, 0)), CInt(rad(a)) * 2, CInt(rad(a) * 2))
                            gr.FillEllipse(Brushes.Black, CInt(p((a + c + 1) Mod amt, 0, 0)), CInt(p((a + c + 1) Mod amt, 1, 0)), CInt(rad((a + c + 1) Mod amt)) * 2, CInt(rad((a + c + 1) Mod amt)) * 2)
                            If Crash = True Then
                                p(a, 0, 1) = (p(a, 0, 1) * m(a) + p((a + c + 1) Mod amt, 0, 1) * m((a + c + 1) Mod amt)) / (m(a) + m((a + c + 1) Mod amt))
                                p(a, 1, 1) = (p(a, 1, 1) * m(a) + p((a + c + 1) Mod amt, 1, 1) * m((a + c + 1) Mod amt)) / (m(a) + m((a + c + 1) Mod amt))
                                v(a, 0) = (v(a, 0) * m(a) + v((a + c + 1) Mod amt, 0) * m((a + c + 1) Mod amt)) / (m(a) + m((a + c + 1) Mod amt))
                                v(a, 1) = (v(a, 1) * m(a) + v((a + c + 1) Mod amt, 1) * m((a + c + 1) Mod amt)) / (m(a) + m((a + c + 1) Mod amt))
                                m(a) = m(a) + m((a + c + 1) Mod amt)
                                m((a + c + 1) Mod amt) = 0
                                rad(a) = (rad(a) ^ 3 + rad((a + c + 1) Mod amt) ^ 3) ^ (1 / 3)
                                rad((a + c + 1) Mod amt) = 0
                            End If
                            Crash = False
                        Next
                        p(a, b, 1) = p(a, b, 0) + v(a, b) * dt + 0.5 * acctemp * dt ^ 2
                        v(a, b) = v(a, b) + acctemp * dt
                        acctemp = 0
                    Next
                Next
                For d = 0 To amt - 1
                    p(d, 0, 0) = p(d, 0, 1)
                    p(d, 1, 0) = p(d, 1, 1)
                    For wait = 0 To 100
                        gr.FillEllipse(Brushes.Red, CInt(p(d, 0, 0)), CInt(p(d, 1, 0)), CInt(rad(d)) * 2, CInt(rad(d)) * 2)
                    Next
                Next
            Next
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Me.Draw = True
            Me.Refresh()
        End Sub
    End Class

  9. #9
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] Graphics problem

    But you set The "draw" to False wich means if you maximize or minimize the window it is not going to redraw. Take that line away.
    vb Code:
    1. Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
    2.         If Me.Draw Then
    3.             CreatePlanets(100, 100, 200, 200, 300, 300, 0, 0, 0, 0, -0.2, 0.2, 0.3, -0.2, 0, 0, 0, 0, 0, 0, 100000000000, 100000000000, 1000000000, 0, 0, 10, 20, 30, 0, 0)
    4.             Me.Draw = False '<= Take away this line!!!
    5.         End If
    6.     End Sub

  10. #10

    Thread Starter
    New Member
    Join Date
    Jun 2007
    Posts
    9

    Re: [2005] Graphics problem

    Ok, but the circles still get cut off. Let me start over. This is all I'm trying to do:

    Code:
    Public Class Form1
        Dim gr As Graphics = CreateGraphics()
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Mult()
        End Sub
        Private Sub Mult()
            gr.DrawEllipse(Pens.Red, New Rectangle(300, 400, 40, 40))
            gr.DrawEllipse(Pens.Red, New Rectangle(400, 400, 40, 40))
        End Sub
    End Class
    Click a button, it calls a function, which draws a couple circles on the form. When I do this, the circles don't show up on the entire screen - they only show in the upper left

  11. #11
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] Graphics problem

    I don't know what you mean by cuts off. The circles draw where you specify. Here is the code where i draw multiple circles as you can see them in the image:
    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim draw As Boolean
    4.  
    5.     Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
    6.         If Me.draw Then
    7.             e.Graphics.DrawEllipse(Pens.Red, New Rectangle(5, 5, 40, 40))
    8.             e.Graphics.DrawEllipse(Pens.Red, New Rectangle(10, 10, 40, 40))
    9.             e.Graphics.DrawEllipse(Pens.Red, New Rectangle(15, 15, 40, 40))
    10.             e.Graphics.DrawEllipse(Pens.Red, New Rectangle(20, 20, 40, 40))
    11.             e.Graphics.DrawEllipse(Pens.Red, New Rectangle(25, 25, 40, 40))
    12.         End If
    13.     End Sub
    14.  
    15.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    16.         Me.draw = True
    17.         Me.Refresh()
    18.     End Sub
    19. End Class
    Attached Images Attached Images  

  12. #12

    Thread Starter
    New Member
    Join Date
    Jun 2007
    Posts
    9

    Re: [2005] Graphics problem

    Thats weird, when I use this code:
    Code:
    Public Class Form1
        Dim gr As Graphics = CreateGraphics()
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Mult()
        End Sub
        Private Sub Mult()
            gr.DrawEllipse(Pens.Red, New Rectangle(200, 250, 40, 40))
            gr.DrawEllipse(Pens.Red, New Rectangle(275, 200, 40, 40))
        End Sub
    End Class
    I get this:
    Attached Images Attached Images  

  13. #13
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    Re: [2005] Graphics problem

    You really shouldn't be using CreateGraphics. Use the Graphics object supplied in the Paint event of the form as VBDT is doing.

    Also, add Me.Refresh inside the form's resize event to redraw the form when you, uh, resize it.
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


  14. #14
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] Graphics problem

    Let me guess what is hapening here. You use that code and draw the circles and then you maximize the form with the mouse so that you see the half circles! That is why i sayd that you need to draw the circles in the paint event and use the paint event's e.Graphics object to draw. It simms you didn't pay attention to that. Try this:
    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim draw As Boolean
    4.  
    5.     Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
    6.         If Me.draw Then
    7.             e.Graphics.DrawEllipse(Pens.Red, New Rectangle(200, 250, 40, 40))
    8.             e.Graphics.DrawEllipse(Pens.Red, New Rectangle(275, 200, 40, 40))
    9.         End If
    10.     End Sub
    11.  
    12.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    13.         Me.draw = True
    14.         Me.Refresh()
    15.     End Sub
    16.  
    17. End Class

  15. #15

    Thread Starter
    New Member
    Join Date
    Jun 2007
    Posts
    9

    Re: [2005] Graphics problem

    I expanded the form in design, not while running the program

  16. #16
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] Graphics problem

    Quote Originally Posted by Andy_P
    You really shouldn't be using CreateGraphics. Use the Graphics object supplied in the Paint event of the form as VBDT is doing.

    Also, add Me.Refresh inside the form's resize event to redraw the form when you, uh, resize it.
    Actualy there is no need to refresh it in the form's resize event, it does it aoutomaticly.

  17. #17
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    Re: [2005] Graphics problem

    Quote Originally Posted by VBDT
    Actualy there is no need to refresh it in the form's resize event, it does it aoutomaticly.
    Is that so? Is that new in 2005? I only have 2003 here, so I stand corrected.
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


  18. #18
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] Graphics problem

    Quote Originally Posted by cmart3
    I expanded the form in design, not while running the program
    if you use that code wich i don't recomend then you need to have the code like this:
    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim gr As Graphics
    4.  
    5.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    6.         Me.Mult()
    7.     End Sub
    8.  
    9.     Private Sub Mult()
    10.         gr = Me.CreateGraphics()
    11.         gr.DrawEllipse(Pens.Red, New Rectangle(200, 250, 40, 40))
    12.         gr.DrawEllipse(Pens.Red, New Rectangle(275, 200, 40, 40))
    13.         gr.Dispose()
    14.     End Sub
    15.  
    16. End Class
    But then try to miximize and minimize the form, you will see what i mean.

  19. #19
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] Graphics problem

    Quote Originally Posted by Andy_P
    Is that so? Is that new in 2005? I only have 2003 here, so I stand corrected.
    I use vb 2005 and that is the case. It might be not in VB 2003.

  20. #20

    Thread Starter
    New Member
    Join Date
    Jun 2007
    Posts
    9

    Re: [2005] Graphics problem

    Thats exactly what I was looking for, thanks for your time. If you're wondering, the program is supposed to mimic planetary motion - orbits, gravitational catapults, all that good stuff. Thanks again

  21. #21
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    Re: [2005] Graphics problem

    cmart3 : Good luck with your app


    VBDT : Just to round things up, VB2003 does not redraw on resize by default. I need to set ResizeRedraw to True, or do a refresh in the resize event.
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


  22. #22
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] Graphics problem

    Quote Originally Posted by Andy_P
    cmart3 : Good luck with your app


    VBDT : Just to round things up, VB2003 does not redraw on resize by default. I need to set ResizeRedraw to True, or do a refresh in the resize event.
    That is what I thought.

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