Public Class MainForm
    Private fBallAdmin As BallAdmin

    Private Sub exitMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitMenuItem.Click
        Close()
    End Sub

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        fBallAdmin = New BallAdmin(gamePanel.Width, gamePanel.Height)
        updateView()
    End Sub
    Private Sub updateView()
        gameTimer.Enabled = fBallAdmin.Running
        gameTimer.Interval = fBallAdmin.TimeInterval
        gamePanel.Refresh() 'Note that only the panel needs to be repainted.
    End Sub

    Private Sub gamePanel_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles gamePanel.Paint
        'The current state of the game is displayed on the panel's Graphics object.
        'Two alternatives are given for displaying the game.
        Dim g As Graphics
        g = gamePanel.CreateGraphics()
        fBallAdmin.draw(g) 'first alternative
        'g.DrawImage(fBallAdmin.GameImage, 0, 0) 'second alternative
        g.Dispose()
    End Sub

    Private Sub gameTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gameTimer.Tick
        'The ball is moved and a check is made for collisions with the walls.
        fBallAdmin.nextMove()
        updateView()
    End Sub

    Private Sub upButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles upButton.Click
        'Direction of travel is set to upwards, increasing the game's speed.
        fBallAdmin.setDirection("u"c)
        updateView()
    End Sub

    Private Sub leftButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles leftButton.Click
        'Direction of travel is set to left, increasing the game's speed.
        fBallAdmin.setDirection("l"c)
        updateView()
    End Sub

    Private Sub rightButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rightButton.Click
        'Direction of travel is set to right, increasing the game's speed.
        fBallAdmin.setDirection("r"c)
        updateView()
    End Sub

    Private Sub downButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles downButton.Click
        'Direction of travel is set to down, increasing the game's speed.
        fBallAdmin.setDirection("d"c)
        updateView()
    End Sub


    Private Sub MainForm_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        'Direction of travel is set according to key pressed, increasing the game's speed.
        Select Case e.KeyCode
            Case Keys.Up : fBallAdmin.setDirection("u"c)
            Case Keys.Down : fBallAdmin.setDirection("d"c)
            Case Keys.Left : fBallAdmin.setDirection("l"c)
            Case Keys.Right : fBallAdmin.setDirection("r"c)
        End Select
        e.Handled = True
        updateView()
    End Sub

    Private Sub upButton_PreviewKeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles upButton.PreviewKeyDown, rightButton.PreviewKeyDown, leftButton.PreviewKeyDown, downButton.PreviewKeyDown
        Select Case e.KeyCode
            Case Keys.Down, Keys.Left, Keys.Right, Keys.Up
                e.IsInputKey = True
        End Select
    End Sub

    Private Sub MainForm_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Resize
        'The game area is changed; if this happens during a game, the game is reset.
        If fBallAdmin IsNot Nothing Then
            fBallAdmin.dispose()
        End If
        fBallAdmin = New BallAdmin(gamePanel.Width, gamePanel.Height)
        updateView()
    End Sub
End Class
