Results 1 to 5 of 5

Thread: Draw Rectangle (random times, random location)

  1. #1

    Thread Starter
    Member
    Join Date
    May 2011
    Posts
    48

    Red face Draw Rectangle (random times, random location)

    I am practicing using the drawing commands, and have gotten a grass background and black happy face that moves around the background.
    It's a 10x10 grid of 50 pixels.

    I want (at the moment, when i press enter) it to generate a random number of trees (15 ~ 25) at random locations on the background. The program doesn't freak out about anything, and the variables seem to be right (using stop commands) but it's not drawing anything. Here is the code for the "GenerateTrees" command I have for when I press enter.

    Code:
        Private Sub GenerateTrees()
    
            NumberOfTrees = Int(Rnd() * 10) + 15
            For i = 0 To NumberOfTrees - 1
                bmap.MakeTransparent(Color.Fuchsia)
                TreeX = Int(Rnd() * 10) * 50
                TreeY = Int(Rnd() * 10) * 50
                sRect = New Rectangle(0, 50, 50, 50)
                dRect = New Rectangle(TreeX, TreeY, 50, 50)
                G.DrawImage(bmap, TreeX, TreeY, sRect, GraphicsUnit.Pixel)
            Next

  2. #2

    Thread Starter
    Member
    Join Date
    May 2011
    Posts
    48

    Re: Draw Rectangle (random times, random location)

    Here is my whole code, as I just thought of something. It MIGHT be drawing the trees under the grass.

    Code:
    Imports System.Drawing
    
    Public Class Form1
    
        Dim G As Graphics
        Dim BBG As Graphics
        Dim sRect As Rectangle
        Dim dRect As Rectangle
        Dim bmap As Bitmap
        Dim BB As Bitmap
        Dim NumberOfTrees As Integer
        Dim TreeX As Integer
        Dim TreeY As Integer
        Dim GuyY As Integer = 1
        Dim GuyX As Integer = 1
    
        Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    
            Select Case e.KeyCode
                Case Keys.W
                    GuyY = GuyY - 50
                Case Keys.A
                    GuyX = GuyX - 50
                Case Keys.S
                    GuyY = GuyY + 50
                Case Keys.D
                    GuyX = GuyX + 50
                Case Keys.Space
                    ChopTree()
                Case Keys.Enter
                    GenerateTrees()
            End Select
    
            DrawAll()
    
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            bmap = New Bitmap(PictureBox1.Image)
            G = Me.CreateGraphics
            BB = New Bitmap(Me.Width, Me.Height)
    
            DrawAll()
        End Sub
    
        Private Sub ChopTree()
    
            MsgBox("Attempting to chop a tree")
    
        End Sub
    
        Private Sub GenerateTrees()
    
            NumberOfTrees = Int(Rnd() * 10) + 15
            For i = 0 To NumberOfTrees - 1
                bmap.MakeTransparent(Color.Fuchsia)
                TreeX = Int(Rnd() * 10) * 50
                TreeY = Int(Rnd() * 10) * 50
                sRect = New Rectangle(0, 50, 50, 50)
                dRect = New Rectangle(TreeX, TreeY, 50, 50)
                G.DrawImage(bmap, TreeX, TreeY, sRect, GraphicsUnit.Pixel)
            Next
            Label1.Text = NumberOfTrees
    
        End Sub
        Private Sub DrawAll()
    
            Dim X, Y As Integer
    
            For X = 0 To 9
                For Y = 0 To 9
                    sRect = New Rectangle(0, 0, 50, 50)
                    dRect = New Rectangle(X * 50, Y * 50, 50, 50)
                    G.DrawImage(bmap, dRect, sRect, GraphicsUnit.Pixel)
                Next
            Next
    
            bmap.MakeTransparent(Color.Fuchsia)
            sRect = New Rectangle(50, 50, 50, 50)
            G.DrawImage(bmap, GuyX, GuyY, sRect, GraphicsUnit.Pixel)
    
            G = (Graphics.FromImage(BB))
            BBG = Me.CreateGraphics
            BBG.DrawImage(BB, 0, 0, Me.Width, Me.Height)
    
        End Sub
    
        Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    
            DrawAll()
    
        End Sub
    End Class

  3. #3
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Draw Rectangle (random times, random location)

    lol you have to use the graphics object from the Paint event to draw. You do not create a graphics object to draw on. How would you expect the Runtime to know that you want to draw on the form ?

    Here is an example of how to draw a rectangle on a form:-
    vb Code:
    1. Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    2.  
    3.         Dim rect As New Rectangle(20, 20, 150, 150)
    4.  
    5.         e.Graphics.DrawRectangle(Pens.Black, rect)
    6.  
    7.     End Sub

  4. #4
    Hyperactive Member stepdragon's Avatar
    Join Date
    Aug 2011
    Location
    Cincinnati
    Posts
    288

    Re: Draw Rectangle (random times, random location)

    As Niya mentioned, you need to use a graphics object which pertains to your form for drawing... You can easily do this by passing the graphics object to each of your subroutines and drawing on that.

    I would also suggest simplifying your drawing to one routine. For example, if you draw your background every _Paint event, but your trees only when the user hits a button, your trees will be drawn over the moment the form refreshes.

    It would be better to have variables for whether trees should be visible, and where, and then setting the variables, and calling me.Refresh(). Then, your form will redraw, your sub will be called, and it will draw trees instead of just a background.

    Make sense?

    If you're wrong, you'll learn. If I'm wrong, I'll learn. Try something new and go from there. That's how we improve.

    CodeBank: VB.Net - Simple Proper Image Scaling in Correct Aspect Ratio - Star Rating Control
    Useful Links: HOW TO USE CODE TAGS

  5. #5

    Thread Starter
    Member
    Join Date
    May 2011
    Posts
    48

    Re: Draw Rectangle (random times, random location)

    So I when it generates the trees and their locations, i need to store those values somewhere and call on them every time it refreshes?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width