0

Greetings Everyone,
I need help to plot two graphics (of a different style (thumbnail and rectangular dot) in parallel using same data on an image. This GUI program executes in the following way:

  1. Reads text file contains the data points which have to be plotted.
  2. The program uses click button and the check box.
  3. After clicking up the button the program execution started with the plotting of the First graphic, however when the check box is checked the second graphic plotting started and when the check box unchecked the second graphic plotting stopped. Both of the graphics style using the same text file.
  4. The second graphic actually plots the trajectory of the first graphic. As when the user wants to observe the trajectory so clicks the check box in parallel with the First graphic.
  5. I used the threading technique to read a text file and activates this thread under click button.
    I need help what to do in this case, should I create the separate thread for check box for this parallel plotting
    here I am sharing a block of my code, which I tried to implement but it plots the same graphic (i.e. First graphic) when the check box is operated and this scenario does not meet the program requirement. It plots the rectangular dot but as a single point, It should be plotted continuously till the check box is checked.
    please help!!

    public partial class Form1 : Form
    {
    double w;
    double z;
    Thread T;
    int flag;
    delegate void refresh();
    private System.Drawing.Graphics g;

             public Form1()
    {
        InitializeComponent();
    }
    
      void refresh_PicBox()
              {
                if (pictureBox1.InvokeRequired)
                 {
                   refresh r = new refresh(refresh_PicBox);
                   pictureBox1.Invoke(r);
                  }
          else
               {
                   pictureBox1.Refresh();
                }
            }
    
          public void thread()
                   {
                       StreamReader file = new StreamReader(@"C:\Text.txt");
                       string line;
                      int sc = 100;
    
                    while ((line = file.ReadLine()) != null)
                    {
                        char[] del = new char[] { '\t' };
                        string[] part = line.Split(del, StringSplitOptions.RemoveEmptyEntries);
    
                        w = Convert.ToDouble(part[0]) * sc;
                        z = Convert.ToDouble(part[1]) * sc;
    
                        g = pictureBox2.CreateGraphics();
                        if (flag == 0)
                        {
                            g.DrawImage(new Bitmap(@"C:\interface.png"), Convert.ToInt32(w), Convert.ToInt32(z), 20, 20);
                            Thread.Sleep(50);
                            refresh_PicBox();// invoke for refreshing picture box
    
                        }
                        if (flag == 1)
                        {
                         g.FillRectangle(Brushes.Blue, Convert.ToInt32(w), Convert.ToInt32(z), 9, 9); 
                         Thread.Sleep(50);
                        }
                    }
                }
                private void Form1_Load(object sender, EventArgs e)
                {
                    pictureBox2.Image = new Bitmap(@"C:\Image.jpg");
                }
    
                private void button2_Click(object sender, EventArgs e)
                {
                    T = new Thread(thread);
                    T.Start();
                    flag = 0;
                }
    
                private void checkBox2_CheckedChanged(object sender, EventArgs e)
                {
                    if (checkBox2.Checked)
                    {
                        flag = 1;
                    }
                    else
                    {
                        flag = 0;
                    }
                }
            }

Edited by Sai Nave

2
Contributors
1
Reply
16
Views
2 Months
Discussion Span
Last Post by tinstaafl
0

Here's your code that way it's supposed to look. without proper formatting, it becomes difficult for anyone else to decipher your code.

public partial class Form1 : Form
{
    double w;
    double z;
    Thread T;
    int flag;
    delegate void refresh();
    private System.Drawing.Graphics g;

    public Form1()
    {
        InitializeComponent();
    }
    void refresh_PicBox()
    {
        if (pictureBox1.InvokeRequired)
        {
        refresh r = new refresh(refresh_PicBox);
        pictureBox1.Invoke(r);
        }
        else
        {
            pictureBox1.Refresh();
        }
    }
    public void thread()
    {
        StreamReader file = new StreamReader(@"C:\Text.txt");
        string line;
        int sc = 100;
        while ((line = file.ReadLine()) != null)
        {
            char[] del = new char[] { '\t' };
            string[] part = line.Split(del, StringSplitOptions.RemoveEmptyEntries);
            w = Convert.ToDouble(part[0]) * sc;
            z = Convert.ToDouble(part[1]) * sc;
            g = pictureBox2.CreateGraphics();
            if (flag == 0)
            {
                g.DrawImage(new Bitmap(@"C:\interface.png"), Convert.ToInt32(w), Convert.ToInt32(z), 20, 20);
                Thread.Sleep(50);
                refresh_PicBox();// invoke for refreshing picture box
            }
            if (flag == 1)
            {
                g.FillRectangle(Brushes.Blue, Convert.ToInt32(w), Convert.ToInt32(z), 9, 9); 
                Thread.Sleep(50);
            }
        }
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        pictureBox2.Image = new Bitmap(@"C:\Image.jpg");
    }
    private void button2_Click(object sender, EventArgs e)
    {
        T = new Thread(thread);
        T.Start();
        flag = 0;
    }
    private void checkBox2_CheckedChanged(object sender, EventArgs e)
    {
        if (checkBox2.Checked)
        {
            flag = 1;
        }
        else
        {
            flag = 0;
        }
    }
}

Another thing to keep in mind, you don't gain anything by shortening your variable names. Be verbose so that anyone looking at the code can tell exactly what each variable does, without backtracking to where it's declared.

Edited by tinstaafl

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.