Note that there are some explanatory texts on larger screens.

plurals
  1. PODouble buffering C#
    primarykey
    data
    text
    <p>I'm trying to implement the following method: void Ball::DrawOn(Graphics g);</p> <p>The method should draw all previous locations(stored in a queue) of the ball and finally the current location. I don't know if that matters, but I print the previous locations using g.DrawEllipse(...) and the current location using g.FillEllipse(...).</p> <p>The question is, that as you could imagine there is a lot of drawing to be done and thus the display starts to flicker much. I had searched for a way to double buffer, but all I could find is these 2 ways:</p> <p>1) System.Windows.Forms.Control.DoubleBuffered = true;</p> <p>2) SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);</p> <p>while trying to use the first, I get the an error explaining that from in this method the Property DoubleBuffered is inaccessible due to its protection level. While I can't figure how to use the SetStyle method.</p> <p>Is it possible at all to double buffer while <strong><em>all the access</em></strong> I have is to the Graphics Object I get as input in the method?</p> <p>Thanks in Advance,</p> <p>Edit: I had created the following class</p> <pre><code>namespace doubleBuffer { class BufferedBall : System.Windows.Forms.Form{ private Ball ball; public BufferedBall(Ball ball) { this.ball = ball; } public void DrawOn(Graphics g){ this.DoubleBuffered = true; int num = 0; Rectangle drawArea1 = new Rectangle(5, 35, 30, 100); LinearGradientBrush linearBrush1 = new LinearGradientBrush(drawArea1, Color.Green, Color.Orange, LinearGradientMode.Horizontal); Rectangle drawArea2 = new Rectangle(5, 35, 30, 100); LinearGradientBrush linearBrush2 = new LinearGradientBrush(drawArea2, Color.Black, Color.Red, LinearGradientMode.Vertical); foreach (PointD point in ball.previousLocations) { Pen myPen1; if (num % 3 == 0) myPen1 = new Pen(Color.Yellow, 1F); else if (num % 3 == 1) myPen1 = new Pen(Color.Green, 2F); else myPen1 = new Pen(Color.Red, 3F); num++; myPen1.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid; myPen1.StartCap = System.Drawing.Drawing2D.LineCap.RoundAnchor; myPen1.EndCap = System.Drawing.Drawing2D.LineCap.AnchorMask; g.DrawEllipse(myPen1, (float)(point.X - ball.radius), (float)(point.Y + ball.radius), (float)(2 * ball.radius), (float)(2 * ball.radius)); } if ((ball.Host.ElapsedTime * ball.Host.FPS * 10) % 2 == 0){ g.FillEllipse(linearBrush1, (float)(ball.Location.X - ball.radius), (float)(ball.Location.Y + ball.radius), (float)(2 * ball.radius), (float)(2 * ball.radius)); }else{ g.FillEllipse(linearBrush2, (float)(ball.Location.X - ball.radius), (float)(ball.Location.Y + ball.radius), (float)(2 * ball.radius), (float)(2 * ball.radius)); } } } </code></pre> <p>}</p> <p>and the ball drawOn looks like this:</p> <pre><code>new BufferedBall(this).DrawOn(g); </code></pre> <p>Is that what you meant? because it is still flickering?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload