Bug when drawing rectangles in my Paint Program

。_饼干妹妹 提交于 2019-12-02 19:57:35

问题


public partial class Form1 : Form
{
    Point downPoint , upPoint;
    List<Shapes> shapes = new List<Shapes>();
    public ShapesEnum shapeType;

    public Form1()
    {
        InitializeComponent();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        foreach (var s in shapes)
            s.Draw(e.Graphics);
    }

    protected override void OnMouseDown(MouseEventArgs e)
    {
        downPoint = e.Location;
    }

    protected override void OnMouseUp(MouseEventArgs e)
    {
        base.OnMouseUp(e);
        upPoint = e.Location;
        CreateShape();
        this.Invalidate();
    }

    private void CreateShape()
    {
        if (shapeType == ShapesEnum.Circle)
        {
            Rectangle rect = new Rectangle(downPoint.X, downPoint.Y, upPoint.X - downPoint.X, upPoint.Y - downPoint.Y);
            Ellipse ellipse = new Ellipse() { rect = rect };
            shapes.Add(ellipse);
        }

        else if (shapeType == ShapesEnum.Rectangle)
        {
            Rectangle rect = new Rectangle(downPoint.X, downPoint.Y, upPoint.X - downPoint.X, upPoint.Y - downPoint.Y);
            Rect rectangle = new Rect() { rect = rect };
            shapes.Add(rectangle);
        }

        else if (shapeType == ShapesEnum.Line)
        {
            Point pt1 = new Point (upPoint.X, upPoint.Y);
            Point pt2 = new Point (downPoint.X, downPoint.Y);

            Ln rectangle = new Ln() { PointUp = pt1, PointDown = pt2 };
            shapes.Add(rectangle);
        }
        lblStatus.Text = string.Format("dn:{0} up:{1} ct:{2}", downPoint, upPoint, shapes.Count());
    }

    private void btnCircle_Click(object sender, EventArgs e)
    {
        shapeType = ShapesEnum.Circle;
    }

    private void btnRectangle_Click(object sender, EventArgs e)
    {
        shapeType = ShapesEnum.Rectangle;
    }

    private void btnLine_Click(object sender, EventArgs e)
    {
        shapeType = ShapesEnum.Line;
    }

}

This is currently the main part of my paint program.

public enum ShapesEnum { Circle, Rectangle, Line }
abstract class Shapes
{
    public Rectangle rect { get; set; }
    public Color color { get; set; }
    public bool isFilled { get; set; }
    public abstract void Draw(Graphics g);
}

class Ellipse : Shapes
{
    public override void Draw(Graphics g)
    {
        g.DrawEllipse(Pens.Black, rect);
    }
}

class Rect : Shapes
{
    public override void Draw(Graphics g)
    {
        g.DrawRectangle(Pens.Black, rect);
    }
}

class Ln : Shapes
{
    public Point PointUp { get; set; }
    public Point PointDown { get; set; }

    public override void Draw(Graphics g)
    {
        g.DrawLine(Pens.Black, PointUp, PointDown);
    }
}

This is my class that is used for inheritance on the shapes. Depending on the shape, one of the classes will be called.


Drawing ovals works fine. Same goes for drawing lines. However, there is a bug when it comes to drawing rectangles. If I put my mouse and drag and let go from left up to bottom right, this will work. However, if I go another direction it is invisible on the form but the shapes.Count() still adds towards the list of shapes.

What caused this bug?


回答1:


Try something like this in order to have the start and end points the right way:

int x0 = Math.Min(upPoint.X, downPoint.X);
int y0 = Math.Min(upPoint.Y, downPoint.Y);
Point upperLeft = new Point(x0, y0);

int x1 = Math.Max(upPoint.X, downPoint.X);
int y1 = Math.Max(upPoint.Y, downPoint.Y);
Point lowerRight = new Point(x1, y1);

And

Rectangle rect = new Rectangle(x0, y0, x1 - x0, y1 - y0);


来源:https://stackoverflow.com/questions/22620215/bug-when-drawing-rectangles-in-my-paint-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!