将三角形对象保存在列表中并再次绘制

Saving triangle object in a list and paint again

我正在尝试保存我绘制的三角形并在之前的三角形仍然存在的情况下再次绘制。我在矩形、正方形、圆形和椭圆形中进行了此操作,并且有效。我不知道为什么它不会在 Triangle 中。代码有问题吗?

This is how I draw and "save (not working)"

形状Class

    public void DrawTriangle(Color c, int stroke,PointF[] tpoints, float w, Graphics g)
    {
        this.width = w;
        this.strokeThickness = stroke;
        this.tPoints = tpoints;
        g.InterpolationMode = InterpolationMode.High;
        g.SmoothingMode = SmoothingMode.AntiAlias;
        g.DrawPolygon(new Pen(c, stroke), tpoints);
    }

表格 1

    public void DrawTriangle()
    {
        tC = Color.Red;
        strokeTriangle = trackBar_Stroke.Value;
        tW = Convert.ToInt32((Convert.ToInt32(tbox_Width.Text) * 96) / 25.4);
        tH = (Convert.ToInt32((tW * (Math.Sqrt(3))) / 2));
        tX = (pictureBox_Canvass.Width - tW) / 2;
        tY = ((pictureBox_Canvass.Width - (tH)) / 2) + tH;

        float angle = 0;
        t_Points[0].X = tX;
        t_Points[0].Y = tY;
        t_Points[1].X = (float)(tX + tW * Math.Cos(angle));
        t_Points[1].Y = (float)(tY + tW * Math.Sin(angle));
        t_Points[2].X = (float)(tX + tW * Math.Cos(angle - Math.PI / 3));
        t_Points[2].Y = (float)(tY + tW * Math.Sin(angle - Math.PI / 3));
    }
    public void AcceptTriangle()
    {
        Shape shape = new Shape();
        tC = Color.Gray;
        shape.strokeThickness = strokeTriangle;
        shape.width = tW;
        shape.x = tX;
        shape.y = tY;
        shape.tPoints = t_Points;
        s._triangle.Add(shape);
    }

 s.DrawTriangle(tC, strokeTriangle,t_Points, tX, tY, tW, e.Graphics);

This is how I iterate it.

public List<Shape> _triangle = new List<Shape>();
foreach(Shape shapes3 in s._triangle)
        {
            shapes3.DrawTriangle(shapes3.color, shapes3.strokeThickness, shapes3.tPoints, shapes3.width, e.Graphics);
        }

在您的代码中的两个位置,您编写了这样的数组赋值。:

this.tPoints = tpoints;

..还有这个:

shape.tPoints = t_Points;

假设这会创建一个包含数据的数组是一个常见的错误。 它没有。它所做的只是使一个数组变量指向到一个以前存在的数组。

数据没有重复。因此,当您覆盖数据或清除它们时,'new' 数组现在指向更改或清除的数据。

所以所有你的对象有非常相同点。

正确创建数据的实际 副本

最简单的方法是像这样添加一个 ToArray() 调用:

this.tPoints = tpoints.ToArray();

..还有这个:

shape.tPoints = t_Points.ToArray();

重申一下:

int[] p1 = new int[2] { 23, 42 };   // Now we have one array.
int[] p2 = new int[2] { 3, 2 };    // Now we have two arrays.
p2 = p1;                 //  Now we have only one array again.
p2[1]=1234;             // Now p1[1] is 1234

接受三角形(保存到列表)

var triangle = new Shape
                {
                    strokeThickness = strokeTriangle,
                    color = tC,
                    tPoints = t_Points.ToArray(),
                    x=tX,
                    y=tY,
                    width = tW,
                };
                s._triangle.Add(triangle);

遍历列表

foreach(Shape shapes3 in s._triangle)
        {
            shapes3.DrawTriangle(shapes3.color, shapes3.strokeThickness,shapes3.tPoints.ToArray(), shapes3.x, shapes3.y, shapes3.width, e.Graphics);
        }