DrawLine一直在错误的x和y位置绘图

时间:2014-02-08 15:28:55

标签: c# graphics line draw

我刚刚开始学习C#,我有一个我必须的分配

  1. 选择radionbutton'lijn'
  2. 选择一个演员(图片框1,2或3,这里名为actor1 / actor2 / actor3)。
  3. 点击大图片框中的某个位置(占据整个屏幕)
  4. 然而,当我点击屏幕上的某个地方时,它会绘制一条线,其中p1为0,0且p2在某处不合逻辑(每个actor都不同,但总是在同一位置)

    我可能犯了一些大错,我知道我的代码非常糟糕,但请记住我刚开始学习并且遇到了一些困难:)

    如果有人知道如何解决这个问题,

    会很棒!

    相关部分(下面的完整代码):

    int x;
    int y;
    Point p1;
    Point p2;
    List<Ellipses> ellipselijst = new List<Ellipses>();
    List<Textbox> boxlijst = new List<Textbox>();
    List<Lijn> lijnlijst = new List<Lijn>();
    int actorselected;
    int clicklocationx;
    int clicklocationy;
    
    
    private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
    {
        x = e.X;
        y = e.Y;
        Ellipses mooieellipse = new Ellipses(x, y);
        ellipselijst.Add(mooieellipse);
        Textbox mooiebox = new Textbox(x, y);
        boxlijst.Add(mooiebox);
        Lijn mooielijn = new Lijn(p1, p2);
        lijnlijst.Add(mooielijn);
        int clicklocationx = e.X;
        int clicklocationy = e.Y;
        this.Refresh();                       
    }
    
    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        Graphics g = e.Graphics;
        if (radiolijn.Checked == true)
        {
            //Draw vanaf actor 1
            //p1 moet location van picture box zijn. p2 moet de location van mouseclick zijn
            if (actorselected == 1)
            {
                p1 = new Point (actor1.Location.X, actor1.Location.Y);
                p2 = new Point(clicklocationx, clicklocationy);
    
                foreach (Lijn l in lijnlijst)
                {
                    g.DrawLine(new Pen(Brushes.Black), p1, p2);
                }
            }
    
            //Draw vanaf actor 2
            //p1 moet location van picture box zijn. p2 moet de location van mouseclick zijn            
            if (actorselected == 2)
            {
                p1 = new Point(actor2.Location.X, actor2.Location.Y);
                p2 = new Point(clicklocationx, clicklocationy);
                foreach (Lijn l in lijnlijst)
                {
                    g.DrawLine(new Pen(Brushes.Black), p1, p2);
                }
            }
    
            //Draw vanaf actor 3
            //p1 moet location van picture box zijn. p2 moet de location van mouseclick zijn
            if (actorselected == 3)
            {
                p1 = new Point(actor3.Location.X, actor3.Location.Y);
                p2 = new Point(clicklocationx, clicklocationy);
                foreach (Lijn l in lijnlijst)
                {
                    g.DrawLine(new Pen(Brushes.Black), p1, p2);
                }
            }
        }
    }
    
    private void actor1_Click(object sender, EventArgs e)
    {
        actorselected = 1;
    }
    
    private void actor2_Click(object sender, EventArgs e)
    {
        actorselected = 2;
    }
    
    private void actor3_Click(object sender, EventArgs e)
    {
        actorselected = 3;
    }
    

    班级“lijn”:

    public class Lijn
    {
        public Point punt1;
        public Point punt2;
    
        public Lijn(Point p1, Point p2)
        {            
            punt1 = p1;
            punt2 = p2;
        }
    }
    

    完整代码:

    public partial class Formcase : Form
    {
        int aantalactors = 0;
    
        public Formcase()
        {
            InitializeComponent();
            actor1.Visible = false;
            actor2.Visible = false;
            actor3.Visible = false;
        }
    
        private void addactorbtn_Click(object sender, EventArgs e)
        {
            aantalactors++;
    
            if (aantalactors == 1)
            {
                actor1.Visible = true;
            }
    
            if (aantalactors == 2)
            {
                actor2.Visible = true;
            }
    
            if (aantalactors == 3)
            {
                actor3.Visible = true;
            }
        }
    
        int x;
        int y;
        Point p1;
        Point p2;
        List<Ellipses> ellipselijst = new List<Ellipses>();
        List<Textbox> boxlijst = new List<Textbox>();
        List<Lijn> lijnlijst = new List<Lijn>();
        int actorselected;
        int clicklocationx;
        int clicklocationy;
    
        private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            x = e.X;
            y = e.Y;
            Ellipses mooieellipse = new Ellipses(x, y);
            ellipselijst.Add(mooieellipse);
            Textbox mooiebox = new Textbox(x, y);
            boxlijst.Add(mooiebox);
            Lijn mooielijn = new Lijn(p1, p2);
            lijnlijst.Add(mooielijn);
            int clicklocationx = e.X;
            int clicklocationy = e.Y;
            this.Refresh();                       
        }
    
        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            if (radioellipse.Checked == true)
            {
                foreach (Ellipses p in ellipselijst)
                {
                    g.DrawEllipse(new Pen(Brushes.Black), new Rectangle(new Point(p.x - 50, p.y - 50), new Size(100, 100)));
                }
            }
    
            if (radiobtntekst.Checked == true)
            {
                foreach (Textbox t in boxlijst)
                {
                    TextBox txtbox = new TextBox();
                    txtbox.Location = new Point(t.x + 125, t.y + 25);
                    this.Controls.Add(txtbox);
                    txtbox.BringToFront();
                }
            }
            if (radiolijn.Checked == true)
            {               
                //Draw vanaf actor 1
                //p1 moet location van picture box zijn. p2 moet de location van mouseclick zijn
                if (actorselected == 1)
                {
                    p1 = new Point (actor1.Location.X, actor1.Location.Y);
                    p2 = new Point(clicklocationx, clicklocationy);
    
                    foreach (Lijn l in lijnlijst)
                    {
                        g.DrawLine(new Pen(Brushes.Black), p1, p2);
                    }
                }
    
                //Draw vanaf actor 2
                //p1 moet location van picture box zijn. p2 moet de location van mouseclick zijn            
                if (actorselected == 2)
                {
                    p1 = new Point(actor2.Location.X, actor2.Location.Y);
                    p2 = new Point(clicklocationx, clicklocationy);
                    foreach (Lijn l in lijnlijst)
                    {
                        g.DrawLine(new Pen(Brushes.Black), p1, p2);
                    }
                }
    
               //Draw vanaf actor 3
               //p1 moet location van picture box zijn. p2 moet de location van mouseclick zijn
                if (actorselected == 3)
                {
                    p1 = new Point(actor3.Location.X, actor3.Location.Y);
                    p2 = new Point(clicklocationx, clicklocationy);
                    foreach (Lijn l in lijnlijst)
                    {
                        g.DrawLine(new Pen(Brushes.Black), p1, p2);
                    }
                }
            }
        }
    
        private void actor1_Click(object sender, EventArgs e)
        {
            actorselected = 1;
        }
    
        private void actor2_Click(object sender, EventArgs e)
        {
            actorselected = 2;
        }
    
        private void actor3_Click(object sender, EventArgs e)
        {
            actorselected = 3;
        }
    }
    

1 个答案:

答案 0 :(得分:1)

您有两组clicklocation变量 - 一个是全局的,一个是您的鼠标单击处理程序的本地变量。摆脱后者。