2012-03-15 2 views
2

내 PictureBox를 내부에 십자가를 끌고있다,하지만 나는 그것이 십자가를 끌고있다 g.DrawRectangle()를 사용하는 경우 아래와 같이왜 DrawRectangle 내가 10 개 사각형을 그릴려고

Drawing cross

을 내가 정점 개체를 만드는거야 해당 꼭지점에 대해 Rectangle 객체를 반환하는 getRectangle() 함수가 포함되어 있습니다.

나는이 객체를 만들고 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()); 
     } 
    } 

코드 정점 클래스 내 코드의

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); 
     } 
    } 

} 
+0

내가 아는 전부는 예외가 귀하의 코드 또는 코드에서 발생했는지 여부입니다. 문제를 디버그하기 위해 무언가를 그릴 때까지 일부 논리를 제거하십시오. –

+1

다른 사람들이 언급했듯이 "십자가"는 예외가 throw 된 결과입니다. 스택에있는 예외를 삼키지 않으면 쉽게 찾아서 수정할 수있는 버그가 코드에있을 수 있습니다. 결국 중요한 교훈이 있습니다. –

답변

2

외모

마지막 호출로 무승부 루프에서 예외처럼 : OutOfRangeException 당신이 i <= 10로하지 반복 말아야과

newGraph.verteces[0,i] 

실패,하지만 i < 10

+0

'for' 루프를 제거해도'g.DrawRectangle (pen1, newGraph.verteces [0, 1] .getRectangle()); '을 넣어도 여전히 십자가를 표시합니다 : ( – Luke

+0

그리고'g .DrawRectangle (pen1, new Rectangle (10,10,10,10)); ' – Luke

+0

은 그래프 생성자에 하나, paintPictureBox 메서드에 하나를 고정합니다. –

1

에 예외가 발생했습니다. 먼저 코드를 살펴에서 : verteces 10 개 항목이 있지만 0 ~ 10까지가주기 (이것은 11 개 요소를 검색 할 수 있습니다 있도록 포함) 것 IndexOutOfRangeException 때문에

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

가 생성됩니다. 을 나타냅니다

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

또는 11

+0

+1 도와 주셔서 감사합니다. :) – Luke

2

적십자에 verteces의 크기를 증가 : 그것은 당신이 수행 할 작업에 따라 달라집니다하지만 당신의주기 (제거 <=에서 =)을 변경해야 예외 처리가 처리 되었기 때문에 예외가 발생했습니다. 그것을 잡으려고 Configure Visual Studio to break on exception throw.

+0

+1 정보를 제공해 주셔서 감사합니다. – Luke