c# - 在鼠标经过的地方画一条线

标签 c# xna drawing mouse line

基本上,我想在鼠标移动的地方画一条线,就像在绘画中一样,但是,当我每次在 mousePos 上画一个点时,会发生这种情况:

This happens

现在,我需要一些帮助将它变成一条线,它没有间隙或任何奇怪的东西。 感谢您的帮助。

我用来生成线条的代码(这不是图片中的代码!)

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

和线对象:

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

最佳答案

你想要做的是这样的(扩展 pathfinder666 的回答):

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

请记住,这可能看起来有点锯齿状。您可能想使用 DrawArc而是将其平滑一点。这取决于您最终要实现的目标。

关于c# - 在鼠标经过的地方画一条线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13017448/

相关文章:

c# - Android 模拟器没有响应 Xamarin 的 AMD 进程的问题

c# - 当 GridView 的单元格内的按钮调用时,模态面板不会出现

.net - 绘制立方体时出现 OutOfMemory 异常

objective-c - 如何旋转 NSTextView

java - 绘图有问题

c# - '' 没有重载匹配委托(delegate) 'System.EventHandler'

c# - Rhino Mocks - stub 一个单例

c# - 锯齿状的边缘和奇怪的阴影

graphics - 如何在 XNA 中消除模型上的光/阴影效果?

iOS swift - 如何使用触摸事件绘制椭圆