Click to See Complete Forum and Search --> : Drawing a triangles and squares?


Cheeko
April 11th, 2003, 06:47 PM
Is it possible in VB .NET to draw shapes in a picture box and then fil lthe shapes with a colour?

For example, could I get the basic outline of a house (square with a triangle on top) and get the square filled brick red and the triangle filled grey?

DdH
April 12th, 2003, 03:05 AM
Yes, that is possible.
To try the following example, add the code to the codesection of a form.

Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
Dim gr As System.Drawing.Graphics = e.Graphics
Dim x, y As Integer

x = 100
y = 100

' Define shape of the house
Dim p(2) As System.Drawing.Point
p(0) = New System.Drawing.Point(x - 10, y)
p(1) = New System.Drawing.Point(x + 110, y)
p(2) = New System.Drawing.Point(x + 50, y - 50)
Dim r As New System.Drawing.Rectangle(x, y, 100, 100)

' Draw house
Dim bGray As New System.Drawing.SolidBrush(System.Drawing.Color.Gray)
Dim bBrick As New System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D.HatchStyle.HorizontalBrick, System.Drawing.Color.Orange, System.Drawing.Color.Red)
gr.FillPolygon(bGray, p)
gr.FillRectangle(bBrick, r)

' Draw house outline
Dim pBlack As New System.Drawing.Pen(System.Drawing.Color.Black, 2)
gr.DrawPolygon(pBlack, p)
gr.DrawRectangle(pBlack, r)
End Sub


Danny

Cheeko
April 12th, 2003, 04:39 AM
wow :D that is really cool :D

.NET \o/

Thanks alot

Cheeko
April 12th, 2003, 07:07 PM
Ok, Ive expanded the code.

I now have it, that when a user clicks within a certain area (ie, a part of the drawing) the color dialog shows and they select a new color. Ive used a system.drawing.color variable to store the color, but i just cant repaint the form :(


Dim cSleave1 As System.Drawing.Color
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
Dim gr As System.Drawing.Graphics = e.Graphics
' Define shape of the shirt
Dim s1(3) As System.Drawing.Point
s1(0) = New System.Drawing.Point(100, 100)
s1(1) = New System.Drawing.Point(55, 123)
s1(2) = New System.Drawing.Point(55, 158)
s1(3) = New System.Drawing.Point(100, 135)

Dim s2(3) As System.Drawing.Point
s2(0) = New System.Drawing.Point(200, 100)
s2(1) = New System.Drawing.Point(245, 123)
s2(2) = New System.Drawing.Point(245, 158)
s2(3) = New System.Drawing.Point(200, 135)
Dim r As New System.Drawing.Rectangle(100, 100, 100, 125)

' Draw house
cSleave1 = System.Drawing.Color.Blue
Dim bS1 As New System.Drawing.SolidBrush(cSleave1)
Dim bS2 As New System.Drawing.SolidBrush(System.Drawing.Color.Blue)
Dim bSH As New System.Drawing.SolidBrush(System.Drawing.Color.WhiteSmoke)
gr.FillPolygon(bS1, s1)
gr.FillPolygon(bS2, s2)
gr.FillRectangle(bSH, r)
' Draw house outline
Dim pBlack As New System.Drawing.Pen(System.Drawing.Color.Black, 2)
gr.DrawPolygon(pBlack, s1)
gr.DrawPolygon(pBlack, s2)
gr.DrawRectangle(pBlack, r)
End Sub

Protected Overrides Sub OnClick(ByVal e As System.EventArgs)
If Me.MousePosition.X > 54 & Me.MousePosition.X < 101 & Me.MousePosition.Y > 99 & Me.MousePosition.Y < 158 Then
dColour.ShowDialog()
cSleave1 = dColour.Color

' REPAINT THE GOD **** FORM! :D
End If
End Sub


Any ideas how to update the color?

DdH
April 13th, 2003, 05:32 AM
You can try something like this.
I made 4 changes to your code.


' 1: set default color for sleave
Dim cSleave1 As System.Drawing.Color = System.Drawing.Color.Blue
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
Dim gr As System.Drawing.Graphics = e.Graphics
' Define shape of the shirt
Dim s1(3) As System.Drawing.Point
s1(0) = New System.Drawing.Point(100, 100)
s1(1) = New System.Drawing.Point(55, 123)
s1(2) = New System.Drawing.Point(55, 158)
s1(3) = New System.Drawing.Point(100, 135)

Dim s2(3) As System.Drawing.Point
s2(0) = New System.Drawing.Point(200, 100)
s2(1) = New System.Drawing.Point(245, 123)
s2(2) = New System.Drawing.Point(245, 158)
s2(3) = New System.Drawing.Point(200, 135)
Dim r As New System.Drawing.Rectangle(100, 100, 100, 125)

' Draw house
' 2: Don't need to set color here anymore
'cSleave1 = System.Drawing.Color.Blue
Dim bS1 As New System.Drawing.SolidBrush(cSleave1)
Dim bS2 As New System.Drawing.SolidBrush(System.Drawing.Color.Blue)
Dim bSH As New System.Drawing.SolidBrush(System.Drawing.Color.WhiteSmoke)
gr.FillPolygon(bS1, s1)
gr.FillPolygon(bS2, s2)
gr.FillRectangle(bSH, r)
' Draw house outline
Dim pBlack As New System.Drawing.Pen(System.Drawing.Color.Black, 2)
gr.DrawPolygon(pBlack, s1)
gr.DrawPolygon(pBlack, s2)
gr.DrawRectangle(pBlack, r)
End Sub

' 3: Use OnMouseDown instate of OnClick
Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
If e.X > 54 And e.X < 101 And e.Y > 99 And e.Y < 158 Then
dColour.ShowDialog()
cSleave1 = dColour.Color

' 4: Redraw your Form
Me.Refresh()
End If
End Sub


Danny

Cheeko
April 14th, 2003, 06:22 AM
again, thanks alot :D