Skip to main content
 首页 » 编程设计

C#通过单击鼠标在图片框中的类中绘制图形

2024年02月06日11wayfarer

我想通过在单独的类 (Paintball) 中创建的方法 (paint) 来绘制图形对象。我希望它仅在我用鼠标左键单击时才在图片框中绘制,并且我希望将拍摄的点存储在列表中。当我尝试下面的代码时,它不会射击。下面是彩弹射击类。

{ 
    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(); 
        }        
    } 
} 

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

请您参考如下方法:

您的原始代码有很多错误。让我们尝试简化您正在做的事情并解决简单地存储点列表并将它们绘制到图片框的问题。

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 方法,但它采用 Point 作为参数并且不对类的任何实例状态进行操作 - 这可能不是你想要什么。现在考虑:

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); 
        } 
    } 
} 

这里我们有一个公共(public)的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); 
    } 
} 

因此,我们通过将绘画方法插入彩弹类本身来简化表单代码。这使得该类负责了解彩弹的外观、数量、位置以及如何将它们绘制到 Graphics 对象。这是封装责任的第一步。