用VB.NET绘制三角形

用VB.NET绘制三角形,vb.net,random,geometry,draw,Vb.net,Random,Geometry,Draw,亲爱的论坛成员,我要做一个三角形的类 我的问题是,我认为公共覆盖sub-teken不起作用 以我的课堂形式(函数随机化) 以我的班级形式(亚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())

亲爱的论坛成员,我要做一个三角形的类

我的问题是,我认为公共覆盖sub-teken不起作用

以我的课堂形式(函数随机化)

以我的班级形式(亚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
朋友类三角形
继承菲古尔
公共属性Point1()作为点
公共属性Point2()作为点
公共属性Point3()作为点
私有_Pointers()作为点={Point1,Point2,Point3}
公共子新建(penKleur作为颜色,vulKleur作为颜色,point1作为点,point2作为点,point3作为点)
MyBase.New(彭克勒、弗克勒)
Me.Point1=Point1
Me.Point2=Point2
Me.Point3=Point3
端接头
公共覆盖子Teken(doek作为图形)
使用borstel作为新的SolidBrush(VulKleur),
笔作为新笔(PenKleur,PenDikte)
将tria尺寸标注为新矩形(_指针)**'两个问题:

矩形对象不接受指针数组,此外,您正在尝试创建三角形,而不是矩形。消除这一点:

' Dim tria As New Rectangle(_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
旁注:确保使用控件的绘制事件:

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

“它不起作用”是一个非常没有希望的诊断。让我们假设它的意思是“我没有看到三角形”,而不是“我的代码点燃了我的主板”。必须有一个OnPaint()方法或Paint事件处理程序来实际调用Teken()方法。您没有发布一个,因此它可能会丢失。重写窗体的OnPaint()方法。
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