Click here to Skip to main content
15,879,096 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
hi all,

so i have just had a challenge to create a checkerboard 64 equal rectangles/squares alternating in color so i managed to do that (i know my code is probably very ugly) but i technically have done the challenge but being curious i notice when the user resizes the form dragging it the form constantly repaints itself giving almost like a stutter of the image. i was wonderin with the code i wrote maybe i could throw some type of code in to make it only invalidate once the user finishes resizing?

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;



public class checkerboard : Form
{
    public checkerboard()
    {
        Size = new Size(400, 400);
        Text = "CheckerBoard";
        BackColor = Color.Red;
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        Graphics g = e.Graphics;

        int h = DisplayRectangle.Height;
        int w = DisplayRectangle.Width;

        for (int i = 0; i < 8; i = i + 2)

            for (int j = 0; j < 8; j = j + 2)
            {
                Color Black = Color.Black;
                Brush brush = new SolidBrush(Black);
                g.FillRectangle(brush, i * w / 8, j * h / 8, w / 8, h / 8);
            }


        for (int i = 1; i < 8; i = i + 2)
            for (int j = 0; j < 8; j = j + 2)
            {
                Color White = Color.White;
                Brush brush = new SolidBrush(White);
                g.FillRectangle(brush, i * w / 8, j * h / 8, w / 8, h / 8);
            }
        for (int i = 1; i < 8; i = i + 2)

            for (int j = 1; j < 8; j = j + 2)
            {
                Color Black = Color.Black;
                Brush brush = new SolidBrush(Black);
                g.FillRectangle(brush, i * w / 8, j * h / 8, w / 8, h / 8);
            }
        for (int i = 0; i < 8; i = i + 2)
            for (int j = 1; j < 8; j = j + 2)
            {
                Color White = Color.White;
                Brush brush = new SolidBrush(White);
                g.FillRectangle(brush, i * w / 8, j * h / 8, w / 8, h / 8);
            }
                base.OnPaint(e);
               
            }

    static void Main()
    {
        Application.Run(new checkerboard());
    }
}
Posted
Comments
Jibesh 10-Jan-13 16:04pm    
You can subscribe the Form.Resize event in your constructor to notify when your form is resized. i made few more changes to work it better check my solution.
Sergey Alexandrovich Kryukov 11-Jan-13 0:26am    
Absolutely no need. I explained it in my answer, please see.
—SA
Jibesh 11-Jan-13 1:48am    
based on his drawing code he must call the invalidate. situation is when he resizes the forms it takes a new DisplayRectangle and starts drawing the graphics for the new size.
Sergey Alexandrovich Kryukov 11-Jan-13 1:59am    
You know, you are right: somehow, I just missed that only a part of the scene will be invalidated...
(Strange, just a few days ago I called Invalidate from Resize handle myself... :-))
And I have to fix my answer...
Thank you very much for pointing it out.
—SA

1 solution

You can call Invalidate method inside the Form Resize handler. In addition to this move your code from OnPaint to OnPainBackground to avoid flickering. Check the code modifications.
C#
public checkerboard()
{
  Size = new Size(400, 400);
  Text = "CheckerBoard";
  BackColor = Color.Red;
  DoubleBuffered = true;
  Resize += new System.EventHandler(this.Form1_Resize);
}

protected override void OnPaintBackground(PaintEventArgs e)
{
   Graphics g = e.Graphics;

   int h = DisplayRectangle.Height;
   int w = DisplayRectangle.Width;

     for (int i = 0; i < 8; i = i + 2)
     for (int j = 0; j < 8; j = j + 2)
     {
        Color Black = Color.Black;
        Brush brush = new SolidBrush(Black);
        g.FillRectangle(brush, i * w / 8, j * h / 8, w / 8, h / 8);
        brush.Dispose(); // added this line to clear object else this will produce resource leak
     }


    for (int i = 1; i < 8; i = i + 2)
    for (int j = 0; j < 8; j = j + 2)
    {
      Color White = Color.White;
      Brush brush = new SolidBrush(White);
      g.FillRectangle(brush, i * w / 8, j * h / 8, w / 8, h / 8);
       brush.Dispose(); // added this line to clear object else this will produce resource leak
    }
    for (int i = 1; i < 8; i = i + 2)

    for (int j = 1; j < 8; j = j + 2)
    {
      Color Black = Color.Black;
      Brush brush = new SolidBrush(Black);
      g.FillRectangle(brush, i * w / 8, j * h / 8, w / 8, h / 8);
      brush.Dispose(); // added this line to clear object else this will produce resource leak
    }
    for (int i = 0; i < 8; i = i + 2)
    for (int j = 1; j < 8; j = j + 2)
    {
      Color White = Color.White;
      Brush brush = new SolidBrush(White);
      g.FillRectangle(brush, i * w / 8, j * h / 8, w / 8, h / 8);
       brush.Dispose();
     }          
 }
private void Form1_Resize(object sender, EventArgs e)
{
  this.Invalidate();
} 
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900