2012-09-29 61 views
-1

我創建的方法的工作原理是它將在pictureBox中創建一個圓,但它只會使用矩形的座標。創建使用圓心和半徑中心繪製圓的方法

我試圖創建以下,

  • Windows窗體有:
    • 1 PictureBox的
    • 1的textBox
    • 1按鈕

的textBox應用於插入cir的半徑CLE。

(double input=Convert.ToDouble(textBox1.Text) 

{ 
private void button1_Click(object sender, EventArgs e) 

    { 
     double input.... 
     double radius= Math.PI*input*input; 
     Graphics paper; 
     paper = pictureBox1.CreateGraphics(); 
     Pen pen = new Pen(Color.Black); 
     getCircle(paper, pen, (variables for center), radius); 
    } 
private void getCircle(Graphics drawingArea, Pen penToUse, int xPos, int yPos, double radius); 

{ 
} 

} 

我的問題在這裏是我不知道如何創建使用Math.PI*radius*radius預先確定的中心座標的圓。

我想看到的編碼方法的答案,並button_click

+1

看起來廣告DrawEllipse。 reasone你可能不會得到太多的答案是你需要做一些研究,嘗試一些,然後問。 –

+0

林猜測它的硬件,他應該使用COS /罪*半徑繪製圓.. –

回答

2

我不明白你爲什麼要找到圓的面積,把它的半徑,但因爲它似乎你正在使用的WinForms我只想使用Graphics.DrawEllipse方法並使用通過從所需中心減去半徑找到的矩形。

private void button1_Click(object sender, EventArgs e) 
{ 
    int centerX; 
    int centerY; 
    int radius; 

    if (!int.TryParse(textBox2.Text, out centerX)) 
     return; 
    if (!int.TryParse(textBox3.Text, out centerY)) 
     return; 
    if(! int.TryParse(textBox1.Text,out radius)) 
     return; 

    Point center = new Point(centerX, centerY); 

    Graphics paper; 
    paper = pictureBox1.CreateGraphics(); 
    Pen pen = new Pen(Color.Black); 
    getCircle(paper, pen, center, radius); 

} 

private void getCircle(Graphics drawingArea, Pen penToUse, Point center, int radius) 
{ 
    Rectangle rect = new Rectangle(center.X-radius, center.Y-radius,radius*2,radius*2); 
    drawingArea.DrawEllipse(penToUse,rect); 
} 
+0

啊,不錯,這項工程偉大的一切,我只是不允許使用if命令呢。另外,這本即時使用的書很容易混淆,當涉及到他們的問題時,這些章節沒有真正的價值,如果從第1章開始,我一直想使用這些書來防止我的「程序」崩潰。但這是一個非常有用的答案。這是我可以改變成書本要我做的事情。 – user1708501

+0

@ user1708501我剛剛使用if命令將字符串解析爲整數。你可以使用int.Parse,但是如果失敗,它會給出一個錯誤,或者使用不帶If語句的int.TryParse。很高興能有所幫助 –

+0

我只是設法在不同的項目中使用int.TryParse。避免錯誤。再次感謝您 – user1708501