将鼠标点击传递给线算法

时间:2012-04-06 09:00:31

标签: c#

我为

编写了bresenham算法
  

0<Angular coefficient<1

我对C#中的图形了解不多,我意识到绘制 pixles 我可以使用函数 Fillrectangel 和坐标1 ,1

我想编写我的代码然后在面板上单击鼠标时,在两个位置为我画一条线,从 x0,y0到xEnd,yEnd

所以这是我的代码有异常

enter image description here

空引用异常未处理 对象引用未设置为对象的实例 这个例外符合e.Graphics.FillRectangle(新的SolidBrush(grad1),x,y,1,1);

我认为问题是对象e是空的,我应该新建它但是如何?

如何更正代码以便绘制Line?

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

            private void panel1_Paint(object sender, PaintEventArgs e)
            {
                Line l=new Line();
                l.LineBres(Cursor.Position.X, Cursor.Position.Y, Cursor.Position.X, Cursor.Position.Y);

            }
        }
    }
    public class Line
    {
        System.Windows.Forms.DrawItemEventArgs e;
        Color grad1 = Color.FromArgb(165, 194, 245);
        public void LineBres(int x0, int y0, int xEnd, int yEnd)
        {
            int dx = xEnd - x0;
            int dy = yEnd = y0;
            int p = 2 * dy - dx;
            int twoDy = 2 * dy;
            int twoDyMinusDx = 2 * (dy - dx);
            int x, y;
            if (x0 > xEnd)
            {
                x = xEnd;
                y = yEnd;
                xEnd = x0;
            }
            else
            {
                x = x0;
                y = y0;
            }

            e.Graphics.FillRectangle(new SolidBrush(grad1), x, y, 1, 1);
            while (x < xEnd)
            {
                x++;
                if (p < 0)
                    p += twoDy;
                else
                {
                    y++;
                    p += twoDyMinusDx;
                }
                e.Graphics.FillRectangle(new SolidBrush(grad1), x, y, 1, 1);
            }
        }

    }

1 个答案:

答案 0 :(得分:0)

以下是您需要更改的内容:

为面板添加鼠标点击事件并稍微更改一下您的行代码 - 删除System.Windows.Forms.DrawItemEventArgs e;并使用panel1.CreateGraphics();传递面板图形。这是代码:

private int firstX, firstY;//store coordinates of first click
private bool firstClick = true;
private void panel1_MouseClick(object sender, MouseEventArgs e)
{
    if (firstClick)
    {
        firstX = e.X;
        firstY = e.Y;
        firstClick = false;
    }
    else
    {
        Line l = new Line();
        l.LineBres(firstX, firstY, e.X, e.Y, panel1.CreateGraphics());
        firstClick = true;
    }
}

public class Line
{
    private Color grad1 = Color.FromArgb(165, 194, 245);

    public void LineBres(int x0, int y0, int xEnd, int yEnd, Graphics e)
    {
        int dx = xEnd - x0;
        int dy = yEnd = y0;
        int p = 2*dy - dx;
        int twoDy = 2*dy;
        int twoDyMinusDx = 2*(dy - dx);
        int x, y;
        if (x0 > xEnd)
        {
            x = xEnd;
            y = yEnd;
            xEnd = x0;
        }
        else
        {
            x = x0;
            y = y0;
        }

        e.FillRectangle(new SolidBrush(grad1), x, y, 1, 1);
        while (x < xEnd)
        {
            x++;
            if (p < 0)
                p += twoDy;
            else
            {
                y++;
                p += twoDyMinusDx;
            }
            e.FillRectangle(new SolidBrush(grad1), x, y, 1, 1);
        }
    }
}