首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

求大神 帮忙 抓急

2013-11-23 
求大神 帮忙 捉急啊哪里出错了在textbox中输入坐标 为毛坐标图上没有点·····using Systemusing System.Co

求大神 帮忙 捉急啊
哪里出错了   在textbox中输入坐标 为毛坐标图上没有点·····求大神 帮忙 抓急

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 mianjian
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

            XYLinesFactory.DrawXY(panel1);
            XYLinesFactory.DrawYLine(panel1, 45, 85, 8);
            XYLinesFactory.DrawXLine(panel1,18,4,14);  


        }


        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string[] ss = textBox1.Text.Trim().Split(new char[] { ',' });

                int x = Convert.ToInt32(ss[0]);
                int y = Convert.ToInt32(ss[1]);

                Graphics g = textBox1.CreateGraphics();
                g.FillEllipse(Brushes.Red, new Rectangle(4, 45, x, y));
            }
        }



    }


    class XYLinesFactory
    {
        #region   画出X轴与Y轴
        /// <summary>   
        /// 在任意的panel里画一个坐标,坐标所在的四边形距离panel边52像素   
        /// </summary>   
        /// <param name="pan"></param> 
        public static void DrawXY(Panel pan)
        {
            Graphics g = pan.CreateGraphics();
            //整体内缩move像素   
            float move = 52f;
            float newX = pan.Width - move;
            float newY = pan.Height - move;

            //绘制X轴,   
            PointF px1 = new PointF(move, newY);
            PointF px2 = new PointF(newX, newY);
            g.DrawLine(new Pen(Brushes.Black, 2), px1, px2);
            //绘制Y轴   
            PointF py1 = new PointF(move, move);
            PointF py2 = new PointF(move, newY);

            g.DrawLine(new Pen(Brushes.Black, 2), py1, py2);

        }
        #endregion

        #region
        /// <summary>   
        /// 画出X轴上的分值线,从零开始   
        /// </summary>   
        /// <param name="pan"></param>   
        /// <param name="maxX"></param>   


        /// <param name="len"></param> 

        public static void DrawXLine(Panel pan, float minX, float maxX, int len)
        {
            float move = 50f;
            float LenX = pan.Width - 2 * move;
            float LenY = pan.Height - 2 * move;
            Graphics g = pan.CreateGraphics();
            for (int i = 0; i <= len; i++)    //len等份X轴   
            {
                PointF py1 = new PointF(LenX * i / len + move, pan.Height - move - 4);
                PointF py2 = new PointF(LenX * i / len + move, pan.Height - move);

                string sx = (maxX - (maxX - minX) * i / len).ToString();
                g.DrawLine(new Pen(Brushes.Black, 2), py1, py2);
                g.DrawLine(new Pen(Brushes.Black, 2), py1, py2);
                g.DrawString(sx, new Font("宋体", 8f), Brushes.Black, new PointF(LenX * i / len + move, pan.Height - move / 1.1f));
            }
            Pen pen = new Pen(Color.Black, 1);
            g.DrawString("X轴", new Font("宋体 ", 10f), Brushes.Black, new PointF(pan.Width - move / 1.5f, pan.Height - move / 1.5f));

        }
        #endregion

        #region   画出Y轴上的分值线,从任意值开始
        /// <summary>   
        /// 画出Y轴上的分值线,从任意值开始   
        /// </summary>   
        /// <param name="pan"></param>   
        /// <param name="minY"></param>   
        /// <param name="maxY"></param>   
        /// <param name="len"></param>  

        public static void DrawYLine(Panel pan, float minY, float maxY, int len)
        {
            float move = 50f;
            float LenX = pan.Width - 2 * move;
            float LenY = pan.Height - 2 * move;
            Graphics g = pan.CreateGraphics();
            for (int i = 0; i <= len; i++)    //len等份Y轴   
            {
                PointF px1 = new PointF(move, LenY * i / len + move);
                PointF px2 = new PointF(move + 4, LenY * i / len + move);
                string sx = (maxY - (maxY - minY) * i / len).ToString();
                g.DrawLine(new Pen(Brushes.Black, 2), px1, px2);
                StringFormat drawFormat = new StringFormat();
                drawFormat.Alignment = StringAlignment.Far;


                drawFormat.LineAlignment = StringAlignment.Center;
                g.DrawString(sx, new Font("宋体", 8f), Brushes.Black, new PointF(move / 1.2f, LenY * i / len + move * 1.1f), drawFormat);
            }
            Pen pen = new Pen(Color.Black, 1);
            g.DrawString("Y轴", new Font("宋体 ", 10f), Brushes.Black, new PointF(move / 3, move / 2f));
        }

        #endregion

    }



}




}

哪里出错了   在textbox中输入坐标 为毛坐标图上没有点·····求大神 帮忙 抓急
[解决办法]

public static void DrawXY(Graphics g)
{
    //删除
   // Graphics g = pan.CreateGraphics();
   ...
}
//全部替换成这个参数
public static void DrawXLine(Graphics g, float minX, float maxX, int len);
public static void DrawYLine(Graphics g, float minY, float maxY, int len)

调用

private void panel1_Paint(object sender, PaintEventArgs e)
{
     Graphics g=e.Graphics;
     XYLinesFactory.DrawXY(g);
     XYLinesFactory.DrawYLine(g, 45, 85, 8);
     XYLinesFactory.DrawXLine(g,18,4,14);  
}

[解决办法]

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Draw(e.Graphics);
        }
private void Draw(Graphics g)
{
//绘图代码
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
this.Invalidate();
}

[解决办法]
 点是text1里,输入坐标回车后有,应该将textBox1-->panel1
                int x = Convert.ToInt32(ss[0]);
                int y = Convert.ToInt32(ss[1]); 
                Graphics g =panel1.CreateGraphics();

热点排行