vb.net 绘制三角形VB.NET
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15747138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Draw triangle VB.NET
提问by Elisa9180
Dear forum members I have to make a triangle class.
亲爱的论坛成员,我必须制作三角形课程。
My problem is that I the public overrides sub teken does not work
我的问题是我的公共覆盖 sub teken 不起作用
IN MY CLASS FORM (FUNCTION RANDOMSTRAAL)
在我的班级形式中(函数随机数)
Private Function RandomStraal() As Int32
Return 20 + _Random.Next(Me.ClientSize.Width)
End Function
IN MY CLASS FORM (SUB MAAKTRIANGLE)
在我的课堂形式(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
what should happen to do this work
做这项工作应该怎么做
thanks in advance
提前致谢
回答by LarsTech
Two problems:
两个问题:
The Rectangle object doesn't take an array of pointers, and besides, you are trying to make a triangle, not a rectangle. Eliminate this:
Rectangle 对象不接受指针数组,此外,您正在尝试创建一个三角形,而不是一个矩形。消除这个:
' Dim tria As New Rectangle(_Pointers)
The second problem is you are referencing the _Pointers array, but they are not updated with the new coordinates. The points are all (0, 0):
第二个问题是您正在引用 _Pointers 数组,但它们没有使用新坐标进行更新。点都是 (0, 0):
Try it like this:
像这样尝试:
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
Side note: make sure you to use the Paint event of your control:
旁注:确保使用控件的 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
or override the OnPaint method if drawing directly on a form.
如果直接在窗体上绘制,则覆盖 OnPaint 方法。

