学习dotnet第五课后作出的绘图程序

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MyDrawCircles
{
    public partial class DrawMyCircles : Form
    {
        private DrawArguss argu = new DrawArguss();
        private Graphics gra;

        public DrawMyCircles()
        {
            InitializeComponent();
        }


        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void selectColor_CheckedChanged(object sender, EventArgs e)
        {

            colorR.Enabled = selectColor.Checked;
            colorB.Enabled = selectColor.Checked;
            colorG.Enabled = selectColor.Checked;

        }

        private void ranColor_CheckedChanged(object sender, EventArgs e)
        {
            colorR.Enabled = false;
            colorG.Enabled = false;
            colorB.Enabled = false;
        }

  

        private void numDeepThis_ValueChanged(object sender, EventArgs e)
        {
            argu.numDeep = (int)numDeepThis.Value;

        }

        private void numCircleCount_ValueChanged(object sender, EventArgs e)
        {
            argu.numCircleCount = (int)numCircleCount.Value;

        }

        private void numRaidus_ValueChanged(object sender, EventArgs e)
        {
            argu.radiusRateOfTwoCircle = (float)numRaidus.Value / 100;

        }


        //g开始绘图
        private void drawGraphics( Graphics g )
        {
            g.Clear(Color.White);
            g.TranslateTransform(pnlDrawCircle.Width / 2, pnlDrawCircle.Height / 2);
            //g.RotateTransform(360 / argu.numCircleCount);
            float rr = Math.Min(pnlDrawCircle.Width / 2, pnlDrawCircle.Height / 2);
            rr = rr / 4;
            PointF origin = new PointF( 0, 0 );
            drawPic( origin, rr, argu.numDeep);

        }

        #region

        private void drawPic( PointF centers, float raidus, int nn )
        {
            if (ranColor.Checked)
            {
                randomPen();
            }
            else
            {
                int r, g, b;
                r = (int)colorR.Value;
                g = (int)colorG.Value;
                b = (int)colorB.Value;
                Color clr = Color.FromArgb(r, g, b);
                Pen newColorPen = new Pen(clr);
                argu.myPen = newColorPen;

            }

            DrawCircle(centers, raidus);

            PointF pfs;
            float angle;

            if (nn == 0)
            {
                return;

            }

            for (int i = 0; i < argu.numCircleCount; i++)
            {
                //angle 2 * ( 360 / 4)  = 2 * 90= 180
                angle = i * (360 / argu.numCircleCount);

                pfs = getPointCircle(centers, raidus * 2, angle);

                DrawCircle(pfs, (float)raidus * (argu.radiusRateOfTwoCircle) );

                drawPic(pfs, (float)raidus * (argu.radiusRateOfTwoCircle), nn - 1 );

            }

        } // drawPic end

        private void DrawCircle(PointF center, float raidus)
        {
            float leftx = center.X - raidus;
            float lefty = center.Y - raidus;

            gra.DrawEllipse(argu.myPen, leftx, lefty, raidus * 2, raidus * 2);
            //gra.RotateTransform(360 / argu.numCircleCount);                 // this is add by myself


        }

        private PointF getPointCircle( PointF center, float raidus, float angle )
        {
            PointF pf = new PointF();
            pf.X = (float)(center.X + raidus * Math.Cos( Math.PI * angle / 180) );
            pf.Y = (float) ( center.Y + raidus * Math.Sin( Math.PI * angle / 180 ));

            return pf;

        }

        //随机颜色的画笔
        private void randomPen()
        {
            Random ran = new Random();
            int r, g, b;
            r = ran.Next(0, 255);
            g = ran.Next(0, 255);
            b = ran.Next(0, 255);

            Color clr = Color.FromArgb(r, g, b);
            argu.myPen = new Pen(clr);

                       
        }


        //在面板中颜色随时显示
        private void showColor()
        {
            int r, g, b;
            r = (int)colorR.Value;
            g = (int)colorG.Value;
            b = (int)colorB.Value;
            Color clr = Color.FromArgb(r, g, b);
            pnlShowColor.BackColor = clr;
        }

        #endregion


    

        private void pnlShowColor_Paint(object sender, PaintEventArgs e)
        {
            showColor();
        }

        private void colorR_ValueChanged(object sender, EventArgs e)
        {
            showColor();
        }

        private void colorG_ValueChanged(object sender, EventArgs e)
        {
            showColor();
        }

        private void colorB_ValueChanged(object sender, EventArgs e)
        {
            showColor();
        }

      //把事件和代码连接起来
       
        private void btnDrawPic_Click(object sender, EventArgs e)
        {
            gra = pnlDrawCircle.CreateGraphics();
            drawGraphics( gra );
            gra.Dispose();
        }

    
    }
}

 

 

附:  DrawArguss 类

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace MyDrawCircles
{
    public class DrawArguss
    {
        public Pen myPen = Pens.Blue;
        public int numDeep = 4;
        public int numCircleCount = 4;
        public float radiusRateOfTwoCircle = 0.3f;

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值