winforms - C# How to Move A Circle Along a Rectangle Path -


i need move point along rectangle path @ command of button. want start @ upper right corner of rectangle path, not sure how go way around path , stop @ original point. screen refreshes @ speed provided user in input box. thank in advance!

using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms; using system.timers; namespace assignment_2 {      public partial class form1 : form     {         private const int formwidth = 1280;         private const int formheight = 720;         private const int ball_a_radius = 10;         private const int horizontaladjustment = 8;         private const double ball_a_distance_moved_per_refresh = 1.6;         private double ball_a_real_coord_x = 515;         private double ball_a_real_coord_y = 40;         private int ball_a_int_coord_x;         private int ball_a_int_coord_y;          private const double graphicrefreshrate = 30.0;          private static system.timers.timer graphic_area_refresh_clock = new system.timers.timer();          private static system.timers.timer ball_a_control_clock = new system.timers.timer();         private bool ball_a_clock_active = false;          public double speed = 0;          public form1()         {              initializecomponent();             ball_a_int_coord_x = (int)(ball_a_real_coord_x);             ball_a_int_coord_y = (int)(ball_a_real_coord_y);             system.console.writeline("initial coordinates: ball_a_int_coord_x = {0}. ball_a_int_coord_y = {1}.",                                ball_a_int_coord_x, ball_a_int_coord_y);              graphic_area_refresh_clock.enabled = false;               graphic_area_refresh_clock.elapsed += new elapsedeventhandler(updatedisplay);               ball_a_control_clock.enabled = false;              ball_a_control_clock.elapsed += new elapsedeventhandler(updateballa);              startgraphicclock(graphicrefreshrate);              startballaclock(speed);             }         public class numerictextbox : textbox         {         }              private void form1_load(object sender, eventargs e)         {          }          private void panel2_paint(object sender, painteventargs e)         {          }          private void panel2_paint_1(object sender, painteventargs e)         {             //create pen             pen blackpen = new pen(color.black, 1);              //create rectangle             rectangle rect = new rectangle(125, 50, 400, 400);              //draw rectangle screen             e.graphics.drawrectangle(blackpen, rect);              graphics graph = e.graphics;             graph.fillellipse(brushes.green, ball_a_int_coord_x, ball_a_int_coord_y, 2 * ball_a_radius, 2 * ball_a_radius);             base.onpaint(e);         }          public void button7_click(object sender, eventargs e)         {            speed = convert.toint32(textbox3.text);          }          private void textbox3_textchanged(object sender, eventargs e)         {          }          protected void startgraphicclock(double refreshrate)         {             double elapsedtimebetweentics;             if (refreshrate < 1.0) refreshrate = 1.0;               elapsedtimebetweentics = 1000.0 / refreshrate;              graphic_area_refresh_clock.interval = (int)system.math.round(elapsedtimebetweentics);             graphic_area_refresh_clock.enabled = true;           }          protected void startballaclock(double updaterate)         {             double elapsedtimebetweenballmoves;             if (updaterate < 1.0) updaterate = 1.0;              elapsedtimebetweenballmoves = 1000.0 / updaterate;              ball_a_control_clock.interval = (int)system.math.round(elapsedtimebetweenballmoves);             ball_a_control_clock.enabled = true;                ball_a_clock_active = true;         }          protected void updatedisplay(system.object sender, elapsedeventargs evt)         {             invalidate();               if (!(ball_a_clock_active))             {                 graphic_area_refresh_clock.enabled = false;                 system.console.writeline("the graphical area no longer refreshing.  may close window.");             }         }          protected void updateballa(system.object sender, elapsedeventargs evt)         {             ball_a_real_coord_x = ball_a_real_coord_x - 5;              ball_a_real_coord_y = ball_a_real_coord_y - 5;             ball_a_int_coord_x = (int)system.math.round(ball_a_real_coord_x);             ball_a_int_coord_y = (int)system.math.round(ball_a_real_coord_y);               if (ball_a_int_coord_x >= formwidth || ball_a_int_coord_y + 2 * ball_a_radius <= 0 || ball_a_int_coord_y >= formheight)             {                 ball_a_clock_active = false;                 ball_a_control_clock.enabled = false;                 system.console.writeline("the clock controlling ball has stopped.");             }          }          private void button4_click(object sender, eventargs e)         {             ball_a_control_clock.enabled = true;          }     }  } 

i have more straightforward way move circle. code long me read. see if this!

if you, use picturebox. first create image of circle, , put image in picturebox. can use timer change position of picturebox.

you should set interval of timer 33 ms, 30 fps. how programme timer:

keep counter indicate how many pixels circle has moved. let's want move in 100px x 50px rectangular path.

for every 33ms,     if counter less 100,          increase x position , counter 1,      if counter between 101 , 150,          increase y position , counter 1,      if counter between 151 , 250,          decrease x position 1 , increment counter     if counter between 251 , 300,         decrease y position 1 , increment counter     if counter greater 300,         stop timer 

i don't drawing stuff on screen onpaint event. mean, moving ball! people think of changing x , y positions of ball, not deleting ball @ previous position , drawing in new position. changing position of picture box makes lots more sense, don't think so?


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -