标准panel内坐标系(没有负方向)

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 CoordinateSystem
{
    public partial class Form1 : Form
    {
        int orgX = 20;
        int orgY = 20;//原点距离边框左,下距离
        int move = 20;//格子间隔
        int timerI = 0;
       
        public Form1()
        {
            InitializeComponent();
            timer1.Interval = 1000;
            timer1.Enabled = true;
        }
       
        private void panel1_Paint(object sender, PaintEventArgs e)
        {
           
          
            Draw draw = new Draw();
            draw.DrawX(move,panel1,orgX,orgY);
            draw.DrawY(move, panel1, orgX, orgY);
           
        }
      
    }
    public class Draw
    {
        //X轴方法
        public void DrawX(int move,Panel panel,int orgX,int orgY)//画X轴上分格
        {
           
          
            int a = (panel.Width - 2*orgX) / move;//X轴有多少格
            Graphics g = panel.CreateGraphics();
           
            g.DrawLine(new Pen(Color.Black, 3), new Point(orgX, panel.Height - orgY),//画X轴长线
                                                new Point(panel.Width - orgX, panel.Height - orgY));

            for (int i = 0; i < a; i++)
            {
                g.DrawLine(new Pen(Color.Black, 3), new Point(orgX + move * i, panel.Height - orgY),
                                                    new Point(orgX + move * i, panel.Height -orgY-4));//划分间隔格子
                g.DrawString(i.ToString(), new Font("宋体", 8f), Brushes.Black, orgX + move * i,
                                                                      panel.Height - orgY+4 );//写X轴间隔的数字
            }
            g.DrawString("X轴", new Font("宋体", 10f), Brushes.Black, panel.Width - 25, panel.Height - orgY-20);
        }
        public void DrawY(int move,Panel pan,int orgX,int orgY)
        {
            Graphics g = pan.CreateGraphics();
            g.DrawLine(new Pen(Color.Black, 3), new Point(orgX, pan.Height-orgY),
                                                new Point(orgX, 20));//画Y轴长线
            int a = (pan.Height - 2 * orgY) / move;
            for (int i = 0; i < a; i++)
            {
                g.DrawLine(new Pen(Color.Black, 3), new Point(orgX, pan.Height - orgY - move * i),
                           new Point(orgX + 4, pan.Height - orgY - move * i));
                g.DrawString(i.ToString(), new Font("宋体", 8f), Brushes.Black, orgX - 10,
                    pan.Height - orgY - move * i);
            }
            g.DrawString("Y轴", new Font("宋体", 10f),Brushes.Black, orgX, orgY - 20);
           
        }
    }
}
posted @ 2019-04-17 13:55  奔腾的岩浆  阅读(302)  评论(0编辑  收藏  举报