behnam
behnam

Reputation: 1143

Why after drawing circles their colors change?

Why after drawing circles their colors change? , in fact, I draw circles but my problem is that after every time double click, the color of next circles change from blue to the background color.

public Form1()
    {
        InitializeComponent();
        pictureBox1.Paint += new PaintEventHandler(pic_Paint);
    }

    public Point positionCursor { get; set; }
    private List<Point> points = new List<Point>();
    public int circleNumber { get; set; }

    private void pictureBox1_DoubleClick(object sender, EventArgs e)
    {
        positionCursor = this.PointToClient(new Point(Cursor.Position.X - 25, Cursor.Position.Y - 25));

        points.Add(positionCursor);

        pictureBox1.Invalidate();
    }

    private void pic_Paint(object sender, PaintEventArgs e)
    {
        Graphics g = e.Graphics;
        g.SmoothingMode = SmoothingMode.AntiAlias;

        foreach (Point pt in points)
        {
            Pen p = new Pen(Color.Tomato, 2);

            g.FillEllipse(Brushes.Blue, positionCursor.X, positionCursor.Y, 20, 20);

            g.DrawEllipse(p, pt.X, pt.Y, 20, 20);

            p.Dispose();
        }
    }

enter image description here

Upvotes: 4

Views: 83

Answers (2)

I A Khan
I A Khan

Reputation: 8869

change pic_Paint as below

 private void pic_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.SmoothingMode = SmoothingMode.AntiAlias;

            foreach (Point pt in points)
            {
                Pen p = new Pen(Color.Tomato, 2);
                g.DrawEllipse(p, pt.X, pt.Y, 20, 20);
                g.FillEllipse(Brushes.Blue, pt.X, pt.Y, 20, 20);
                p.Dispose();
            }

        }

Upvotes: 0

Gabriel Negut
Gabriel Negut

Reputation: 13960

You're drawing the ellipses correctly, but you always fill only one of them (the last one added, at the cursor's position).

// This is ok
g.DrawEllipse(p, pt.X, pt.Y, 20, 20);

// You should use pt.X and pt.Y here
g.FillEllipse(Brushes.Blue, positionCursor.X, positionCursor.Y, 20, 20);

Upvotes: 3

Related Questions