Skip to content

Commit 84987ca

Browse files
author
jwthomson
committed
Add double buffering
Change the way rendering is done to facilitate double buffering
1 parent 75e942b commit 84987ca

File tree

2 files changed

+46
-33
lines changed

2 files changed

+46
-33
lines changed

Maze/Form1.Designer.cs

Lines changed: 13 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Maze/Form1.cs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,49 @@ namespace Jthomson.Maze
1212
{
1313
public partial class Form1 : Form
1414
{
15+
BufferedPanel displayPanel;
16+
Maze m;
17+
1518
public Form1()
1619
{
1720
InitializeComponent();
21+
displayPanel = new BufferedPanel();
22+
displayPanel.Anchor = (
23+
AnchorStyles.Top |
24+
AnchorStyles.Bottom |
25+
AnchorStyles.Left |
26+
AnchorStyles.Right
27+
);
28+
displayPanel.Location = new System.Drawing.Point(12, 84);
29+
displayPanel.Size = new System.Drawing.Size(504, 507);
30+
displayPanel.Paint += (sender, e) =>
31+
{
32+
if (m != null)
33+
{
34+
m.Draw(e.Graphics, new Pen(Color.Black));
35+
}
36+
};
37+
this.Controls.Add(displayPanel);
1838
}
1939

20-
private void btnDraw_Click(object sender, EventArgs e)
40+
private void btnUpdate_Click(object sender, EventArgs e)
2141
{
2242
int rows = trkRows.Value;
2343
int cols = trkCols.Value;
24-
Maze m = new Maze(cols, rows);
44+
m = new Maze(cols, rows);
2545
m.Generate();
2646
m.CellSize = 10;
27-
pctDisplay.CreateGraphics().Clear(pctDisplay.BackColor);
28-
m.Draw(pctDisplay.CreateGraphics(), new Pen(Color.Black));
47+
displayPanel.Invalidate();
48+
49+
}
50+
51+
private class BufferedPanel : Panel
52+
{
53+
public BufferedPanel(): base()
54+
{
55+
this.DoubleBuffered = true;
56+
this.ResizeRedraw = true;
57+
}
2958
}
3059
}
3160
}

0 commit comments

Comments
 (0)