如何在 PictureBox 中绘制带间隔的椭圆

发布于 2024-12-23 04:28:17 字数 3864 浏览 0 评论 0原文

我想使用椭圆进行简单的模拟。在用户界面中,会有一个按钮。用户按下按钮,然后以 1 秒的间隔,将在图片框中绘制椭圆。就像交通灯一样...

我想我需要使用计时器事件,但不知道如何使用。 我怎样才能实现它?

我最诚挚的问候...

CodeWork

public void Button1_Click(Object sender, EventArgs e)
{
   DrawMap ortamcizdir = new DrawMap(p_box_map, bmp, ZoomControl, panel1);
   DrawCell hucrecizdir = new DrawCell (p_box_map, bmp, a, ZoomControl, ZoomKontrolBolen);
}

class DrawCell
{
   for (int z = 0; z < Ortamlar.HucreSayisi; z++)
   {
         Pen Kalem = new Pen(Brushes.CadetBlue);
         Rectangle rect = new Rectangle(((Ortamlar.HUCREKorX[z] / ScaleFactor - Ortamlar.HUCREYaricap[z] / ScaleFactor)
                        * ZoomKontrolCarpan) / ZoomKontrolBolen, (Ortamlar.HUCREKorY[z] / ScaleFactor - Ortamlar.HUCREYaricap[z] / ScaleFactor) *
                        ZoomKontrolCarpan / ZoomKontrolBolen, (2 * Ortamlar.HUCREYaricap[z] / ScaleFactor) * ZoomKontrolCarpan / ZoomKontrolBolen
                        , (2 * Ortamlar.HUCREYaricap[z] / ScaleFactor) * ZoomKontrolCarpan / ZoomKontrolBolen);

         if (Ortamlar.HUCREYZ[z] == "001:400")
         {
                g.DrawString(Ortamlar.HUCREKB[z].ToString(), Font, Brushes.Black, ((Ortamlar.HUCREKorX[z] / ScaleFactor)
                    * ZoomKontrolCarpan) / ZoomKontrolBolen, 600 - (Ortamlar.HUCREKorY[z] / ScaleFactor) *
                    ZoomKontrolCarpan / ZoomKontrolBolen);
                g.MultiplyTransform(new Matrix(1, 0, 0, -1, 0, 600));
                g.DrawEllipse(Kalem, rect);

                g.FillEllipse(Brushes.Wheat, rect);
                g.ResetTransform();

                picturebox1.Image = bmp;
                bmp1 = (Bitmap)picturebox1.Image;
          }
          if (Ortamlar.HUCREYZ[z] == "401:800")
          {
                        g.DrawString(Ortamlar.HUCREKB[z].ToString(), Font, Brushes.Black, ((Ortamlar.HUCREKorX[z] / ScaleFactor)
                    * ZoomKontrolCarpan) / ZoomKontrolBolen, 600 - (Ortamlar.HUCREKorY[z] / ScaleFactor) *
                    ZoomKontrolCarpan / ZoomKontrolBolen);
                        g.MultiplyTransform(new Matrix(1, 0, 0, -1, 0, 600));
                        g.DrawEllipse(Kalem, rect);

                        g.FillEllipse(Brushes.Yellow, rect);

                        g.ResetTransform();
                        picturebox1.Image = bmp;
                        bmp2 = (Bitmap)picturebox1.Image;
          }
          if (Ortamlar.HUCREYZ[z] == "801:120")
          {
                        g.DrawString(Ortamlar.HUCREKB[z].ToString(), Font, Brushes.Black, ((Ortamlar.HUCREKorX[z] / ScaleFactor)
                    * ZoomKontrolCarpan) / ZoomKontrolBolen, 600 - (Ortamlar.HUCREKorY[z] / ScaleFactor) *
                    ZoomKontrolCarpan / ZoomKontrolBolen);
                        g.MultiplyTransform(new Matrix(1, 0, 0, -1, 0, 600));
                        g.DrawEllipse(Kalem, rect);

                        g.FillEllipse(Brushes.Green, rect);
                        g.ResetTransform();

                        picturebox1.Image = bmp;
                        bmp3 = (Bitmap)picturebox1.Image;
          }
          if (Ortamlar.HUCREYZ[z] == "121:161")
          {
                        g.DrawString(Ortamlar.HUCREKB[z].ToString(), Font, Brushes.Black, ((Ortamlar.HUCREKorX[z] / ScaleFactor)
                    * ZoomKontrolCarpan) / ZoomKontrolBolen, 600 - (Ortamlar.HUCREKorY[z] / ScaleFactor) *
                        ZoomKontrolCarpan / ZoomKontrolBolen);
                        g.MultiplyTransform(new Matrix(1, 0, 0, -1, 0, 600));
                        g.DrawEllipse(Kalem, rect);

                        g.FillEllipse(Brushes.Blue, rect);

                        g.ResetTransform();
                        picturebox1.Image = bmp;
                        bmp4 = (Bitmap)picturebox1.Image;
          }
    }
}

I want to make a simple simulation using ellipses. In user interface, there will be a button. User will press the button, then with 1 second interval, ellipses will be drawn to pictureBox. Like a traffic light...

I guess I need to use Timer Event, but Don't know how to.
How can I achieve it?

My best regards...

CodeWork:

public void Button1_Click(Object sender, EventArgs e)
{
   DrawMap ortamcizdir = new DrawMap(p_box_map, bmp, ZoomControl, panel1);
   DrawCell hucrecizdir = new DrawCell (p_box_map, bmp, a, ZoomControl, ZoomKontrolBolen);
}

class DrawCell
{
   for (int z = 0; z < Ortamlar.HucreSayisi; z++)
   {
         Pen Kalem = new Pen(Brushes.CadetBlue);
         Rectangle rect = new Rectangle(((Ortamlar.HUCREKorX[z] / ScaleFactor - Ortamlar.HUCREYaricap[z] / ScaleFactor)
                        * ZoomKontrolCarpan) / ZoomKontrolBolen, (Ortamlar.HUCREKorY[z] / ScaleFactor - Ortamlar.HUCREYaricap[z] / ScaleFactor) *
                        ZoomKontrolCarpan / ZoomKontrolBolen, (2 * Ortamlar.HUCREYaricap[z] / ScaleFactor) * ZoomKontrolCarpan / ZoomKontrolBolen
                        , (2 * Ortamlar.HUCREYaricap[z] / ScaleFactor) * ZoomKontrolCarpan / ZoomKontrolBolen);

         if (Ortamlar.HUCREYZ[z] == "001:400")
         {
                g.DrawString(Ortamlar.HUCREKB[z].ToString(), Font, Brushes.Black, ((Ortamlar.HUCREKorX[z] / ScaleFactor)
                    * ZoomKontrolCarpan) / ZoomKontrolBolen, 600 - (Ortamlar.HUCREKorY[z] / ScaleFactor) *
                    ZoomKontrolCarpan / ZoomKontrolBolen);
                g.MultiplyTransform(new Matrix(1, 0, 0, -1, 0, 600));
                g.DrawEllipse(Kalem, rect);

                g.FillEllipse(Brushes.Wheat, rect);
                g.ResetTransform();

                picturebox1.Image = bmp;
                bmp1 = (Bitmap)picturebox1.Image;
          }
          if (Ortamlar.HUCREYZ[z] == "401:800")
          {
                        g.DrawString(Ortamlar.HUCREKB[z].ToString(), Font, Brushes.Black, ((Ortamlar.HUCREKorX[z] / ScaleFactor)
                    * ZoomKontrolCarpan) / ZoomKontrolBolen, 600 - (Ortamlar.HUCREKorY[z] / ScaleFactor) *
                    ZoomKontrolCarpan / ZoomKontrolBolen);
                        g.MultiplyTransform(new Matrix(1, 0, 0, -1, 0, 600));
                        g.DrawEllipse(Kalem, rect);

                        g.FillEllipse(Brushes.Yellow, rect);

                        g.ResetTransform();
                        picturebox1.Image = bmp;
                        bmp2 = (Bitmap)picturebox1.Image;
          }
          if (Ortamlar.HUCREYZ[z] == "801:120")
          {
                        g.DrawString(Ortamlar.HUCREKB[z].ToString(), Font, Brushes.Black, ((Ortamlar.HUCREKorX[z] / ScaleFactor)
                    * ZoomKontrolCarpan) / ZoomKontrolBolen, 600 - (Ortamlar.HUCREKorY[z] / ScaleFactor) *
                    ZoomKontrolCarpan / ZoomKontrolBolen);
                        g.MultiplyTransform(new Matrix(1, 0, 0, -1, 0, 600));
                        g.DrawEllipse(Kalem, rect);

                        g.FillEllipse(Brushes.Green, rect);
                        g.ResetTransform();

                        picturebox1.Image = bmp;
                        bmp3 = (Bitmap)picturebox1.Image;
          }
          if (Ortamlar.HUCREYZ[z] == "121:161")
          {
                        g.DrawString(Ortamlar.HUCREKB[z].ToString(), Font, Brushes.Black, ((Ortamlar.HUCREKorX[z] / ScaleFactor)
                    * ZoomKontrolCarpan) / ZoomKontrolBolen, 600 - (Ortamlar.HUCREKorY[z] / ScaleFactor) *
                        ZoomKontrolCarpan / ZoomKontrolBolen);
                        g.MultiplyTransform(new Matrix(1, 0, 0, -1, 0, 600));
                        g.DrawEllipse(Kalem, rect);

                        g.FillEllipse(Brushes.Blue, rect);

                        g.ResetTransform();
                        picturebox1.Image = bmp;
                        bmp4 = (Bitmap)picturebox1.Image;
          }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半边脸i 2024-12-30 04:28:17

我不太明白你所问的目的,但我猜你可以通过阅读这些链接来接近。

第一篇将引导您了解有关计时器控件的大部分知识。

第二个 将让您了解绘制椭圆的方法。

关于如何构建代码,简单地说,我会得到启动计时器的按钮,然后timerElapsed 事件将控制图片框中椭圆的创建,每1 秒一次。

如需更多解释,我认为最好将一些代码放在一起,以便我们更好地理解您的意图。那么我们或许可以提供更多帮助。希望这会有所帮助。

[edit1]

这将创建省略号,直到计时器停止,您可以添加一个按钮来停止计时器1:

public void Button1_Click(Object sender, EventArgs e)
{
    timer1.Interval = 1000;
    timer1.Start();
}

private void timer1_Tick(object sender, EventArgs e)
{
    DrawMap ortamcizdir = new DrawMap(p_box_map, bmp, ZoomControl, panel1);
    DrawCell hucrecizdir = new DrawCell (p_box_map, bmp, a, ZoomControl, ZoomKontrolBolen);
}

不要忘记您需要将计时器拖到设计上,然后双击勾选< 属性窗格上的 /strong> 事件。

这个答案认为 DrawCell 类没有任何问题。

[edit2]

我正在思考我发布的内容,这会创建省略号,直到计时器停止。因此,您可能需要添加一个 while 语句来控制绘制多少个椭圆。检查此链接以查看其他方法来实现这一点。

通过这种方式,您可以设置要绘制的椭圆的数量,这是一个示例,但有很多方法可以实现它。

//create this field and property before the constructor
public int counter;
public int Counter
{
    get { return counter; }
    set { counter = value; }
}

//button click sets the interval to 1sec, starts timer and sets the int Counter to 0
private void button1_Click(object sender, EventArgs e)
{
    timer1.Interval = 1000;
    timer1.Start();
    Counter = 0;
}

//the tick event iterates the Counter property +1 everytime the timer advances 1sec and while Counter is under 5 for example it creates those objects
private void timer1_Tick(object sender, EventArgs e)
{
    Counter++;
    while (counter < 5)
    {
        DrawMap ortamcizdir = new DrawMap(p_box_map, bmp, ZoomControl, panel1);
        DrawCell hucrecizdir = new DrawCell (p_box_map, bmp, a, ZoomControl, ZoomKontrolBolen);
    }
}

I dont quite understand the purpose of what you're asking, but im guessing you can get close by reading through these links.

The first will run you through most of what you need to know about the timer control.

The second will let you in on ways to draw the ellipses.

About how to structure your code, well simply put, i'd get the button to start the timer, then the timerElapsed event would control the creation of ellipses in the picture box, every 1second.

For more explanations, i think it would be best if you put some code together so we can understand better what your intentions are. Then we might be able to help out more. Hope this helps though.

[edit1]

This will create ellipses untill the timer stops, you can add a button to stop timer1:

public void Button1_Click(Object sender, EventArgs e)
{
    timer1.Interval = 1000;
    timer1.Start();
}

private void timer1_Tick(object sender, EventArgs e)
{
    DrawMap ortamcizdir = new DrawMap(p_box_map, bmp, ZoomControl, panel1);
    DrawCell hucrecizdir = new DrawCell (p_box_map, bmp, a, ZoomControl, ZoomKontrolBolen);
}

Dont forget you need to drag the timer onto the design, then double-click the Tick event on the property pane.

This answer considers that nothing is wrong with the DrawCell class.

[edit2]

I was thinking about what i posted and this would create ellipses untill the timer stops. So you might want to add a while statement to control how many ellipses to draw. Check this link to see other ways to implement that.

This way you can set the amount of ellipses to draw, this is an example, but there are plenty ways to pull it off.

//create this field and property before the constructor
public int counter;
public int Counter
{
    get { return counter; }
    set { counter = value; }
}

//button click sets the interval to 1sec, starts timer and sets the int Counter to 0
private void button1_Click(object sender, EventArgs e)
{
    timer1.Interval = 1000;
    timer1.Start();
    Counter = 0;
}

//the tick event iterates the Counter property +1 everytime the timer advances 1sec and while Counter is under 5 for example it creates those objects
private void timer1_Tick(object sender, EventArgs e)
{
    Counter++;
    while (counter < 5)
    {
        DrawMap ortamcizdir = new DrawMap(p_box_map, bmp, ZoomControl, panel1);
        DrawCell hucrecizdir = new DrawCell (p_box_map, bmp, a, ZoomControl, ZoomKontrolBolen);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文