Growing line from a point in C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shimajavar
    New Member
    • Oct 2007
    • 17

    Growing line from a point in C#

    Hi every one

    I have really got stuck in writting a piece of code.I suppose to create a form with a button on it. when ever button is clicked, a line which is stated from a certain point of the form(200,200) should grow upward.
    its the code i have written
    namespace ConsoleApplicat ion1
    {
    class Program :Form
    {
    int x=202;
    int y=202;
    public Program()
    {
    this.Size = new Size(400, 400);
    this.BackColor = Color.Wheat;
    this.StartPosit ion = FormStartPositi on.CenterScreen ;
    Button b1 = new Button();
    this.Controls.A dd(b1);
    b1.Click+=new EventHandler(b1 _Click);


    }
    protected override void OnPaint(PaintEv entArgs e)
    {
    Graphics g = e.Graphics;
    Brush brush = new SolidBrush(Colo r.Black);
    Pen pen = new Pen(brush, 4);
    g.DrawLine(pen, 200, 200, 200, 201);


    }
    protected void b1_Click(object sender, EventArgs e)
    {

    x = 200;
    y = 20;

    }


    button doesn't work! any suggestions??
    thank u in advanced
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    There are a few logical/coding errors here.

    Lets start with your button click:
    Code:
    protected void b1_Click(object sender, EventArgs e)
    {
    x = 200;
    y = 20;
    }
    You're setting the adsolute value of your variables instead of incrementing them.
    Try:
    Code:
    protected void b1_Click(object sender, EventArgs e)
    {
       y = y-20;
    }

    Now onto your paint function:
    Code:
    protected override void OnPaint(PaintEventArgs e)
    {
    Graphics g = e.Graphics;
    Brush brush = new SolidBrush(Color.Black);
    Pen pen = new Pen(brush, 4);
    g.DrawLine(pen, 200, 200, 200, 201);
    }
    You are drawing the same line everytime, at no point do you use your x,y values that you are storing.
    Try this:
    Code:
    protected override void OnPaint(PaintEventArgs e)
    {
    Graphics g = e.Graphics;
    Brush brush = new SolidBrush(Color.Black);
    Pen pen = new Pen(brush, 4);
    g.DrawLine(pen, 200, 200, x,y);
    }

    That should be enough to get you started.

    Comment

    • shimajavar
      New Member
      • Oct 2007
      • 17

      #3
      Originally posted by Plater
      There are many logical/coding falicies here.

      Lets start with your button click:
      I tried threads as well, do u wantto take a look at the code?

      class Program :Form
      {
      int x1=202;
      int y1=202;

      int y2;
      private Thread animationThread ;

      public Program()
      {
      this.Size = new Size(400, 400);
      this.BackColor = Color.Wheat;
      this.StartPosit ion = FormStartPositi on.CenterScreen ;
      Button b1 = new Button();
      this.Controls.A dd(b1);
      b1.Click+=new EventHandler(b1 _Click);
      animationThread = new Thread(new ThreadStart(gro wingLine));

      }
      protected override void OnPaint(PaintEv entArgs e)
      {
      Graphics g = e.Graphics;
      Brush brush = new SolidBrush(Colo r.Black);
      Pen pen = new Pen(brush, 4);
      g.DrawLine(pen, 200, 200, x1, y2);


      }
      protected void b1_Click(object sender, EventArgs e)
      {

      animationThread .Start();
      }

      public void growingLine()
      {
      y1 = y2;
      y2++;
      Invalidate();
      Thread.Sleep(40 );
      }

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Sorry, I was in the middle of editing my post with the relevant information, please scroll up and have a look.

        Comment

        • shimajavar
          New Member
          • Oct 2007
          • 17

          #5
          Originally posted by Plater
          Sorry, I was in the middle of editing my post with the relevant information, please scroll up and have a look.
          I tried ur code, actually when i execute it, a line(200,200,20 0,180) is created.Program doesn't go throght the "b1_click" method

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            If it drew the line up to 180, then it performed the button click.

            Is the line supposed to GROW or MOVE? I gave you code for GROWing

            Comment

            • shimajavar
              New Member
              • Oct 2007
              • 17

              #7
              Originally posted by shimajavar
              I tried ur code, actually when i execute it, a line(200,200,20 0,180) is created.Program doesn't go throght the "b1_click" method
              it should grow by clicking on button , not by opening the form.
              Now I have some progress in the code...
              class Program :Form
              {
              int x1=200;
              int y1=200;
              int y2=200;
              private Thread animationThread ;

              public Program()
              {
              this.Size = new Size(400, 400);
              this.BackColor = Color.Wheat;
              this.StartPosit ion = FormStartPositi on.CenterScreen ;
              Button b1 = new Button();
              this.Controls.A dd(b1);
              b1.Click+=new EventHandler(b1 _Click);
              animationThread = new Thread(new ThreadStart(gro wingLine));

              }
              protected override void OnPaint(PaintEv entArgs e)
              {
              Graphics g = e.Graphics;
              Brush brush = new SolidBrush(Colo r.Black);
              Pen pen = new Pen(brush, 4);
              while(y2>50)
              {
              g.DrawLine(pen, x1, y1, x1, y2);
              Thread.Sleep(10 );
              Invalidate();
              y1 = y2;
              y2--;
              }


              }
              protected void b1_Click(object sender, EventArgs e)
              {
              if (animationThrea d.ThreadState == ThreadState.Uns tarted)
              {

              animationThread .Start();
              }
              else if (animationThrea d.ThreadState == ThreadState.Run ning ||
              animationThread .ThreadState == ThreadState.Wai tSleepJoin)
              {

              animationThread .Suspend();
              }
              else if (animationThrea d.ThreadState == ThreadState.Sus pended)
              {

              animationThread .Resume();
              }

              }

              public void growingLine()
              {
              y1 = y2;
              y2++;
              Invalidate();
              Thread.Sleep(40 );
              }

              but , the problem is, i want the line to grow up when i click on the button, but it growa up as soon as the form is open!!

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                You are creating an endless loop in your onPaint() function by moving it and then telling it to paint again.

                Comment

                • shimajavar
                  New Member
                  • Oct 2007
                  • 17

                  #9
                  Originally posted by Plater
                  You are creating an endless loop in your onPaint() function by moving it and then telling it to paint again.
                  There is a while loop in OnPaint:while(y 2>20)....
                  how if i move OnPaint in the other class?

                  class Program :Form
                  {

                  private Thread animationThread ;
                  private GrowingLine growingline;

                  public Program()
                  {
                  this.Size = new Size(400, 400);
                  this.BackColor = Color.Wheat;
                  this.StartPosit ion = FormStartPositi on.CenterScreen ;
                  Button b1 = new Button();
                  this.Controls.A dd(b1);
                  b1.Click+=new EventHandler(b1 _Click);
                  growingline = new GrowingLine();
                  animationThread = new Thread(new ThreadStart(gro wingline.StartG rowing));

                  }

                  protected void b1_Click(object sender, EventArgs e)
                  {
                  if (animationThrea d.ThreadState == ThreadState.Uns tarted)
                  {

                  animationThread .Start();
                  }
                  else if (animationThrea d.ThreadState == ThreadState.Run ning ||
                  animationThread .ThreadState == ThreadState.Wai tSleepJoin)
                  {

                  animationThread .Suspend();
                  }
                  else if (animationThrea d.ThreadState == ThreadState.Sus pended)
                  {

                  animationThread .Resume();
                  }

                  }

                  public static void Main(string[] args)
                  {
                  Application.Run (new Program());
                  Application.Exi t();
                  }
                  private void InitializeCompo nent()
                  {
                  this.SuspendLay out();
                  //
                  // Program
                  //
                  this.ClientSize = new System.Drawing. Size(292, 266);
                  this.Name = "Program";
                  this.Load += new System.EventHan dler(this.Progr am_Load);
                  this.ResumeLayo ut(false);

                  }

                  private void Program_Load(ob ject sender, EventArgs e)
                  {

                  }


                  }
                  class GrowingLine:Pan el
                  {
                  int x1;
                  int y1;
                  int y2;

                  public GrowingLine()
                  {
                  x1 = 200;
                  y1 = 200;
                  y2 = 200;
                  }

                  protected override void OnPaint(PaintEv entArgs e)
                  {
                  Graphics g = e.Graphics;
                  Brush brush = new SolidBrush(Colo r.Black);
                  Pen pen = new Pen(brush, 4);
                  while (y2 > 60)
                  {
                  g.DrawLine(pen, x1, y1, x1, y2);
                  Thread.Sleep(10 );
                  Invalidate();
                  y1 = y2;
                  y2--;
                  g.Dispose();
                  }
                  }

                  public void StartGrowing()
                  {
                  y1 = y2;
                  y2++;
                  Invalidate();
                  Thread.Sleep(40 );
                  }


                  }


                  OnPaint doesn't work in this case.

                  Comment

                  • Plater
                    Recognized Expert Expert
                    • Apr 2007
                    • 7872

                    #10
                    I think you really need to go back and read some tutorials on the basics, event handling and the onPaint event...

                    Comment

                    • shimajavar
                      New Member
                      • Oct 2007
                      • 17

                      #11
                      Originally posted by Plater
                      I think you really need to go back and read some tutorials on the basics, event handling and the onPaint event...
                      do u have any suggestions? (tutorials?)

                      Comment

                      • Plater
                        Recognized Expert Expert
                        • Apr 2007
                        • 7872

                        #12
                        I was able to accomplish the task with the code I provided (slightly modified to have it grow upward automatically)

                        Code:
                        private int x = 200;
                        private int y = 202;
                        
                                private void myform_Load(object sender, EventArgs e)
                                {
                                    Button bt = new Button();
                                    bt.Name = "bt";
                                    bt.Text = "Up";
                                    bt.Click += new EventHandler(bt_Click);
                                    this.Controls.Add(bt);
                                }
                        
                                void bt_Click(object sender, EventArgs e)
                                {
                                    AnimateUp();
                                }
                        
                                private void AnimateUp()
                                {
                                    int NumSteps = 10;
                                    int StepSize = 10;
                                    for (int i = 0; i < NumSteps; i++)
                                    {
                                        y = y - StepSize;
                                        this.Invalidate();
                                        this.Refresh();
                                        System.Threading.Thread.Sleep(100); 
                                        
                                    }
                                }
                        
                                private void myform_Paint(object sender, PaintEventArgs e)
                                {
                                    Graphics g=e.Graphics; 
                                    Pen blackpen = new Pen(Color.Black,4);
                                    g.DrawLine(blackpen, 200, 200, x, y);
                                }

                        msdn has lots of great tutorials and help on .NET

                        Comment

                        • shimajavar
                          New Member
                          • Oct 2007
                          • 17

                          #13
                          This code has the same problem as mine..no line is painted on the form!!!

                          Comment

                          • Plater
                            Recognized Expert Expert
                            • Apr 2007
                            • 7872

                            #14
                            Works fine for me.
                            Did you set that paint function to be the paint event handler for the form?

                            Comment

                            • shimajavar
                              New Member
                              • Oct 2007
                              • 17

                              #15
                              nah...How should i do it?

                              Comment

                              Working...