绘制三角形VB.NET

时间:2013-04-01 16:20:32

标签: vb.net random geometry draw

亲爱的论坛成员,我必须做一个三角课。

我的问题是我公开覆盖子teken不起作用

以我的课程表格(功能随机)

Private Function RandomStraal() As Int32
    Return 20 + _Random.Next(Me.ClientSize.Width\2)
End Function

IN MY CLASS FORM(SUB MAAKTRIANGLE)

 Private Sub MaakTriangle(x As Integer, y As Integer)
    Dim tria As New Triangle(RandomKleur, RandomKleur, New Point(x, y), New Point(x, y + RandomStraal()), New Point(x + RandomStraal(), y))
    tria.PenDikte = _Random.Next(1, 6)
    _Tekening.Add(tria)
    '_Tekening.Teken(Me.CreateGraphics)
    Invalidate()
End Sub

MY CLASS TRIANGLE

Friend Class Triangle
    Inherits Figuur


    Public Property Point1() As Point
    Public Property Point2() As Point
    Public Property Point3() As Point

    Private _Pointers() As Point = {Point1, Point2, Point3}

    Public Sub New(penKleur As Color, vulKleur As Color, point1 As Point, point2 As Point, point3 As Point)
        MyBase.New(penKleur, vulKleur)
        Me.Point1 = point1
        Me.Point2 = point2
        Me.Point3 = point3
    End Sub

    Public Overrides Sub Teken( doek As Graphics)
        Using borstel As New SolidBrush(VulKleur),
            pen As New Pen(PenKleur, PenDikte)
            Dim tria As New Rectangle(_Pointers)      **'<--the problem**
            doek.FillPolygon(borstel, tria)
            doek.DrawPolygon(pen, tria)
        End Using
    End Sub
End Class

做这项工作会发生什么

提前致谢

1 个答案:

答案 0 :(得分:3)

两个问题:

Rectangle对象不接受指针数组,此外,您尝试制作三角形而不是矩形。消除这个:

' Dim tria As New Rectangle(_Pointers)

第二个问题是您正在引用_Pointers数组,但它们不会使用新坐标进行更新。这些点都是(0,0):

试试这样:

Public Overrides Sub Teken(doek As Graphics)
  Using borstel As New SolidBrush(VulKleur), _
        pen As New Pen(Me.PenKleur, Me.PenDikte)
    Dim myPoints() As Point = New Point() {Point1, Point2, Point3}
    doek.FillPolygon(borstel, myPoints)
    doek.DrawPolygon(pen, myPoints)
  End Using
End Sub

旁注:确保使用控件的Paint事件:

Private Sub Panel1_Paint(sender As Object, e As PaintEventArgs) _
                         Handles Panel1.Paint
  e.Graphics.Clear(Color.White)

  Dim tria As New Triangle(Color.Blue, Color.Red, New Point(64, 64), _
                                                  New Point(96, 96), _
                                                  New Point(32, 96))
  tria.Teken(e.Graphics)
End Sub
如果直接在表单上绘图,则

或覆盖OnPaint方法。