0

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?

siamezzze
  • 81
  • 1
  • 5
  • There is an easier way. Use XNA. the learning curve is steep but you'll soon be abstracting things like this out. – Geeky Guy Sep 16 '13 at 19:47
  • If you want something more effecient (and im my opinion, easier) then use [WPF](http://msdn.microsoft.com/en-us//library/ms754130.aspx). – Sam Sep 16 '13 at 19:51
  • 2
    I would stay away from XNA like it's the plague. XNA is no longer being developed and is not being supported under the Windows 8 or the Windows RT environments. http://www.computerandvideogames.com/389018/microsoft-email-confirms-plan-to-cease-xna-support/ – Bill Hatter Sep 16 '13 at 20:00
  • I'll also throw SFML out there for simple 2D graphics – Didaxis Sep 16 '13 at 20:17
  • A lot of things to learn... Ok, WPF seems quite interesting. Thanks, anyway:) – siamezzze Sep 16 '13 at 20:27
  • What about initialising g only once when the image is loaded and then store it as a member variable to be used inside this mouse function? – morishuz Sep 16 '13 at 21:24
  • Yes, I tried that, but nothing happens, image doesn't appear at all. Probably missing something. – siamezzze Sep 16 '13 at 22:12
  • Thanks Sam for WPF suggestion, it helped me a lot. Had to rewrite almost everything, but it solved a set of problems I have faced using Windows.Forms. – siamezzze Sep 17 '13 at 02:03
  • I don't know if this can be counted as an appropriate answer. There is still no information about the proper usage of Graphics class, so people who will find this question in their search result probably won't be satisfied. However, that worked for me. How can I mark this as a good answer? – siamezzze Sep 17 '13 at 02:11

0 Answers0