Raskoljnikovic
Raskoljnikovic

Reputation: 99

How to draw this red circles in black circles? C#

I draw dots on panel1 for Dots&Boxes game. I want to draw black circles and red around them but for some reason half of my red circles are drawn well and other half is moved in right. I tried different offsets, but if right side of panel is drawn well then left one is not. 10x9 board; 2x2 board

private void panel1_Paint(object sender, PaintEventArgs e)
    {
        graphics = e.Graphics;

        for (int i = 0; i <= numberOfColumns; i++)
        {
            for (int j = 0; j <= numberOfRows; j++)
            {
                graphics.DrawEllipse(new Pen(Color.Black, 1.75f), i * ((panel1.Width-10)/ numberOfColumns), j * ((panel1.Height-10) / numberOfRows), dotWidth, dotHeight);
                listOfRectanglesF.Add(new RectangleF((i * panel1.Width-10) / numberOfColumns, j * ((panel1.Height - 10) / numberOfRows),dotWidth,dotHeight));

                graphics.FillEllipse(new SolidBrush(Color.Black), i * ((panel1.Width - 10) / numberOfColumns), j * ((panel1.Height - 10) / numberOfRows), dotWidth, dotHeight);
            }
        }
        foreach(RectangleF x in listOfRectanglesF)
        {
            graphics.DrawEllipse(new Pen(Color.Red, 1f), x.X, x.Y, x.Width, x.Height);

        }

Upvotes: 0

Views: 201

Answers (1)

Oguz Ozgul
Oguz Ozgul

Reputation: 7187

You can draw them at the same time in your first for loop and forget about the second for loop (Delete it):

The same calculation is used to find the drawing start point, then, both x and y positions are decreased by 1, width and height are incremented by 2.

Hope this helps.

graphics.FillEllipse(new SolidBrush(Color.Black), i * ((panel1.Width - 10) / numberOfColumns), j * ((panel1.Height - 10) / numberOfRows), dotWidth, dotHeight);
graphics.DrawEllipse(new Pen(Color.Red, 1f), i * ((panel1.Width-10) / numberOfColumns)-1, j * ((panel1.Height - 10) / numberOfRows)-1, dotWidth+2, dotHeight+2);

Upvotes: 2

Related Questions