|
-
Jul 23rd, 2012, 05:05 PM
#1
Thread Starter
New Member
Coloring in shapes?
I have a set of shapes created in visual basic that need to be colored in:
something Code:
Public Class Form1
Private Sub btnDraw_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDraw.Click
Dim formSurface As Graphics = Me.CreateGraphics 'creates surface
Dim world As New Pen(Color.Black, 3)
formSurface.DrawRectangle(world, 250, 50, 300, 300) 'world
Dim roof As New Pen(Color.Black, 3)
Dim roof1 As New Point(325, 200)
Dim roof2 As New Point(475, 200)
Dim roof3 As New Point(400, 100)
Dim roof4 As New Point(400, 100)
Dim curvePoints As Point() = {roof1, roof2, roof3, roof4}
formSurface.DrawPolygon(roof, curvePoints) 'triangle roof
Dim body As New Pen(Color.Black, 3)
formSurface.DrawRectangle(body, 325, 200, 150, 150) 'square body
Dim sun As New Pen(Color.Black, 3)
formSurface.DrawEllipse(sun, 450, 75, 50, 50) 'sun
Dim door As New Pen(Color.Black, 3)
formSurface.DrawRectangle(door, 387, 300, 25, 50) 'door
End Sub
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!
-
Jul 23rd, 2012, 05:08 PM
#2
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
 
-
Jul 23rd, 2012, 05:10 PM
#3
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
-
Jul 23rd, 2012, 05:12 PM
#4
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
 
-
Jul 23rd, 2012, 06:02 PM
#5
Re: Coloring in shapes?
 Originally Posted by Shaggy Hiker
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.
-
Jul 23rd, 2012, 07:15 PM
#6
Thread Starter
New Member
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!
-
Jul 23rd, 2012, 10:54 PM
#7
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 ;]
-
Jul 23rd, 2012, 11:08 PM
#8
Re: Coloring in shapes?
 Originally Posted by dunfiddlin
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.
-
Jul 24th, 2012, 08:01 AM
#9
Thread Starter
New Member
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.
-
Jul 24th, 2012, 10:11 AM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|