VB.NET 从列表中沿 X 坐标移动直线的所有点

VB.NET shifting all points of a line along the X coordinate from a list

我有一个函数可以从 VB.NET 函数中的点列表绘制线到 PictureBox 上。将两个点添加到列表中,并在添加到列表中的所有点之间绘制一条线。然而,当一个新的点被添加到列表中时,我想将所有以前绘制的线移到左边。

例如,如果分别从列表中在 (9, 0) (9, 4)(10, 0) (10, 3) 之间绘制了 2 条不同的线,并将第三条线添加到列表 (10, 0) (10, 2),我想像这样移动前两行:

(9, 0), (9, 4)(8, 0), (8, 4)

(10, 0), (10, 3)(9, 0), (9, 3)

这是对我的意思的演示。

绿线是画的第一条线,黄线是画的第二条线。在第二张图中,添加了一条深蓝色的线,并且绘制的前两条线,黄色和绿色,都向左移动。

我正在使用此代码尝试绘制这些类型的线条。

Public Class Line

    Public ReadOnly Property StartPoint As Point

    Public ReadOnly Property EndPoint As Point

    Public Sub New(startPoint As Point, endPoint As Point)
        Me.StartPoint = startPoint
        Me.EndPoint = endPoint
    End Sub

End Class
Public Class Form1
Private lines As New List(Of Line)

Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
    For Each line In lines
        e.Graphics.DrawLine(Pens.Black, line.StartPoint, line.EndPoint)
    Next
End Sub

Private Sub AddNewLine(length As Integer)

    Dim pictureBoxRightXPoint As Integer = 300 'the right most side of the PictureBox is the x coordinate of 300 (PictureBox has the x size of 300).

    For Each l As line In lines 'move all points of X in all previously drawn lines in the list to the left.
            l.StartPoint.X = l.StartPoint.X - 1
            l.EndPoint.X = l.EndPoint.X - 1

    Next
    lines.Add(New Line((pictureBoxRightXPoint, 0), (pictureBoxRightXPoint, length))
    PictureBox1.Invalidate() 'Refresh the PictureBox to redraw the lines.
End Sub

End Class

添加后,出现错误:

Expression is a value and therefore cannot be the target of an assignment

当我尝试从 X 坐标中减去 1 以将列表中的所有点向左移动时。

如何解决此问题或在 PictureBox 中将所有先前绘制的点向左移动时绘制一条线? (注意:列表中可能已经添加了多行)。

(用于绘制和向列表添加线条的代码来自:)。

如果您想绘制我相信您在 PictureBox 末尾陈述的图像(在您的示例中),您可以将 PictureBox 放在右端a Panel 并在 PictureBox 的开头绘制你的 graphics/lines。要将所有先前绘制的图形向左移动,您可以将 PictureBox 的位置在 Panel 内向左移动。

您可以使用之前的函数来添加新行,但是移动您的 PictureBox 并将其开始放在 Panel 的末尾。要添加新行,请使用:

Dim xint As Integer = 1
Dim r As Random = New Random
xint = xint + 1
Dim Point1 As New Point(xint, r.Next(40, 79))
Dim point2 As New Point(xint, r.Next(80, 100))

AddNewLine(Point1, point2)
PictureBox1.Left -= 1 'Move the PictureBox Left

PictureBox1.Size = New Size(PictureBox2.Size.Width + 1, 269) 'Change its size to allow for continuous line drawing 

您可以将我使用的随机 Y 长度替换为您操作的实际点。您链接的画线功能将适用于此。为了澄清,请确保使用此:

Private Sub AddNewLine(startPoint As Point, endPoint As Point)
    lines.Add(New Line(startPoint, endPoint))
    PictureBox1.Invalidate()
End Sub

你的 Paint 事件应该是这样的:

For Each line In lines
     e.Graphics.DrawLine(Pens.Black, line.StartPoint, line.EndPoint)
Next