Results 1 to 10 of 10

Thread: Coloring in shapes?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2012
    Posts
    9

    Coloring in shapes?

    I have a set of shapes created in visual basic that need to be colored in:
    something Code:
    1. Public Class Form1
    2.  
    3.     Private Sub btnDraw_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDraw.Click
    4.         Dim formSurface As Graphics = Me.CreateGraphics 'creates surface
    5.  
    6.         Dim world As New Pen(Color.Black, 3)
    7.         formSurface.DrawRectangle(world, 250, 50, 300, 300) 'world
    8.  
    9.  
    10.         Dim roof As New Pen(Color.Black, 3)
    11.         Dim roof1 As New Point(325, 200)
    12.         Dim roof2 As New Point(475, 200)
    13.         Dim roof3 As New Point(400, 100)
    14.         Dim roof4 As New Point(400, 100)
    15.         Dim curvePoints As Point() = {roof1, roof2, roof3, roof4}
    16.         formSurface.DrawPolygon(roof, curvePoints) 'triangle roof
    17.  
    18.         Dim body As New Pen(Color.Black, 3)
    19.         formSurface.DrawRectangle(body, 325, 200, 150, 150) 'square body
    20.  
    21.         Dim sun As New Pen(Color.Black, 3)
    22.         formSurface.DrawEllipse(sun, 450, 75, 50, 50) 'sun
    23.  
    24.         Dim door As New Pen(Color.Black, 3)
    25.         formSurface.DrawRectangle(door, 387, 300, 25, 50) 'door
    26.  
    27.     End Sub
    28. End Class
    Everything works perfectly; the shapes are all generated where I want them to be. (I'm trying to draw a house) However, how do I color them in? Also, if I do, will the colors overlap (I want that to happen)?

    Should I use some function like image.fill? I know that isn't right but I'm looking for something like that.

    Thanks!

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Coloring in shapes?

    There is generally a Filled version of each method. Therefore, you should have a FillRectangle that will draw a solid rectangle rather than the outline. Also, it would be advisable to do your drawing in the Paint event. That gives you a Graphics object as part of the e argument, and will have the advantage that it will redraw as needed.
    My usual boring signature: Nothing

  3. #3
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Re: Coloring in shapes?

    Take a look at the fill[shape]. Like for example, the fillrectangle. A couple of changes would be using a brush instead of a pen.

    Edit - Ninja'd by Shaggy trying to get a msdn link :P
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Coloring in shapes?

    Your link is better than what I found, which is why I didn't include it.
    My usual boring signature: Nothing

  5. #5
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Coloring in shapes?

    Quote Originally Posted by Shaggy Hiker View Post
    There is generally a Filled version of each method. Therefore, you should have a FillRectangle that will draw a solid rectangle rather than the outline. Also, it would be advisable to do your drawing in the Paint event. That gives you a Graphics object as part of the e argument, and will have the advantage that it will redraw as needed.
    And the disadvantage that it will go on redrawing itself whether you want it to or not! Bit of a pain if you want to reuse a drawing surface.

  6. #6

    Thread Starter
    New Member
    Join Date
    Jun 2012
    Posts
    9

    Re: Coloring in shapes?

    Ok. I was having trouble with the brushes because I declared them as variables. Instead, I just, instead of the original Pen name (roof, etc.), used Brushes.MyColor. It worked. Thanks though!

  7. #7
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Re: Coloring in shapes?

    Brushes can be kinda tricky, you gotta declare it as a SolidBrush. Check out this example:
    Code:
            Dim yellerBrush As New SolidBrush(Color.Yellow)
            Dim g As Graphics = e.Graphics
            Dim rect As New Rectangle(25, 25, 50, 50)
            g.FillEllipse(yellerBrush, rect)
    But if you want to use FillRectangle(Brushes.Yellow, Rectangle), then thats perfectly fine ;]
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Coloring in shapes?

    Quote Originally Posted by dunfiddlin View Post
    And the disadvantage that it will go on redrawing itself whether you want it to or not! Bit of a pain if you want to reuse a drawing surface.
    Well, no. First up, the fact that drawing not done on a Paint event will disappear when the form is minimised or another form dragged over it or at various other times is obviously completely unacceptable.

    As for drawing going on whether you want it or not, that is simply not the case. The code in your Paint event handler will get executed every time the Paint event is raised. There's no reason that that code has to draw the same thing every time. You simply design your app in such a way that all the information that describes the drawing is stored in one or more member variables. Each Paint event, you read those variables and draw what they describe. If what they describe changes then what you draw changes. If they describe nothing then you draw nothing.

    For examples of that in action, check this out:

    http://www.vbforums.com/showthread.php?t=588199

    You could easily extend that to allow the user to delete boxes, change their size and colour or draw different shapes.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    New Member
    Join Date
    Jun 2012
    Posts
    9

    Re: Coloring in shapes?

    Also, how do I get it so that the colors don't overlap when I change them? I'm trying to get options for colors and when I change the house body color, it overlaps the door color. When I change the sky color, it overlaps everything.

  10. #10
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Coloring in shapes?

    I imagine that you are selecting the particular rectangle/polygon and changing the colour within it without redrawing the shapes it intersects with. This will, of course, overwrite anything which appears in the intersecting area. You need to redraw all the items in the correct order or at least as many as intersect with the area in which you've changed the colour. So 'sky' will require a complete redrawing always, 'walls' will require a redrawing of 'door' and so on.

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