Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
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?
 
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 10-Jan-13 7:26am
Comments
jibesh at 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 at 11-Jan-13 0:26am
   
Absolutely no need. I explained it in my answer, please see.
—SA
jibesh at 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 at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
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();
} 
  Permalink  
v2

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

  Print Answers RSS
0 Maciej Los 245
1 OriginalGriff 210
2 Manfred R. Bihy 190
3 CHill60 180
4 _Amy 155
0 OriginalGriff 7,445
1 Sergey Alexandrovich Kryukov 6,157
2 Maciej Los 3,774
3 Peter Leow 3,448
4 CHill60 2,702


Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 11 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100