C#椭圆和线条在形状容器中重叠

时间:2013-06-26 21:52:53

标签: c# drawing shape overlapping

在C#中,我试图使用Microsoft.VisualBasic.PowerPacks中的椭圆和线条组件绘制地图(由线条连接的圆圈)。 在我绘制圆或线后,我将其父设置为形状容器(Microsoft.VisualBasic.PowerPacks.ShapeContainer())。 我的问题是线条显示在圆圈上方,即使在我的代码中我在圆圈之前创建了线条。 你知道如何将椭圆形带到前面吗?

Image

代码本身:

public partial class Map : Form
{
    private int map_w, map_h;
    private List<int> map_data;

    int j, i;

    private Microsoft.VisualBasic.PowerPacks.ShapeContainer canvas;

    private const int circle_size=75;
    private const int circle_spacing = 75;

    private List<Microsoft.VisualBasic.PowerPacks.OvalShape> circles = new List<Microsoft.VisualBasic.PowerPacks.OvalShape>();
    private List<Microsoft.VisualBasic.PowerPacks.LineShape> lines = new List<Microsoft.VisualBasic.PowerPacks.LineShape>();
    /// <summary>
    /// Shows the game map
    /// </summary>
    /// <param name="w">the width of the map</param>
    /// <param name="h">the hieght of the map</param>
    /// <param name="d">the list containing the map</param>
    public Map(int w, int h, List<int> d)
    {
        InitializeComponent();
        this.Width = w * circle_size + circle_spacing * w;//the number of territories vertically * radius of a territory * number of connections between circles
        this.Height = h * circle_size + circle_spacing * h + circle_size / 2 + circle_size/10;//same as above, only on vertical

        canvas = new Microsoft.VisualBasic.PowerPacks.ShapeContainer();
        canvas.Parent = this;

        map_w = w;
        map_h = h;
        map_data = d;

        for(j=0;j<h;j++)//draw lines
            for (i = 0; i < w; i++)
            {
                if (j - 1 >= 0 && i - 1 >= 0 && map_data[i + map_w * j] == 1)//to upper left
                    if (map_data[(i - 1) + map_w * (j - 1)] == 1)
                    {
                        int x1, y1, x2, y2;//start and end points for the line
                        x1 = (i - 1) * circle_size + Math.Abs((i - 1)) * circle_spacing + circle_size / 2;
                        //the circle(i-1, j-1 because it's upper left) and we also take acccount of the space between circles
                        y1 = (j - 1) * circle_size + Math.Abs((j - 1)) * circle_spacing + circle_size / 2;

                        x2 = i * circle_size + i * circle_spacing + circle_size / 2;
                        //the second circle(original one)
                        y2 = j * circle_size + j * circle_spacing + circle_size / 2;

                        Microsoft.VisualBasic.PowerPacks.LineShape line = new Microsoft.VisualBasic.PowerPacks.LineShape(x1, y1, x2, y2);//this is our line
                        line.Parent = canvas;//we draw it
                        lines.Add(line);//add it to the array
                        /*int t1, t2;//for debugging
                        t1 = i - 1;
                        t2 = j - 1;
                        string str = "From " + i + "," + j + " to " + t1 + "," + t2;
                        //listBox1.Items.Add(str);*/
                    }
                if (j + 1 < map_h && i - 1 >= 0 && map_data[i + map_w * j] == 1)//to lower left
                    if (map_data[(i - 1) + map_w * (j + 1)] == 1)
                    {
                        int x1, y1, x2, y2;
                        x1 = (i - 1) * circle_size + Math.Abs((i - 1)) * circle_spacing + circle_size / 2;
                        y1 = (j + 1) * circle_size + Math.Abs((j + 1)) * circle_spacing + circle_size / 2;
                        x2 = i * circle_size + i * circle_spacing + circle_size / 2;
                        y2 = j * circle_size + j * circle_spacing + circle_size / 2;
                        Microsoft.VisualBasic.PowerPacks.LineShape line = new Microsoft.VisualBasic.PowerPacks.LineShape(x1, y1, x2, y2);
                        line.Parent = canvas;
                        lines.Add(line);
                    }
                if (j + 1 < map_h && map_data[i + map_w * j] == 1)//below
                    if (map_data[i + map_w * (j + 1)] == 1)
                    {
                        int x1, y1, x2, y2;
                        x1 = i * circle_size + Math.Abs(i) * circle_spacing + circle_size / 2;
                        y1 = (j + 1) * circle_size + Math.Abs((j + 1)) * circle_spacing + circle_size / 2;
                        x2 = i * circle_size + i * circle_spacing + circle_size / 2;
                        y2 = j * circle_size + j * circle_spacing + circle_size / 2;
                        Microsoft.VisualBasic.PowerPacks.LineShape line = new Microsoft.VisualBasic.PowerPacks.LineShape(x1, y1, x2, y2);
                        line.Parent = canvas;
                        lines.Add(line);
                    }
                if (i + 1 < map_w && map_data[i + map_w * j] == 1)//right
                    if (map_data[i + 1 + map_w * j] == 1)
                    {
                        int x1, y1, x2, y2;
                        x1 = (i + 1) * circle_size + Math.Abs(i + 1) * circle_spacing + circle_size / 2;
                        y1 = j * circle_size + Math.Abs(j) * circle_spacing + circle_size / 2;
                        x2 = i * circle_size + i * circle_spacing + circle_size / 2;
                        y2 = j * circle_size + j * circle_spacing + circle_size / 2;
                        Microsoft.VisualBasic.PowerPacks.LineShape line = new Microsoft.VisualBasic.PowerPacks.LineShape(x1, y1, x2, y2);
                        line.Parent = canvas;
                        lines.Add(line);
                    }
            }

        for(j=0;j<h;j++)//draw circles
            for (i = 0; i < w; i++)
            {
                if (map_data[i + w*j] == 1)//if there's a circle at that location, we draw it
                {
                    //create a circle at the locaiton on screen
                    Microsoft.VisualBasic.PowerPacks.OvalShape oval = new Microsoft.VisualBasic.PowerPacks.OvalShape(i * circle_size + i * circle_spacing, j * circle_size + j * circle_spacing, circle_size, circle_size);
                    oval.FillColor = Color.Green;//set the backgorund color
                    oval.FillStyle = Microsoft.VisualBasic.PowerPacks.FillStyle.Solid;//and fill style
                    oval.BorderColor = Color.Black;//border
                    oval.BorderWidth = 2;//and width
                    oval.Parent = canvas;//set the parent to be the shape container
                    circles.Add(oval);//and add it to our list
                } 
            }
    }
}

1 个答案:

答案 0 :(得分:0)