I'm writing a little game using C#. In fact, it just have to draw corolful circles every time user moves a mouse (or just every n milliseconds) according to the mouse location. The problem is, i have to redraw the whole pictureBox every single period of time. I know there are .Invalidate() and .Refresh() options for that, but it seems like I need to re-create Graphics object every single time I need to redraw something, that happens every second.
private void redrawCircles(int distance)
{
prevdist = distance / 5;
g = Graphics.FromImage(pictureBox1.Image);
for (int i = 0; i < n - 1; i++)
{
brushes[i] = brushes[i + 1];
}
brushes[n - 1] = BrushFromDistance(distance);
for (int i = 0; i < n; ++i)
{
g.FillEllipse(brushes[i], startX + i * rad, startY + i * rad, 2 * diag - 2 * i * rad, 2 * diag - 2 * i * rad);
}
g.Dispose();
pictureBox1.Refresh();
}
where g is: public static System.Drawing.Graphics g;
redrawCircles is called in the MouseMove event handler, and i'm planning to cal it in Timer.Tick event handler. So it's called very often. Re-creating Graphics object seems not effective. Do I really need re-creating Graphics object in that situation or there is an easier way?