1

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

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

在我的课堂形式中(功能随机)

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

在我的班级表格中(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

我的班级三角

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

做这项工作应该怎么做

提前致谢

4

1 回答 1

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 方法。

于 2013-04-01T18:19:36.053 回答