Re: [2005] Graphics problem
Are you drawing it on your form? If yew then do it in the Form's Paint event.
vb Code:
Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
e.Graphics.DrawEllipse(Pens.Red, New Rectangle(5, 5, 40, 40))
End Sub
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
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:
Public Class Form1
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
e.Graphics.DrawEllipse(Pens.Red, New Rectangle(5, 5, 40, 40))
End If
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
Re: [2005] Graphics problem
Yay! Thanks a lot, I finally got this program working
Re: [2005] Graphics problem
Hmm, actually, when I replace the DrawEllipse command with a function that draws multiple ellipses, the problem comes back again
Re: [2005] Graphics problem
What problem? Show the code!
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
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:
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 '<= Take away this line!!!
End If
End Sub
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
1 Attachment(s)
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:
Public Class Form1
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
e.Graphics.DrawEllipse(Pens.Red, New Rectangle(5, 5, 40, 40))
e.Graphics.DrawEllipse(Pens.Red, New Rectangle(10, 10, 40, 40))
e.Graphics.DrawEllipse(Pens.Red, New Rectangle(15, 15, 40, 40))
e.Graphics.DrawEllipse(Pens.Red, New Rectangle(20, 20, 40, 40))
e.Graphics.DrawEllipse(Pens.Red, New Rectangle(25, 25, 40, 40))
End If
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
1 Attachment(s)
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:
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.
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:
Public Class Form1
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
e.Graphics.DrawEllipse(Pens.Red, New Rectangle(200, 250, 40, 40))
e.Graphics.DrawEllipse(Pens.Red, New Rectangle(275, 200, 40, 40))
End If
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
Re: [2005] Graphics problem
I expanded the form in design, not while running the program
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.
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. :)
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:
Public Class Form1
Dim gr As Graphics
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Mult()
End Sub
Private Sub Mult()
gr = Me.CreateGraphics()
gr.DrawEllipse(Pens.Red, New Rectangle(200, 250, 40, 40))
gr.DrawEllipse(Pens.Red, New Rectangle(275, 200, 40, 40))
gr.Dispose()
End Sub
End Class
But then try to miximize and minimize the form, you will see what i mean.
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.
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
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.
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.