在 VB.NET 中用给定的大小和 X/Y POS 绘制一个圆产生意想不到的结果

Drawing a Circle in VB.NET with given Size and X/Y POS producing unexpected results

我正在制作一个应用程序,当给定圆的 XY 的直径和中心点的输入时,它会绘制一个圆。 PictureBox 中的 TextBox

当我 运行 应用程序并输入常量 X 和 Y 但缓慢 increase/decrease 圆的直径时,圆开始随机移动位置,即使 X 和 Y 位置是持续的。有时,如果我在没有更改任何变量(X 位置、Y 位置和直径)时刷新 PictureBox,即使没有任何更改,圆圈也会毫无理由地移动...

这是要重现的代码:

这些变量已经声明:

    Dim beginSCircleXPos As Integer 'What the user put into as the circle's center for X
    Dim beginSCircleYPos As Integer 'What the user put into as the circle's center for y

    Dim sCircleXPos As Integer 'Top-Left point of the circle in X
    Dim sCircleYPos As Integer 'Top-Left point of the circle in Y
    Dim sCircleDiameter As Integer 'The diameter that user put into.


    Dim sCenterPointX As Integer 'Variables will be updated later
    Dim sCenterPointY As String

PictureBoxPaint事件

beginSCircleXPos = TextBox1.Text
        beginSCircleYPos = TextBox2.Text

        sCircleXPos = beginSCircleXPos - sCircleDiameter / 2
        sCircleYPos = beginSCircleYPos - sCircleDiameter / 2


        sCircleDiameter = TextBox3.Text


        Using path As New GraphicsPath
            path.AddEllipse(sCircleXPos, sCircleYPos, sCircleDiameter, sCircleDiameter)
            sCenterPointX = sCircleXPos + sCircleDiameter / 2
            sCenterPointY = sCircleYPos + sCircleDiameter / 2

            Using brush As New PathGradientBrush(path)
                brush.CenterPoint = New PointF(sCenterPointX, sCenterPointY) 
                brush.CenterColor = Color.FromArgb(0, Color.Orange)
                brush.SurroundColors = {Color.Orange}
                brush.FocusScales = PointF.Empty

                e.Graphics.FillRectangle(brush, sCircleXPos, sCircleYPos, sCircleDiameter, sCircleDiameter)
            End Using
        End Using

值得注意的是PictureBox是通过PictureBox1.Refresh()命令点击一个按钮刷新的

我该如何解决这个问题,以便中心点的输入和 X/Y 值每次都能得到正确且恒定的答案?

编辑:

  beginSCircleXPos = Convert.ToSingle(TextBox1.Text)
        beginSCircleYPos = Convert.ToSingle(TextBox2.Text)


        sCircleXPos = beginSCircleXPos - sCircleDiameter / 2
        sCircleYPos = beginSCircleYPos - sCircleDiameter / 2


        sCircleDiameter = Convert.ToSingle(TextBox3.Text)






        Using path As New GraphicsPath
            path.AddEllipse(sCircleXPos, sCircleYPos, sCircleDiameter, sCircleDiameter)
            sCenterPointX = sCircleXPos + sCircleDiameter / 2
            sCenterPointY = sCircleYPos + sCircleDiameter / 2

            Using brush As New PathGradientBrush(path)
                brush.CenterPoint = New PointF(sCenterPointX, sCenterPointY) 
                brush.CenterColor = Color.FromArgb(0, Color.Orange)
                brush.SurroundColors = {Color.Orange}
                brush.FocusScales = PointF.Empty

                e.Graphics.FillRectangle(brush, sCircleXPos, sCircleYPos, sCircleDiameter, sCircleDiameter)
            End Using
        End Using
    ```
Variables:

将 beginSCircleXPos 调暗为单个 将 beginSCircleYPos 变暗为单个

Dim sCircleXPos As Single
Dim sCircleYPos As Single
Dim sCircleDiameter As Single


Dim sCenterPointX As Single
Dim sCenterPointY As Single

我没有仔细看你的代码。我仍然有我提供的代码来回答你之前的问题,所以我以此为基础,只是像我自己做的那样修改它。我使用 Timer 每秒重新绘制一次 PictureBox 以使其更简单一些。这就是我最终得到的:

Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
    Dim centreX As Single
    Dim centreY As Single
    Dim diameter As Single

    If Single.TryParse(TextBox1.Text, centreX) AndAlso
       Single.TryParse(TextBox2.Text, centreY) AndAlso
       Single.TryParse(TextBox3.Text, diameter) Then
        Dim radius = diameter / 2.0F
        Dim bounds As New RectangleF(centreX - radius,
                                     centreY - radius,
                                     diameter,
                                     diameter)

        Using path As New GraphicsPath
            path.AddEllipse(bounds)

            Using brush As New PathGradientBrush(path) With {.CenterPoint = New PointF(centreX, centreY),
                                                             .CenterColor = Color.FromArgb(0, Color.Orange),
                                                             .SurroundColors = {Color.Orange},
                                                             .FocusScales = PointF.Empty}
                e.Graphics.FillRectangle(brush, bounds)
            End Using
        End Using
    End If
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    PictureBox1.Invalidate()
End Sub

它对我来说似乎和预期的一样有效。如果 TextBoxes 中的一个或多个不包含有效数字,则不会绘制任何内容,否则会在预期的位置和方式绘制分级圆圈。如果我设置中心坐标然后改变直径,绘制的圆保持不变的中心但按预期增长和收缩。

如果你愿意,你显然可以使用 Button.Click 而不是 Timer.Tick