Drawing a line where the mouse went
c#, drawing, line, mouse, xna
Solution
What your going to want to do is something like this (expanding on pathfinder666's answer):
private Point _lastPoint;
protected void MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
Graphics g = CreateGraphics();
g.LineTo(_lastPoint.X, _lastPoint.Y, e.X, e.Y);
_lastPoint = new Point(e.X, e.Y);
}
Now keep in mind, this might look a little jaggedy. You may want to use DrawArc instead to smooth it out a little. It depends on what you are trying to accomplish in the end.
Problem
Basically, I want to draw a line where the mouse goes, like in paint, but, when I draw a dot over the mousePos every tick, this happens: Now, I need some help turning this into a line, that doesn't have gaps or anything weird. Thanks for helping. The code I'm using to generate the lines (This is not the code for in the picture!) ``` protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); spriteBatch.Begin(); Line newLine = new Line(pixel, point1, point2, 2, Color.White); allLines.Add(newLine); foreach (Line lines in allLines) { lines.Draw(spriteBatch); } spriteBatch.End(); base.Draw(gameTime); } ``` and the line object: ``` public class Line { Texture2D texture; Vector2 point1, point2; float width; Color color; float angle, length; public Line(Texture2D texture, Vector2 point1, Vector2 point2, float width, Color color) { this.texture = texture; this.point1 = point1; this.point2 = point2; this.width = width; this.color = color; angle = (float)Math.Atan2(point2.Y - point1.Y, point2.X - point1.X); length = Vector2.Distance(point1, point2); } public void Draw(SpriteBatch spriteBatch) { spriteBatch.Draw(texture, point1, null, color, angle, Vector2.Zero, new Vector2(length, width), SpriteEffects.None, 0); } } ```