Graphics are flashing... (GDI+ in picturebox...)

12padams

Well-known member
Joined
Feb 19, 2010
Messages
48
Programming Experience
Beginner
I have tried making the form double buffered and stuff but it still does not work...

Basically its a powder game clone (example here: Powder Game

only problem is the more objects (such as sand) on the screen the more it flickers and with 100+ it becomes unusable (I was hoping to get at least 10,000 on screen at once...)

Heres the bit of code I believe is wrong
VB.NET:
Private Sub drawall()
        PictureBox1.Refresh()
        dealwithsand()
        dealwithmetal()
End Sub
it refreshes the picture box which everything is displayed in every timer tick (20miliseconds)

Here is all the code:

VB.NET:
Public Class Form1
    Dim csand As Boolean
    Dim cmetal As Boolean
    Dim cnothing As Boolean
    Dim sand(300) As Point
    Dim metal(2) As Point
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1.Start()
        metal(0) = New Point(50, 100)
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        drawall()
    End Sub

    Private Sub checkfree(ByVal point As Point)
        If sand.Contains(point) Then csand = True
        If metal.Contains(point) Then cmetal = True

        If csand = False AndAlso cmetal = False Then cnothing = True
    End Sub

    Private Sub clearcontains()
        csand = False
        cmetal = False
        cnothing = False
    End Sub

    Private Sub dealwithsand()
        For i As Integer = 0 To sand.Length - 1
            If sand(i).IsEmpty Then
            Else
                If sand(i).Y < PictureBox1.Height Then
                    Dim below As Point = New Point(sand(i).X, sand(i).Y + 1)
                    checkfree(below)

                    If csand = True Then

                    End If

                    If cmetal = True Then

                    End If

                    If cnothing = True Then
                        sand(i).Y = sand(i).Y + 1
                    End If

                    clearcontains()
                Else
                    sand(i) = New Point(0, 0)
                End If
                End If
        Next
        showsand()
    End Sub

    Private Sub dealwithmetal()
        showmetal()
    End Sub

    Private Sub showsand()
        For i As Integer = 0 To sand.Length - 1
            If sand(i).IsEmpty Then
            Else
                PictureBox1.CreateGraphics.DrawRectangle(Pens.DarkKhaki, sand(i).X, sand(i).Y, 1, 1)
            End If

        Next
    End Sub

    Private Sub showmetal()
        For i As Integer = 0 To metal.Length - 1
            If metal(i).IsEmpty Then
            Else
                PictureBox1.CreateGraphics.DrawRectangle(Pens.Gray, metal(i).X, metal(i).Y, 1, 1)
            End If

        Next
    End Sub

    Private Sub drawall()
        PictureBox1.Refresh()
        dealwithsand()
        dealwithmetal()

    End Sub

    Private Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseDown
        emititem.Start()
    End Sub

    Private Sub PictureBox1_MouseUp(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseUp
        emititem.Stop()
    End Sub

    Private Sub emititem_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles emititem.Tick
        Dim done As Boolean = False
        For i As Integer = 0 To sand.Length - 1
            If done = False Then
                If sand(i).IsEmpty = True Then
                    Dim droppoint As New Point(MousePosition.X - Me.Location.X - 24, MousePosition.Y - Me.Location.Y - 43)
                    checkfree(droppoint)
                    If cnothing = True Then
                        sand(i) = New Point(MousePosition.X - Me.Location.X - 24, MousePosition.Y - Me.Location.Y - 43)
                        done = True
                    Else

                    End If
                End If
            End If
        Next
    End Sub
End Class

Note that the metal is placed at 50, 100 for testing purposes...
 
Well after the two days that has passed I fixed it myself...

I just put all the drawing in an overridden onpaint event and now it works without a single flicker...
 
Back
Top