Click here to Skip to main content
15,885,159 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

anyone could please give me some idea about this, i want to move a circle from one location to another location in graphical mode, at the begging circle will be at any valid position then i will search next location. now circle should start movement from start position to next position gradually when it reaches to that exact location. so program should find the next location and then circle should move toward that.

and this process should be repeated infinitely..

any idea or piece of code or tutorial please.

thanks
Posted
Comments
Sampath Lokuge 13-Nov-13 4:04am    
What is your App (win or web )?
Fredrik Bornander 13-Nov-13 4:06am    
Framework used?
Web, Winform or WPF?
Muhammad Tufail 1979 13-Nov-13 4:10am    
windows application. please.
i used System.Form.Timer class for it. two timers objects in first i am trying to find the next location, as i get the next location then i want that timer 2 should be started and to move circle from one location to another gradually . and when timer 2 completes its task so timer 1 again search for the new location,
but the problem is that timer 1 does not allow timer 2 to start, as it find the next location.
firs timer1 completes and then timer 2 starts.
this is the problems. please.
BillWoodruff 13-Nov-13 9:50am    
This is essentially the same question you asked yesterday:

http://www.codeproject.com/Questions/680998/how-to-move-circle-on-a-picture-in-picturebox

When are you going to respond to the several previous responses to your other posts on this same problem ?
Muhammad Tufail 1979 13-Nov-13 9:55am    
Actually i am stuck in this problem and not finding any proper solution, thats why i am trying to find out the solution, i did try through timer, through threading , through loops. but still the problem exists.

1 solution

Don't use two timers - a single timer will be enough.
In the timer Tick event handler, decide if the circle has completed moving - if it has, then look for a new location, and start it moving towards it. If it hasn't, move it a step closer.

If the "find a new location" operation takes a long time, then move the code into a background worker thread so that it doesn't impact the main UI thread that is moving the circle, and kick it off to find a new location as soon as you start the circle moving towards a point. You then store the next destination when the task completes, and use it when the circle reaches it's current destination.

Without you existing code, I can't be more explicit, but if you treat it as two separate operations rather than one followed by the other it will probably be easier to work out.
 
Share this answer
 
Comments
Muhammad Tufail 1979 13-Nov-13 5:00am    
i am doing like this in one timer, ( System.Form.Timer) the problem is , i am using another timer in MoveCircle method. so that is not started and completed within this timer please.

private void Sim_Timer_Tick(object sender, EventArgs e)
{
pictureBox1.Image = testImage;
nxtno = utilObject.SelectNextCell(startCell, rnd);
moveCircle(startCell, nxtno); // call move circle method.
DrawCircle(Get_CellCenterPoint(nxtno));
startCell = nxtno;

}
OriginalGriff 13-Nov-13 5:14am    
Why are you using another timer in the move circle method?
The whole idea of a timer is that it *isn't* completed within the method, so the moveCircle method will return immediately - and you almost certainly shouldn't be actually drawing the circle from a timer Tick event anyway: that is what Paint events are for.

What are you trying to achieve that you think this is the right solution? There may be a better way...
Muhammad Tufail 1979 13-Nov-13 5:26am    
Actually i am using a picture box and an image , i have drawn a circle on that image and want to change the position of that circle in the image and to show it in the picture box.
thats why i am not using paint event, if paint is the best option so how to use it please.
OriginalGriff 13-Nov-13 5:41am    
Brrrrr! Nasty, nasty, nasty! You do realize that that doubles the amount of work involved?
First you have to clear the image, (possibly redraw the original image onto it) then draw your circle into it, then invalidate your picture box - which causes the Paint event, which clears the screen area, and draws the whole image as a bitmap on to it! :laugh:
Unless you actually want to save the image with the circle on it exactly as it is right now then that is an unnecessary complication - and I hope you are Disposing of any intermediate bitmaps and Graphics objects, or your program will fall over pretty quickly...

Instead, use the PictureBox.Paint event to draw the circle onto the surface using the provided Graphics context - the background image will be automatically refreshed - and any previous circles you drew will be discarded. You can cause the Paint just by calling Invalidate on the PictureBox.
Muhammad Tufail 1979 13-Nov-13 5:52am    
sorry i think i made you anger sorry for that.
could you please give me an example with a piece of code please.
here is my movecircle code and drawcircle code please.
private void moveCircle(int stCell, int nxtCell)
{
bool b = false;
Sim_Timer.Stop();

Point pointp = Get_CellCenterPoint(stCell);
Point pointnxt = Get_CellCenterPoint(nxtCell);
if (!b)

{

while (pointp.X != pointnxt.X && pointp.Y != pointnxt.Y)
{
int motionDirection = FindDirection(pointp, pointnxt);
switch (motionDirection)
{
case 1:
//event for Direction1 (North)

pointp.Y -= y_moveUnit;
if ((pointp.Y - pointnxt.Y) < y_moveUnit)
pointp.Y = pointnxt.Y;
DrawCircle(pointp);
Invalidate();
break;

case 2:
//event for Direction2 (NE)

pointp.X += x_moveUnit;
pointp.Y -= y_moveUnit;

if (Math.Abs(pointp.X - pointnxt.X) < x_moveUnit)
pointp.X = pointnxt.X;
if ((pointp.Y - pointnxt.Y) < y_moveUnit)
pointp.Y = pointnxt.Y;
DrawCircle(pointp);
Invalidate();
break;

case 3:
//event for Direction3 (East)

pointp.X += x_moveUnit;
if (Math.Abs(pointp.X - pointnxt.X) < x_moveUnit)
pointp.X = pointnxt.X;
DrawCircle(pointp);
Invalidate();
break;


case 4:
//event for Direction4 ( SE)

pointp.X += x_moveUnit;
pointp.Y += y_moveUnit;
if (Math.Abs(pointp.X - pointnxt.X) < x_moveUnit)
pointp.X = pointnxt.X;
if (Math.Abs(pointp.Y - pointnxt.Y) < y_moveUnit)
pointp.Y = pointnxt.Y;
DrawCircle(pointp);
Invalidate();
break;

case 5:
//event for Direction5 (South)

pointp.Y = y_moveUnit;
if (Math.Abs(pointp.Y - pointnxt.Y) < y_moveUnit)
pointp.Y = pointnxt.Y;
DrawCircle(pointp);
Invalidate();
break;

case 6:
//event for Direction6 (SW)

pointp.X -= x_moveUnit;
pointp.Y += y_moveUnit;
if (Math.Abs(pointp.X - pointnxt.X) < x_moveUnit)
pointp.X = pointnxt.X;
if (Math.Abs(pointp.Y - pointnxt.Y) < y_moveUnit)
pointp.Y = pointnxt.Y;
DrawCircle(pointp);
Invalidate();
break;

case 7:
//event for Direction7 (West)

pointp.X -= x_moveUnit;
if (Math.Abs(pointp.X - pointnxt.X) < x_moveUnit)
pointp.X = pointnxt.X;
DrawCircle

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900