C#通过鼠标点击从图片框中的类绘制图形(C# paint graphics from class in picturebox with mouse click)

我想从我在一个单独的类(Paintball)中创建的方法(paint)中绘制一个图形对象。 我希望它只在我用鼠标左键单击时才能在图片框中绘画,我希望我拍摄的点存储在列表中。 当我尝试下面的代码时,它不会拍摄。 下面是Paintball类。

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

}

这是下面的表格1。

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

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

I want to paint a graphics object from a method (paint) I created in a separate class (Paintball). I want it to paint in a picturebox only when I left-click with my mouse and I want the point where I shoot to be stored in a List. When I try the code below, it doesn't shoot. Below is the class Paintball.

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

}

This is form1 below.

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

Please let me know if you know what has to be edited/created. Thank You

最满意答案

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

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

这里我们有一个公共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对象。 这是封装责任的第1步。

Your original code has many mistakes. Let's try to simplify what you are doing and tackle simply storing a list of points and drawing them to the picturebox.

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

Here we have a list, we add the click points to it in the click handler and paint them in the paint handler. Once you get comfortable with this, perhaps move to the next task in your program and ask a new question if you get stuck with the next feature.


Ok, I've got a bit of time, so let's look at your paintball class. I've renamed it Paintballs since it contains many of them and this name is more appropriate. If you want to keep the list of points private that's ok. You are trying to implement a Paint method in the class, but it takes a Point as argument and does not operate on any of the class's instance state - this probably isn't what you want. Consider now :

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

Here we have a public Paint method that will draw all of the paintballs in the class to any graphics instance you pass to it. Now your form code would look like :

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

So we've simplified the form code by pushing the painting method into the paintballs class itself. This makes the class responsible for knowing what the paintballs look like, how many there are, where they are, and how to draw them to a Graphics object. This is step 1 in encapsulating responsibility.

C#通过鼠标点击从图片框中的类绘制图形(C# paint graphics from class in picturebox with mouse click)

我想从我在一个单独的类(Paintball)中创建的方法(paint)中绘制一个图形对象。 我希望它只在我用鼠标左键单击时才能在图片框中绘画,我希望我拍摄的点存储在列表中。 当我尝试下面的代码时,它不会拍摄。 下面是Paintball类。

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

}

这是下面的表格1。

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

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

I want to paint a graphics object from a method (paint) I created in a separate class (Paintball). I want it to paint in a picturebox only when I left-click with my mouse and I want the point where I shoot to be stored in a List. When I try the code below, it doesn't shoot. Below is the class Paintball.

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

}

This is form1 below.

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

Please let me know if you know what has to be edited/created. Thank You

最满意答案

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

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

这里我们有一个公共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对象。 这是封装责任的第1步。

Your original code has many mistakes. Let's try to simplify what you are doing and tackle simply storing a list of points and drawing them to the picturebox.

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

Here we have a list, we add the click points to it in the click handler and paint them in the paint handler. Once you get comfortable with this, perhaps move to the next task in your program and ask a new question if you get stuck with the next feature.


Ok, I've got a bit of time, so let's look at your paintball class. I've renamed it Paintballs since it contains many of them and this name is more appropriate. If you want to keep the list of points private that's ok. You are trying to implement a Paint method in the class, but it takes a Point as argument and does not operate on any of the class's instance state - this probably isn't what you want. Consider now :

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

Here we have a public Paint method that will draw all of the paintballs in the class to any graphics instance you pass to it. Now your form code would look like :

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

So we've simplified the form code by pushing the painting method into the paintballs class itself. This makes the class responsible for knowing what the paintballs look like, how many there are, where they are, and how to draw them to a Graphics object. This is step 1 in encapsulating responsibility.