none
How do i insert pictures randomly in this grid? RRS feed

  • Question

  • Public Class Form4
        Public Event Shadow(sender As Object, e As PaintEventArgs)
        Dim Random As New Random
        Dim Bandit(9) As String
        Dim Chest(4) As String
    
        Sub DrawGrid(g As Graphics, origin As Point, rows As Integer, columns As Integer, cellSize As Size)
            Dim gridWidth As Integer = columns * cellSize.Width 'Grid width is the number of columns multiplied with the width of each column
            Dim gridHeight As Integer = rows * cellSize.Height 'Grid height is number of rows times the height of each cell
            Dim left As Integer = origin.X - (gridWidth \ 2) 'LeftMost = Origin.X - (TotalGridWidth \ 2)
            Dim right As Integer = origin.X + (gridWidth \ 2) 'RightMost = Origin.X + (TotalGridWidth \ 2)
            Dim top As Integer = origin.Y - (gridHeight \ 2) ' TopMost = origin.Y - (TotalGridHeight \ 2)
            Dim bottom As Integer = origin.Y + (gridHeight \ 2) 'BottomMost = origin.Y + (TotalGridHeight \ 2)
    
            For y As Integer = top To bottom + 1 Step cellSize.Height 'Declaring the y co-ordinates and where it should be drawn
                Dim pt1 As New Point(left, y) 'Declaring point 1 which will start the drawing from left side of the grid
                Dim pt2 As New Point(right, y) '
                g.DrawLine(Pens.Black, pt1, pt2)
            Next
            For x As Integer = left To right + 1 Step cellSize.Width
                Dim pt1 As New Point(x, top)
                Dim pt2 As New Point(x, bottom)
                g.DrawLine(Pens.Black, pt1, pt2)
            Next
    
            g.DrawEllipse(Pens.Red, New Rectangle(origin.X - 5, origin.Y - 5, 10, 10))
        End Sub
        Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
            Dim origin As New Point(Me.ClientRectangle.Width \ 2, Me.ClientRectangle.Height \ 2)
            Dim cellSize As New Size(45, 45)
            Dim rowCount As Integer = 8
            Dim columnCount As Integer = 8
    
            DrawGrid(e.Graphics, origin, rowCount, columnCount, cellSize)
        End Sub
    End Class


    HIRA RASHEED

    Thursday, March 2, 2017 12:05 PM

All replies

  • Public Class Form2
        Public Event Shadow(sender As Object, e As PaintEventArgs)
        Dim Random As New Random
        Dim Bandit(9) As String
        Dim Chest(4) As String
    
        Sub DrawGrid(g As Graphics, origin As Point, rows As Integer, columns As Integer, cellSize As Size)
            Dim gridWidth As Integer = columns * cellSize.Width 'Grid width is the number of columns multiplied with the width of each column
            Dim gridHeight As Integer = rows * cellSize.Height 'Grid height is number of rows times the height of each cell
            Dim left As Integer = origin.X - (gridWidth \ 2) 'LeftMost = Origin.X - (TotalGridWidth \ 2)
            Dim right As Integer = origin.X + (gridWidth \ 2) 'RightMost = Origin.X + (TotalGridWidth \ 2)
            Dim top As Integer = origin.Y - (gridHeight \ 2) ' TopMost = origin.Y - (TotalGridHeight \ 2)
            Dim bottom As Integer = origin.Y + (gridHeight \ 2) 'BottomMost = origin.Y + (TotalGridHeight \ 2)
    
            For y As Integer = top To bottom + 1 Step cellSize.Height 'Declaring the y co-ordinates and where it should be drawn
                Dim pt1 As New Point(left, y) 'Declaring point 1 which will start the drawing from left side of the grid
                Dim pt2 As New Point(right, y) '
                g.DrawLine(Pens.Black, pt1, pt2)
            Next
            For x As Integer = left To right + 1 Step cellSize.Width
                Dim pt1 As New Point(x, top)
                Dim pt2 As New Point(x, bottom)
                g.DrawLine(Pens.Black, pt1, pt2)
            Next
    
    
            'draw image at random grid
            Dim r As Integer = Random.Next(0, rows)
            Dim c As Integer = Random.Next(0, columns)
            Using bmp As Bitmap = SystemIcons.Question.ToBitmap()
                Dim destrect As New Rectangle(left + (r * cellSize.Width), top + (c * cellSize.Height), cellSize.Width, cellSize.Height)
                Dim srcrect As New Rectangle(0, 0, bmp.Width, bmp.Height)
                g.DrawImage(bmp, destrect, srcrect, GraphicsUnit.Pixel)
            End Using
    
    
            g.DrawEllipse(Pens.Red, New Rectangle(origin.X - 5, origin.Y - 5, 10, 10))
        End Sub
        Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
            Dim origin As New Point(Me.ClientRectangle.Width \ 2, Me.ClientRectangle.Height \ 2)
            Dim cellSize As New Size(45, 45)
            Dim rowCount As Integer = 8
            Dim columnCount As Integer = 8
    
            DrawGrid(e.Graphics, origin, rowCount, columnCount, cellSize)
    
        End Sub
    End Class


    Thursday, March 2, 2017 12:36 PM