Re-Drawing Multiple Ellipses

Gluttard

Active member
Joined
Jan 9, 2008
Messages
44
Programming Experience
Beginner
Hello, I'm writing a little program, and right now I need to be able to store the coordinates of an 'x' number of ellipses.
Let's just say that I click on the screen, it draws an ellipse, and redraws the other ellipses before it.

What can I do to make this happen? I want to store the coordinates of each ellipse in an array, but I can't figure out how to do that. Is there something else I can do as well?
 
Alright, I decided to figure out a different way, which worked, but for some reason only two ellipses are visible at once. Weird. I'm not sure where the error is...
PHP:
Public Class Form1
    Dim size As Single = 10
    Dim draw As Boolean = False
    Dim drawprev As Boolean = False
    Dim ballnum As Integer = 0
    Dim aryX(0) As Integer
    Dim aryY(0) As Integer
    Dim arySize(0) As Integer

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Enter Then
            Me.Close()
        End If
        If e.KeyCode = Keys.Space Then
            MsgBox(ballnum)
        End If
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        size = size + 2
        If Cursor.Position.X + size / 2 > Screen.PrimaryScreen.Bounds.Width Then
            System.Windows.Forms.Cursor.Position = New Point(Screen.PrimaryScreen.Bounds.Width - size / 2, Cursor.Position.Y)
        End If
        If Cursor.Position.X - size / 2 < 0 Then
            System.Windows.Forms.Cursor.Position = New Point(size / 2, Cursor.Position.Y)
        End If
        If Cursor.Position.Y + size / 2 > Screen.PrimaryScreen.Bounds.Height Then
            System.Windows.Forms.Cursor.Position = New Point(Cursor.Position.X, Screen.PrimaryScreen.Bounds.Height - size / 2)
        End If
        If Cursor.Position.Y - size / 2 < 0 Then
            System.Windows.Forms.Cursor.Position = New Point(Cursor.Position.X, size / 2)
        End If
        If size = Screen.PrimaryScreen.Bounds.Height Or size = Screen.PrimaryScreen.Bounds.Width Then
            Timer1.Enabled = False
        End If
        Me.Refresh()
    End Sub

    Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
        draw = True
        Timer1.Enabled = True
        size = 10
    End Sub

    Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp

        Timer1.Enabled = False
        Try
            ballnum = ballnum + 1
            drawprev = True
            ReDim aryX(ballnum)
            ReDim aryY(ballnum)
            ReDim arySize(ballnum)
            aryX(ballnum) = Cursor.Position.X - size / 2
            aryY(ballnum) = Cursor.Position.Y - size / 2
            arySize(ballnum) = size

        Catch
            MsgBox("Too many circles made")
        End Try
    End Sub

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        If draw = True Then
            e.Graphics.FillEllipse(Brushes.Green, Cursor.Position.X - size / 2, Cursor.Position.Y - size / 2, size, size)
            If drawprev = True Then
                Dim i As Integer = 0
                For i = 0 To aryX.GetUpperBound(0)
                    e.Graphics.FillEllipse(Brushes.Green, aryX(ballnum), aryY(ballnum), arySize(ballnum), arySize(ballnum))
                Next
            End If
        End If
    End Sub
End Class
 
Look at this:
VB.NET:
                Dim i As Integer = 0
                For i = 0 To aryX.GetUpperBound(0)
                    e.Graphics.FillEllipse(Brushes.Green, aryX(ballnum), aryY(ballnum), arySize(ballnum), arySize(ballnum))
                Next
Your loop counter is named "i". It's the loop counter whose value increments each iteration. Where are you using the loop counter inside the loop?
 
No. The loop counter is already incremented. That's what For loops do. What I'm saying is that you should be using the loop counter inside the loop. You're using 'ballnum' as the index inside the loop. The value of 'ballnum' never changes so you're just going to be drawing the same ellipse over and over.
 
Back
Top