s00014405

s00014405

  • NA
  • 24
  • 0

Apologies - this has been posted before but i'm at my wits end..

Nov 20 2004 11:04 AM
Sorry all.. this has been posted before but i am truly at my wits end - can anyone suggest how to stop the trail being left (redrawing on the image) on the Picturebox being drawn on with GDI+? I'm implemented double buffering. I've tried using the Color.Clear() method - that just fills the picture box with the specified colour and stays that way. Basically, i think all i need to do is insert the image into the picturebox, draw on it. Then, clear and re-insert the picture over it again to prevent the trail. Am i on the right track? Here's my entire code - any suggestions would be great? Surely, people have used GDI+ to paint on a PictureBox before.. can you tell me where i'm going wrong? Thanks in advance.. sorry about the reposting of this topic but i've to give a brief presentation on what i've done in two weeks and i can't go in there with this problem still evident. using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Collections; using System.ComponentModel; using System.Windows.Forms; namespace AirHockey { /// /// Summary description for Table. /// public class frmTable : System.Windows.Forms.Form { private System.Windows.Forms.PictureBox picTable; private System.Windows.Forms.Label lblPlayer2Score; private System.Windows.Forms.Label lblPlayer1Score; private System.Windows.Forms.Label lblPlayer2Name; private System.Windows.Forms.Label lblPlayer1Name; Timer gameClock; Graphics g; int puck_x; int puck_y; int paddle_x; int paddle_y; /// /// Required designer variable. /// private System.ComponentModel.Container components = null; public frmTable() { InitializeComponent(); SolidBrush redBrush = new SolidBrush(Color.FromArgb(255,50,50)); Pen bluePen = new Pen( Color.Blue, 2 ); Pen blackPen = new Pen( Color.Black, 2 ); DoubleBuffering(); } private void DoubleBuffering() { SetStyle(ControlStyles.UserPaint, true); SetStyle(ControlStyles.AllPaintingInWmPaint, true); SetStyle(ControlStyles.DoubleBuffer, true); } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// /// private void InitializeComponent() { System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmTable)); this.picTable = new System.Windows.Forms.PictureBox(); this.lblPlayer2Score = new System.Windows.Forms.Label(); this.lblPlayer1Score = new System.Windows.Forms.Label(); this.lblPlayer2Name = new System.Windows.Forms.Label(); this.lblPlayer1Name = new System.Windows.Forms.Label(); this.SuspendLayout(); // // picTable // this.picTable.BackColor = System.Drawing.Color.Transparent; this.picTable.Image = ((System.Drawing.Image)(resources.GetObject("picTable.Image"))); this.picTable.Location = new System.Drawing.Point(64, -16); this.picTable.Name = "picTable"; this.picTable.Size = new System.Drawing.Size(848, 720); this.picTable.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage; this.picTable.TabIndex = 0; this.picTable.TabStop = false; this.picTable.Paint += new System.Windows.Forms.PaintEventHandler(this.picTable_Paint); this.picTable.MouseMove += new System.Windows.Forms.MouseEventHandler(this.picTable_MouseMove); // // lblPlayer2Score // this.lblPlayer2Score.Anchor = System.Windows.Forms.AnchorStyles.None; this.lblPlayer2Score.BackColor = System.Drawing.Color.Black; this.lblPlayer2Score.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.lblPlayer2Score.Cursor = System.Windows.Forms.Cursors.Default; this.lblPlayer2Score.FlatStyle = System.Windows.Forms.FlatStyle.System; this.lblPlayer2Score.Font = new System.Drawing.Font("OCR A Extended", 48F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.lblPlayer2Score.ForeColor = System.Drawing.Color.Red; this.lblPlayer2Score.ImageAlign = System.Drawing.ContentAlignment.TopLeft; this.lblPlayer2Score.Location = new System.Drawing.Point(16, 56); this.lblPlayer2Score.Name = "lblPlayer2Score"; this.lblPlayer2Score.Size = new System.Drawing.Size(144, 72); this.lblPlayer2Score.TabIndex = 1; this.lblPlayer2Score.Text = "00"; this.lblPlayer2Score.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // lblPlayer1Score // this.lblPlayer1Score.Anchor = System.Windows.Forms.AnchorStyles.None; this.lblPlayer1Score.BackColor = System.Drawing.Color.Black; this.lblPlayer1Score.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.lblPlayer1Score.FlatStyle = System.Windows.Forms.FlatStyle.System; this.lblPlayer1Score.Font = new System.Drawing.Font("OCR A Extended", 48F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.lblPlayer1Score.ForeColor = System.Drawing.Color.Red; this.lblPlayer1Score.ImageAlign = System.Drawing.ContentAlignment.TopLeft; this.lblPlayer1Score.Location = new System.Drawing.Point(816, 544); this.lblPlayer1Score.Name = "lblPlayer1Score"; this.lblPlayer1Score.Size = new System.Drawing.Size(144, 72); this.lblPlayer1Score.TabIndex = 2; this.lblPlayer1Score.Text = "00"; this.lblPlayer1Score.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // lblPlayer2Name // this.lblPlayer2Name.BackColor = System.Drawing.Color.Transparent; this.lblPlayer2Name.FlatStyle = System.Windows.Forms.FlatStyle.Flat; this.lblPlayer2Name.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.lblPlayer2Name.ForeColor = System.Drawing.Color.Yellow; this.lblPlayer2Name.Location = new System.Drawing.Point(16, 24); this.lblPlayer2Name.Name = "lblPlayer2Name"; this.lblPlayer2Name.Size = new System.Drawing.Size(144, 32); this.lblPlayer2Name.TabIndex = 3; this.lblPlayer2Name.Text = "Player2Name"; this.lblPlayer2Name.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // lblPlayer1Name // this.lblPlayer1Name.BackColor = System.Drawing.Color.Transparent; this.lblPlayer1Name.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.lblPlayer1Name.ForeColor = System.Drawing.Color.Yellow; this.lblPlayer1Name.Location = new System.Drawing.Point(816, 512); this.lblPlayer1Name.Name = "lblPlayer1Name"; this.lblPlayer1Name.Size = new System.Drawing.Size(144, 32); this.lblPlayer1Name.TabIndex = 4; this.lblPlayer1Name.Text = "Player1Name"; this.lblPlayer1Name.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // frmTable // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage"))); this.ClientSize = new System.Drawing.Size(973, 647); this.Controls.Add(this.lblPlayer1Name); this.Controls.Add(this.lblPlayer2Name); this.Controls.Add(this.lblPlayer1Score); this.Controls.Add(this.lblPlayer2Score); this.Controls.Add(this.picTable); this.Cursor = System.Windows.Forms.Cursors.Default; this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D; this.Name = "frmTable"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "Air Hockey"; this.Load += new System.EventHandler(this.frmTable_Load); this.Closed += new System.EventHandler(this.frmTable_Closed); this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.picTable_MouseMove); this.ResumeLayout(false); } #endregion private void frmTable_Closed(object sender, System.EventArgs e) { frmMainMenu MainMenu = new frmMainMenu(); MainMenu.Show(); } //This updates that game variables. private void gameLoop(Object sender, EventArgs e) { //Called so the form will repaint itself. Invalidate(); } private void gameDraw() { Graphics 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(); } public void setName(string pName) { lblPlayer1Name.Text = pName; } private void frmTable_Load(object sender, System.EventArgs e) { gameClock = new Timer(); //Start the game clock to control game speed gameClock.Interval = 15; gameClock.Tick += new EventHandler(gameLoop); gameClock.Start(); // initial dimensions for puck puck_x = 200; puck_y = 200; // initial location for paddle paddle_x = 150; paddle_y = 150; } private void picTable_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) { paddle_x = e.X-5; paddle_y = e.Y-5; } private void gameOver() { gameClock.Stop(); } private void picTable_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { g = e.Graphics; gameDraw(); } } }

Answers (3)