Hey all,
Been working on a project for a bit that has a pictureBox set to re-draw on a timer. Essentially, it draws a flashing black and white square on one side of the screen and a reversing checkerboard (black checks to white and white checks to black) on the other side. The timer triggers either every half-second or every quarter-second. What should ideally be happening is that the checkerboard reverses at the exact same time as the square on the other side changes from white-to-black (or vice versa). However, I am drawing the checkerboard programmatically (one check at a time) to allow for different check densities, and I think the checkerboard ends up drawing just slightly after the square flips (maybe by 20 milliseconds). This is a very time-sensitive program, so any delay between the square and the checkerboard is unacceptable. Is there a way to ensure that everything in the pictureBox is drawn at the same time? I tried doubleBuffered, but I think I'm still getting a delay. Thanks in advance!
Been working on a project for a bit that has a pictureBox set to re-draw on a timer. Essentially, it draws a flashing black and white square on one side of the screen and a reversing checkerboard (black checks to white and white checks to black) on the other side. The timer triggers either every half-second or every quarter-second. What should ideally be happening is that the checkerboard reverses at the exact same time as the square on the other side changes from white-to-black (or vice versa). However, I am drawing the checkerboard programmatically (one check at a time) to allow for different check densities, and I think the checkerboard ends up drawing just slightly after the square flips (maybe by 20 milliseconds). This is a very time-sensitive program, so any delay between the square and the checkerboard is unacceptable. Is there a way to ensure that everything in the pictureBox is drawn at the same time? I tried doubleBuffered, but I think I'm still getting a delay. Thanks in advance!
C#:
private void drawBoard(Graphics graphics, int checknum, bool dark, bool left, int tickCount)
{
width = (sWidth / 2) / checknum;
height = (sWidth / 2) / checknum;
// Checks for left or right side
if (left == true)
{
// Draw checkerboard for left side
for (int i = 0; i < checknum; i++)
{
dark = !dark;
for (int j = 0; j < checknum; j++)
{
dark = !dark;
if (dark == true)
{
// This is bright
brush = whiteBrush;
}
else
{
// This is dark
brush = blackBrush;
}
// Checkerboard left edge
x = width * i;
y = (height * j) + (sHeight - (sWidth / 2)) / 2;
// Draw rectangle to screen.
graphics.DrawRectangle(blackPen, x, y, width, height);
graphics.FillRectangle(brush, x, y, width, height);
}
}
}
else
{
// Draw checkerboard for right side
for (int i = 0; i < checknum; i++)
{
dark = !dark;
for (int j = 0; j < checknum; j++)
{
dark = !dark;
if (dark == true)
{
// This is bright
brush = whiteBrush;
}
else
{
// This is dark
brush = blackBrush;
}
// Checkerboard right edge
x = width * i + (sWidth / 2);
y = (height * j) + (sHeight - (sWidth / 2)) / 2;
// Draw rectangle to screen.
graphics.DrawRectangle(blackPen, x, y, width, height);
graphics.FillRectangle(brush, x, y, width, height);
}
}
}
}
// Draw flashing rectangle
// NOTE! We are moving this to later as a check! Possibility that the checkerboard creation is taking such a long time
// that flashing rectangle was being drawn FAR before checkerboard. May need to find a better way to improve checkerboard
// creation... try to switch to a simple bitmap that we can flip and tile as needed?
if (dark == true)
{
fill = whiteBrush;
}
else
{
fill = blackBrush;
}
graphics.DrawRectangle(blackPen, trig);
graphics.FillRectangle(fill, trig);
}