Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Implement Double Buffering into C# (with GDI+)?

Options
  • 18-11-2004 5:01pm
    #1
    Registered Users Posts: 43,907 ✭✭✭✭


    Am working on an Air Hockey game in GDI+.

    I'm drawing a puck and paddle (two circles :)) over a pictureBox image of a table. My drawings were flickering, but not leaving much of a trail...

    So i looked up on getting rid of that flicker and i found out about double buffering.

    So i implemented that into my class, but now i'm getting a non-flickering image but it's leaving a trail now!

    Any idea what could be wrong with the code below? What i require is for my paddle object to move without any flicker and leaving no trail.

    Any help is much appreciated!

    public frmTable()
    {
    InitializeComponent();

    SetStyle(ControlStyles.UserPaint, true);
    SetStyle(ControlStyles.AllPaintingInWmPaint, true);
    SetStyle(ControlStyles.DoubleBuffer, true);
    }

    private void frmTable_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {
    gameDraw();
    }

    private void gameDraw()
    {
    g = Graphics.FromImage(picTable.Image);

    SolidBrush redBrush = new SolidBrush(Color.FromArgb(255,50,50));
    Pen bluePen = new Pen( Color.Blue, 2 );
    Pen blackPen = new Pen( Color.Black, 2 );

    g.FillEllipse( redBrush, paddle_x, paddle_y, 32, 32 );
    g.DrawEllipse( bluePen, paddle_x, paddle_y, 32, 32 );

    g.FillEllipse( redBrush, puck_x, puck_y, 32, 32 );
    g.DrawEllipse( blackPen, puck_x, puck_y, 32, 32 );

    redBrush.Dispose();
    bluePen.Dispose();
    blackPen.Dispose();
    }

    private void picTable_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    paddle_x = e.X-25;
    paddle_y = e.Y-25;
    }


Comments

  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    I assume you mean by "leaving a trail" that anything that was drawn before is still there?

    Try using g.Clear(Color color) before the drawing methods.


  • Registered Users Posts: 43,907 ✭✭✭✭Basq


    I assume you mean by "leaving a trail" that anything that was drawn before is still there?

    Try using g.Clear(Color color) before the drawing methods.
    Cheers.... yeah, that's what i meant!

    Tried a load of different methods.. including that Color.Clear method. The picture box just fills with that colour specified and doesn't change.

    Below is the code with the code i changed in italics, is this what you had it mind?

    public frmTable()
    {
    InitializeComponent();

    SetStyle(ControlStyles.UserPaint, true);
    SetStyle(ControlStyles.AllPaintingInWmPaint, true);
    SetStyle(ControlStyles.DoubleBuffer, true);
    }

    private void frmTable_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {
    g = Graphics.FromImage(picTable.Image);

    gameDraw();
    }

    private void gameDraw()
    {
    g.Clear(Color.Gold);

    SolidBrush redBrush = new SolidBrush(Color.FromArgb(255,50,50));
    Pen bluePen = new Pen( Color.Blue, 2 );
    Pen blackPen = new Pen( Color.Black, 2 );

    g.FillEllipse( redBrush, paddle_x, paddle_y, 32, 32 );
    g.DrawEllipse( bluePen, paddle_x, paddle_y, 32, 32 );

    g.FillEllipse( redBrush, puck_x, puck_y, 32, 32 );
    g.DrawEllipse( blackPen, puck_x, puck_y, 32, 32 );

    redBrush.Dispose();
    bluePen.Dispose();
    blackPen.Dispose();
    }

    private void picTable_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    paddle_x = e.X-25;
    paddle_y = e.Y-25;
    }


Advertisement