首页 新闻 会员 周边 捐助

递归方法画树小程序

0
[已解决问题] 解决于 2017-01-02 19:31

提示错误是:“Form1_Paint”的重载均与委托“System.EventHandler”不匹配 。求老司机帮我看看。。

public partial class Form1 : Form
    {
      
        
        public class MyLine {//定义了线段类包括起始点,终止点,线长,线宽,倾角
            public double x0, y0;
            public double x1,y1;
            public double leng,linesize;
            public double th;
            public MyLine ( double _x0, double _y0, double _x1, double _y1,
                            double _leng, double _linesize, double _th ) {
                x0 = _x0;
                y0 = _y0;
                x1 = _x1;
                y1 = _y1;
                leng = _leng;
                linesize = _linesize;
                th = _th;}}
        public Form1() 
        {
            this.AutoScaleBaseSize = new Size ( 6, 14 );
            this.ClientSize = new Size ( 900, 600 );
            this.Paint += new PaintEventHandler ( this.Form1_Paint );
   this.Click += new System.EventHandler(this.redraw);}
        static void Main()
        {
            Application.Run ( new Form1() );
        }
  private void redraw(object sender,EventArgs e)
  {
   this.Invalidate();
  }
        private void Form1_Paint ( object sender, PaintEventArgs e )
        {
            graphics = e.Graphics ;
            double width = this.Width/2;
            double height = this.Height;
            double leng = 150;
            Queue lines = new Queue();//使用队列用于BFS遍历
            lines.Enqueue ( new MyLine (width,height,width,height-leng,leng,7,-PI/2 ) );
            drawCayleyTree (10,lines );
        }
        private Graphics graphics;
        const double PI = Math.PI;
        double dth1 = 30 * Math.PI / 180;
        double dth2 = 20 * Math.PI / 180;
        double per1 = 0.6;
        double per2 = 0.7;
        Random rnd = new Random();
        void drawCayleyTree ( int n, Queue lines) 
        {
            if ( n == 0 ) 
            {
                return;     
            }
            Queue newlines = new Queue();
            while ( lines.Count > 0 )
            {//一边遍历一遍添加元素
                MyLine line = lines.Dequeue() as MyLine;
                drawLine(line.x0,line.y0,line.x1,line.y1,line.linesize );
                double x0 = line.x1;
                double y0 = line.y1;
                double newlinesize = 0.8 * line.linesize;
                double leng1 = per1 *(rnd.NextDouble()+0.5)*line.leng;
                double leng2 = per2 *(rnd.NextDouble()+0.5)*line.leng;
                double th1 = line.th + dth1*(rnd.NextDouble()+0.4);
                double th2 = line.th - dth2*(rnd.NextDouble()+0.6);
                double x1 = x0 + leng1 * Math.Cos (th1);
                double y1 = y0 + leng1 * Math.Sin (th1);
                double x2 = x0 + leng2 * Math.Cos (th2);
                double y2 = y0 + leng2 * Math.Sin (th2);
                newlines.Enqueue (new MyLine ( x0, y0, x1, y1, leng1, newlinesize,th1));
                newlines.Enqueue (new MyLine ( x0, y0, x2, y2, leng2, newlinesize,th2));
    if (rnd.NextDouble() > 0.7)
    {//随机生成第三棵子树
     double leng3 = 0.6*(rnd.NextDouble()+0.4)*line.leng;
     double th3 = line.th - dth2*(rnd.NextDouble()+0.5); 
     double x3 = x0 + leng3*Math.Cos(th3);
     double y3 = y0 + leng3*Math.Sin(th3);
     newlines.Enqueue(new MyLine(x0,y0,x3,y3,leng3,newlinesize,th3));
    }
            }
        
            drawCayleyTree ( n - 1, newlines );}
            //调用下一层函数}
        void drawLine ( double x0, double y0, double x1, double y1 , double linesize)
        {
            Pen pen = new Pen ( GetColor(), ( float ) linesize );
            graphics.DrawLine ( pen, ( int ) x0, ( int ) y0, ( int ) x1, ( int ) y1 );
        }
          private Color GetColor() 
          {
            return Color.FromArgb ( ( int ) rnd.Next ( 256 ),
                                    ( int ) rnd.Next ( 256 ),
                                    (int) rnd.Next (256));
          }
    }
C#
蓝烟的主页 蓝烟 | 菜鸟二级 | 园豆:206
提问于:2016-12-10 16:30
< >
分享
最佳答案
0

EventHandler的委托方法原型是(object sender,EventArgs e)

你的方法是( object sender, PaintEventArgs e )

奖励园豆:5
Daniel Cai | 专家六级 |园豆:10424 | 2016-12-11 18:57
其他回答(1)
0

不知楼主是否依然关注该贴,向你请教下,你是如何解决  (Form1_Paint”的重载均与委托“System.EventHandler”不匹配)这个问题?

Y.ss | 园豆:202 (菜鸟二级) | 2018-03-22 08:48
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册