我旨在创建一个灵活的进度条,以用于简单的游戏中。我正在绘制一条曲线(样条线),然后将这些点提取到列表中。然后,我有一个选择:我可以使用最后一个点来绘制播放器的位置-或将所有点连接到标记。绘制曲线成功。提取点成功。画点是成功的。当我在窗体中添加按钮后,就会很快出现一条多余的线,该线将曲线的末端连接到其起点。问题:如何防止不必要的线路?抱歉,我想添加图片,但是我不知道该怎么做。这是我的代码:-
private void Form1_Paint(object sender, PaintEventArgs e)
{
Point point = new Point(ovalPictMansion.Left - 30, ovalPictMansion.Top - 20);
Size size = new Size(ovalPictMansion.Width + 60, ovalPictMansion.Height + 50);
Rectangle rect = new Rectangle(point, size);
Pen pen = new Pen(Color.DarkKhaki, 9);
e.Graphics.DrawArc(pen, rect, 10, 160);
DrawPath1(e.Graphics, point, pen);
}
GraphicsPath myPath = new GraphicsPath();
private void DrawPath1(Graphics g, Point p, Pen pen)
{
Point[] points1 =
{
new Point(p.X + 10, p.Y + 120),
new Point(p.X - 250, p.Y + 180),
new Point(p.X - 380, p.Y + 390),
new Point(p.X - 430, p.Y + 560),
new Point(p.X - 520, p.Y + 700)
};
g.DrawCurve(pen, points1);
myPath.AddCurve(points1);
g.DrawPath(Pens.Red, myPath);
using (var mx = new Matrix(1, 0, 0, 1, 0, 0))
{
myPath.Flatten(mx, 0.1f);
}
// store points in a list
var list_of_points = new List<PointF>(myPath.PathPoints);
//// Show position of points
//foreach(PointF postnF in list_of_points)
//{
// Point postn = new Point((int)postnF.X - 3, (int)postnF.Y - 3);
// Size size = new Size(6, 6);
// Rectangle rect = new Rectangle(postn, size);
// g.DrawEllipse(Pens.Red, rect);
//}
// Show position of last point only ( track the player )
// Note : The start of the spline is the end of the Player's Path
// So, show the first point ( instead of the last point )
PointF postnF = list_of_points[0];
Point postn = new Point((int)postnF.X - 3, (int)postnF.Y - 3);
Size size = new Size(6, 6);
Rectangle rect = new Rectangle(postn, size);
g.DrawEllipse(Pens.Red, rect);
}
但在我向表单添加按钮后不久,出现了多余的行 将曲线的末端连接到其起点。
这是因为当您添加按钮时,它会使表单重新绘制自身。
在DrawPath1()
中,您有myPath.AddCurve(points1);
。这将多次添加点,每次Paint()事件触发一次。因此,最后一点将吸引到第一个点,因为这些点有多个集合,而最后一点将在新添加的集合中的第一个点旁边...