C#绘制图片框中的类中的图形,只需单击鼠标

厨师布罗汉

我想从我在单独的类(绘画球)中创建的方法(绘画)绘画图形对象。我只希望在用鼠标左键单击时才将其绘制在图片框中,并且希望将拍摄点存储在列表中。当我尝试下面的代码时,它不会显示。以下是彩弹射击课。

{
    private List<Point> myClick;

    public Paintball()
    {                 
        myClick = new List<Point>();
    }

    public void add(Point location)
    {
        myClick.Add(location);                    
    }

    public void paint(Graphics g, Point point)
    {
        g.FillEllipse(Brushes.Blue, point.X, point.Y, 20, 20);
    }
}

}

这是下面的form1。

namespace AmazingPaintball
{
    public partial class Form1 : Form
    {
        Random positionX = new Random();
        Random positionY = new Random();
        Target einstein;
        int count;
        List<Point> ballList = new List<Point>();
        Paintball gun; 

        public Form1()
        {
            InitializeComponent();
            Point point = new Point(positionX.Next(0, 638), positionY.Next(0, 404));
            einstein = new Target(point);
            ptrEinstein.Location = point;
            gun = new Paintball();               
        }       

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            ptrEinstein.Location = einstein.Move(e.KeyData);
            pictureBox1.Update();
            pictureBox1.Refresh();           
        }




        private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            count++;
            gun.add(e.Location);
            pictureBox1.Refresh();


        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            foreach (var Paintball in ballList)
            {
                gun.paint(e.Graphics, this.PointToClient(Cursor.Position));
                pictureBox1.Refresh();               
            }                        
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Start();
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            pictureBox1.Refresh();
        }       
    }
}

如果您知道必须编辑/创建的内容,请告诉我。谢谢你

J ...

您的原始代码有很多错误。让我们尝试简化您的工作,并解决简单的存储点列表并将其绘制到图片框的问题。

public partial class Form1 : Form
{        
    List<Point> ballList = new List<Point>();        

    public Form1()
    {
        InitializeComponent();
    }

    private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
    {           
        ballList.Add(e.Location);
        pictureBox1.Refresh();
    }

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        foreach (Point pBall in ballList)
        {
            e.Graphics.FillEllipse(Brushes.Blue, pBall.X, pBall.Y, 20, 20);
        }
    }
}

这里有一个列表,我们在点击处理程序中向其添加点击点,并在绘制处理程序中对其进行绘制。一旦您对此感到满意,也许就可以转到程序中的下一个任务,并问一个新的问题,如果您对下一个功能感到困惑。


好的,我有一点时间,让我们看看您的彩弹射击课。我将其重命名,Paintballs因为其中包含许多内容,并且此名称更合适。如果您想将点列表保密,那就没关系。您正在尝试Paint在该类中实现一个方法,但是该方法采用Pointas参数并且不能在该类的任何实例状态上进行操作-这可能不是您想要的。现在考虑:

public class Paintballs
{
    private List<Point> myClick;

    public Paintballs()
    {
        myClick = new List<Point>();
    }

    public void Add(Point location)
    {
        myClick.Add(location);
    }

    public void Paint(Graphics g)
    {
        foreach (Point p in myClick)
        {
            g.FillEllipse(Brushes.Blue, p.X, p.Y, 20, 20);
        }
    }
}

在这里,我们有一个公共Paint方法,该方法会将类中的所有彩弹绘制到传递给它的任何图形实例上。现在,您的表单代码将如下所示:

public partial class Form1 : Form
{
    Paintballs pBalls = new Paintballs();

    public Form1()
    {
        InitializeComponent();
    }

    private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
    {
        pBalls.Add(e.Location);
        pictureBox1.Refresh();
    }

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        pBalls.Paint(e.Graphics);
    }
}

因此,我们通过将绘画方法推入paintballs类本身来简化了表单代码。这使班级负责了解彩弹的外观,数量,位置以及如何将其绘制到Graphics对象上。这是封装职责的步骤1。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章