2

10個の長方形を描画しようとしていますが、使用g.DrawRectangle()すると、次のように十字が描画されます。

クロスを描く

その頂点のオブジェクトを返すgetRectangle()関数を含むVertexオブジェクトを作成していますRectangle

これらのオブジェクトを作成して、上の長方形として表示したいと思っていましたpictureBox

これが私のコードです

    private System.Drawing.Graphics g;
    private System.Drawing.Pen pen1 = new System.Drawing.Pen(Color.Blue, 2F);

    public Form1()
    {
        InitializeComponent();

        pictureBox.Dock = DockStyle.Fill;
        pictureBox.BackColor = Color.White;
    }

    private void paintPictureBox(object sender, PaintEventArgs e)
    {
        // Draw the vertex on the screen
        g = e.Graphics;

        // Create new graph object
        Graph newGraph = new Graph();

        for (int i = 0; i <= 10; i++)
        {
           // Tried this code too, but it still shows the cross
           //g.DrawRectangle(pen1, Rectangle(10,10,10,10);

           g.DrawRectangle(pen1, newGraph.verteces[0,i].getRectangle());
        }
    }

Vertexクラスのコード

class Vertex
{
    public int locationX;
    public int locationY;
    public int height = 10;
    public int width = 10;

    // Empty overload constructor
    public Vertex()
    {
    }

    // Constructor for Vertex
    public Vertex(int locX, int locY)
    {
        // Set the variables
        this.locationX = locX;
        this.locationY = locY;
    }

    public Rectangle getRectangle()
    {
        // Create a rectangle out of the vertex information
        return new Rectangle(locationX, locationY, width, height);

    }
}

グラフクラスのコード

class Graph
{
    //verteces;
    public Vertex[,] verteces = new Vertex[10, 10];

    public Graph()
    {

        // Generate the graph, create the vertexs
        for (int i = 0; i <= 10; i++)
        {
            // Create 10 Vertexes with different coordinates
            verteces[0, i] = new Vertex(0, i);
        }
    }

}
4

3 に答える 3

2

ドローループの例外のように見えます

最後の呼び出し:

newGraph.verteces[0,i]

あなたと一緒に失敗するOutOfRangeException ことを繰り返すのではなくi <= 10i < 10

于 2012-03-15T09:57:00.633 に答える
2

赤十字は、例外がスローされたことを示します。処理されているため、例外は表示されていません。例外スローで中断してキャッチするようにVisualStudioを構成します。

于 2012-03-15T09:58:27.997 に答える
1

例外がスローされました。最初にあなたのコードを見てください:

for (int i = 0; i <= 10; i++)

IndexOutOfRangeExceptionは10個のアイテムがあるために生成されvertecesますが、0から10まで循環します(含まれているため、11個の要素が検索されます)。何をしたいかにもよりますが、サイクルを次のように変更する必要があります(=fromを削除<=):

for (int i = 0; i < 10; i++)

vertecesまたはのサイズを11にインクリメントします。

于 2012-03-15T09:57:43.307 に答える