-1

我创建的方法可以在图片框中创建一个圆圈,但它只会使用矩形的坐标。

我正在尝试创建以下内容,

  • Windows 窗体:
    • 1个图片框
    • 1 个文本框
    • 1 个按钮

textBox 应该用于插入圆的半径。

(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

4

1 回答 1

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);
}  
于 2012-09-29T16:37:18.577 回答